slug
import { slug } from "jsr:@ende/markdown";function
function slug(text: string): stringTurns text into a heading id the way GitHub does (with the algorithm of
github-slugger): it
lowercases the text, removes everything except letters, marks, numbers,
connector punctuation such as _, hyphens and spaces, and turns each space
into a hyphen. Runs of spaces and hyphens aren't collapsed, and the result
isn't trimmed, so ids match GitHub's anchors.
This is the default slugger of parse(), which also makes the ids
unique; slug() alone doesn't. Character categories come from the
runtime's Unicode version, which can be newer than github-slugger's
(Unicode 13).
text: stringThe text to slug, such as a heading's plain text.
Returns
The slug, which can be empty.
Example: Usage
import { slug } from "@ende/markdown/slug";
import { assertEquals } from "@ende/assert";
assertEquals(slug("Hello, World!"), "hello-world");
assertEquals(slug("What's new in 2.0?"), "whats-new-in-20");
assertEquals(slug("snake_case & kebab-case"), "snake_case--kebab-case");
assertEquals(slug("Déjà vu"), "déjà-vu");