Module: RedQuilt::Slug

Defined in:
lib/red_quilt/slug.rb

Overview

Heading-anchor slugs, following GitHub’s github-slugger approach: downcase, strip punctuation, spaces to hyphens – but keep Unicode letters/marks/numbers verbatim, so Japanese (and other non-ASCII) headings survive instead of collapsing to empty ids.

Defined Under Namespace

Classes: Counter

Constant Summary collapse

STRIP_RE =

Drop anything that is not a letter, mark, number, underscore, space, or hyphen. Browsers percent-encode non-ASCII fragment ids on the wire but resolve and display them fine, matching GitHub.

/[^\p{L}\p{M}\p{N}_ -]+/u
SPACE_RE =
/ +/

Class Method Summary collapse

Class Method Details

.slugify(text) ⇒ Object



17
18
19
20
# File 'lib/red_quilt/slug.rb', line 17

def slugify(text)
  base = text.downcase.gsub(STRIP_RE, "").strip.gsub(SPACE_RE, "-")
  base.empty? ? "section" : base
end