Skip to main content

Generate a slug just like GitHub does for markdown headings. (JavaScript Port)

Project description

github-slugger

Build

This is a Python Fork of the JavaScript github-slugger package

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

pip install github-slugger

Usage

from github_slugger import GithubSlugger

slugger = 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):

from github_slugger import slug

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

github-slugger-0.0.3.tar.gz (7.7 kB view hashes)

Uploaded Source

Built Distribution

github_slugger-0.0.3-py3-none-any.whl (8.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page