1 unstable release

0.1.0 Jun 22, 2023

#1619 in Text processing

Download history 27/week @ 2024-04-05 32/week @ 2024-04-12 26/week @ 2024-04-19 22/week @ 2024-04-26 28/week @ 2024-05-03 17/week @ 2024-05-10 8/week @ 2024-05-17 28/week @ 2024-05-24 22/week @ 2024-05-31 22/week @ 2024-06-07 30/week @ 2024-06-14 25/week @ 2024-06-21 5/week @ 2024-06-28 11/week @ 2024-07-05 24/week @ 2024-07-12 27/week @ 2024-07-19

69 downloads per month
Used in 2 crates (via markdown-it-heading-ancho…)

Apache-2.0

43KB

github-slugger

crates.io

Generate a slug just like GitHub does for markdown headings. It also ensures slugs are unique in the same way GitHub does it. The overall goal of this package is to emulate the way GitHub handles generating markdown heading anchors as close as possible. It is based on the github-slugger JavaScript package.

This project is not a markdown or HTML parser: passing alpha *bravo* charlie or alpha <em>bravo</em> charlie doesn’t work. Instead pass the plain text value of the heading: alpha bravo charlie.

Usage

let mut slugger = github_slugger::Slugger::default();

slugger.slug("foo")
// returns 'foo'

slugger.slug("foo")
// returns 'foo-1'

slugger.slug("bar")
// returns 'bar'

slugger.slug("foo")
// returns 'foo-2'

slugger.slug("Привет non-latin 你好")
// returns 'привет-non-latin-你好'

slugger.slug("😄 emoji")
// returns '-emoji'

slugger.reset()

slugger.slug("foo")
// returns 'foo'

Check tests/fixtures.json for more examples.

If you need, you can also use the underlying implementation which does not keep track of the previously slugged strings:

github_slugger::slug("foo bar baz")
// returns 'foo-bar-baz'

github_slugger::slug("foo bar baz")
// returns the same slug 'foo-bar-baz' because it does not keep track

Dependencies

~2.2–3MB
~54K SLoC