Install globally, via npm
is exported, so instead of " default opens in a new tab
bSlug" below, you can name the consumed function however you want.
Consume via a
const bSlug = require ( "bitbucket-slug" ) ;
or as an ES Module:
import bSlug from "bitbucket-slug" ; Install as a script, via CDN
Put this in your HTML:
<script src = "https://cdn.jsdelivr.net/npm/bitbucket-slug/dist/bitbucket-slug.umd.js " >
Then, you’ll get a global variable
bitbucketSlug in your
Global object and you consume it like this: opens in a new tab const bSlug = bitbucketSlug ;
bSlug is your main function. See its API in the main section.
This program has
113 assertions in its unit tests. That's more than all unit test assertions' median (52) and more than geometric mean (71). Idea
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 (
déjà vu ->
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 on the first repeated slug onwards.
There are only two dependencies:
ent to decode entities and opens in a new tab lodash.deburr to convert letters to basic Latin. opens in a new tab Competition
Whoever wonders, no,
slugify on npm won't match the BitBucket heading slug generation API. There are peculiarities which differ. opens in a new tab
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.
Usage const bSlug = require ( "bitbucket-slug" ) ; const res1 = bSlug ( ` ## So-called "music" ` ) ; console . log ( "res1 = " + JSON . stringify (res1 , null , 4 ) ) ; const res2 = bSlug ( "## Some Lithuanian - Ąžuolynas" ) ; console . log ( "res2 = " + JSON . stringify (res2 , null , 4 ) ) ; API
API is simple:
If the input is
null or not a string - empty string will be returned.
Licence MIT opens in a new tab
Copyright © 2015–2020 Roy Revelt and other contributors