BitBucket readme file headings are automatically linked with anchors.
This library generates those anchor links, just in case you want to generate a "Table of Contents" or programmatically generate links to any given BitBucket headings.
We backwards-engineered the BitBucket slug-generation algorithm, and it appears to be:
- Strip all punctuation (
- Strip all emoji or non-letter characters (like
- Strip hashes which mean Markdown headings and single space that follows them (
- Replace each chunk of spaces with single hyphen
- Deburr (
- Strip non-latin letters (Cyrillic, Hiragana, Katakana etc.)
In BitBucket README's, there's a rule that no two slugs can be the same. If BitBucket slug-generation function generates the same URL, it starts to append
_2 and so on.
The main function
bSlug() is imported like this:
It's a function which takes three input arguments, third-one optional:
If the input is
null or not a string - empty string will be returned. In all other cases, function will return a string.
Whoever wonders, no,
slugify on npm won't match the BitBucket heading slug generation API. There are peculiarities which differ.
This library, on another hand, is aiming to match BitBucket spec as close as possible. Our unit tests are pinning the output of this library against the BitBucket-rendered HTML.