Détail du package

github-slugger

Flet22.7mISC2.0.0

Generate a slug just like GitHub does for markdown headings.

anchor, github, hash, heading

readme

github-slugger

npm Build

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.

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.

Install

npm install github-slugger

Usage

import GithubSlugger from 'github-slugger'

const slugger = new GithubSlugger()

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 test/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 (not recommended):

import GithubSlugger, {slug} from 'github-slugger'

slug('foo bar baz')
// returns 'foo-bar-baz'

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

Contributing

Contributions welcome! Please read the contributing guidelines first.

License

ISC

changelog

github-slugger change log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

2.0.0 2022-10-27

  • Use ESM breaking: please read this guide
  • Add types breaking: tiny chance of breaking, use a new version of TS and it’ll work

1.5.0 2022-10-25

  • Update Unicode to 13, to match GH

1.4.0 2021-08-24

  • Fix to match GitHub’s algorithm on unicode

1.3.0 2020-02-21

  • Expose static slug function for folks who do not want/need the stateful bits (Thanks @bobbylito!).

1.2.1 2019-xx-xx

  • Fix collisions for slugs with occurrences
  • Fix accessing Object.prototype methods
  • Fix Russian
  • Add files to package.json

1.2.0 2017-09-21

  • Add maintainCase support

1.1.3 2017-05-29

  • Fixemoji-regex semver version to ensure npm5 compatibility.

1.1.2 2017-05-26

1.1.1

  • Add more conformant unicode handling to ensure:
    • emoji are correctly stripped
    • non-Latin characters are not incorrectly lowercased
  • Also adds more verified test cases

Check the PR for more details!

Thanks @wooorm!

1.1.0

1.0.1