From e822bf6ce51ca82b316515b055d52d08c7067887 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 28 Sep 2024 02:18:28 +0400 Subject: [PATCH] Add method Repubmark.validate_slug! --- lib/repubmark.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/repubmark.rb b/lib/repubmark.rb index de0507f..5ea22d5 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -66,6 +66,8 @@ require_relative 'repubmark/elems/footnote' module Repubmark FORMATS = %i[gemtext html summary_plain word_count].freeze + SLUG_RE = /\A\w+(-\w+)*\z/ + UNICODE_SUPS = { '0' => '⁰', '1' => '¹', @@ -102,6 +104,13 @@ module Repubmark ')' => '₎', }.freeze + def self.validate_slug!(slug) + slug = String(slug).freeze + raise 'Invalid slug' unless SLUG_RE.match? slug + + slug + end + def self.unicode_sup(val) String(val).each_char.map { |chr| UNICODE_SUPS.fetch chr }.join.freeze end