Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional support for custom options.slugger #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@
* @typedef {import('hast').Root} Root
*/

/**
* @typedef {Function} SlugFunction
* Custom `slug` implementation (optional).
* @param {string} value
* String of text to 'slugify'
* @return {string}
* A unique slug string
*/

/**
* @typedef {Function} ResetFunction
* Custom `reset` implementation (optional).
*/

/**
* @typedef SluggerImplementation
* Custom `Slugger` implementation (optional).
* @property {SlugFunction} slug
* The function to call for `slug(string)`
* @property {ResetFunction} [reset]
* The function to call for `reset()`
*/

/**
* @typedef Options
* Configuration (optional).
* @property {string} [prefix='']
* Prefix to add in front of `id`s.
* @property {SluggerImplementation} [slugger]
* The `Slugger` implementation to use.
*/

import Slugger from 'github-slugger'
Expand All @@ -24,13 +49,16 @@ const slugs = new Slugger()
*/
export default function rehypeSlug(options = {}) {
const prefix = options.prefix || ''
const slugger = options.slugger || slugs

return (tree) => {
slugs.reset()
if (slugger.reset) {
slugger.reset()
}

visit(tree, 'element', (node) => {
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
node.properties.id = prefix + slugs.slug(toString(node))
node.properties.id = prefix + slugger.slug(toString(node))
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rehype-slug",
"name": "rehype-slugify",
"version": "5.1.0",
"description": "rehype plugin to add `id` attributes to headings",
"license": "MIT",
Expand Down Expand Up @@ -48,6 +48,7 @@
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",
"slugify": "^1.6.5",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
Expand Down
147 changes: 137 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import test from 'tape'
import slugify from 'slugify'
import {rehype} from 'rehype'
import rehypeSlug from './index.js'

test('rehypeSlug', (t) => {
t.plan(4)
t.plan(10)

rehype()
.data('settings', {fragment: true})
Expand All @@ -15,7 +16,8 @@ test('rehypeSlug', (t) => {
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>elit</h5>',
' <h5>Elit</h5>',
' <h5>Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
Expand All @@ -30,7 +32,8 @@ test('rehypeSlug', (t) => {
' <h2 id="dolorsitamet">dolor—sit—amet</h2>',
' <h3 id="consectetur--adipisicing">consectetur &#x26; adipisicing</h3>',
' <h4 id="elit">elit</h4>',
' <h5 id="elit-1">elit</h5>',
' <h5 id="elit-1">Elit</h5>',
' <h5 id="elit-">Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
Expand All @@ -41,15 +44,16 @@ test('rehypeSlug', (t) => {

rehype()
.data('settings', {fragment: true})
.use(rehypeSlug, {prefix: 'test-'})
.use(rehypeSlug, {prefix: 'prefix--'})
.process(
[
'<section>',
' <h1>Lorem ipsum 😪</h1>',
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>elit</h5>',
' <h5>Elit</h5>',
' <h5>Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
Expand All @@ -60,11 +64,134 @@ test('rehypeSlug', (t) => {
String(file),
[
'<section>',
' <h1 id="test-lorem-ipsum-">Lorem ipsum 😪</h1>',
' <h2 id="test-dolorsitamet">dolor—sit—amet</h2>',
' <h3 id="test-consectetur--adipisicing">consectetur &#x26; adipisicing</h3>',
' <h4 id="test-elit">elit</h4>',
' <h5 id="test-elit-1">elit</h5>',
' <h1 id="prefix--lorem-ipsum-">Lorem ipsum 😪</h1>',
' <h2 id="prefix--dolorsitamet">dolor—sit—amet</h2>',
' <h3 id="prefix--consectetur--adipisicing">consectetur &#x26; adipisicing</h3>',
' <h4 id="prefix--elit">elit</h4>',
' <h5 id="prefix--elit-1">Elit</h5>',
' <h5 id="prefix--elit-">Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
'should match'
)
}
)

rehype()
.data('settings', {fragment: true})
.use(rehypeSlug, {
slugger: {slug: (_value) => `test--${_value}--test`}
})
.process(
[
'<section>',
' <h1>Lorem ipsum 😪</h1>',
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>Elit</h5>',
' <h5>Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
(error, file) => {
t.ifErr(error, 'shouldn’t throw')

t.equal(
String(file),
[
'<section>',
' <h1 id="test--Lorem ipsum 😪--test">Lorem ipsum 😪</h1>',
' <h2 id="test--dolor—sit—amet--test">dolor—sit—amet</h2>',
' <h3 id="test--consectetur &#x26; adipisicing--test">consectetur &#x26; adipisicing</h3>',
' <h4 id="test--elit--test">elit</h4>',
' <h5 id="test--Elit--test">Elit</h5>',
' <h5 id="test--Elit --test">Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
'should match'
)
}
)

rehype()
.data('settings', {fragment: true})
.use(rehypeSlug, {
prefix: 'prefix--',
slugger: {
slug: (_value) =>
`test--${_value.replace(/\W/g, '').toLowerCase()}--test`
}
})
.process(
[
'<section>',
' <h1>Lorem ipsum 😪</h1>',
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>Elit</h5>',
' <h5>Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
(error, file) => {
t.ifErr(error, 'shouldn’t throw')

t.equal(
String(file),
[
'<section>',
' <h1 id="prefix--test--loremipsum--test">Lorem ipsum 😪</h1>',
' <h2 id="prefix--test--dolorsitamet--test">dolor—sit—amet</h2>',
' <h3 id="prefix--test--consecteturadipisicing--test">consectetur &#x26; adipisicing</h3>',
' <h4 id="prefix--test--elit--test">elit</h4>',
' <h5 id="prefix--test--elit--test">Elit</h5>',
' <h5 id="prefix--test--elit--test">Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
'should match'
)
}
)

rehype()
.data('settings', {fragment: true})
.use(rehypeSlug, {
prefix: 'prefix--',
slugger: {
slug: (_value) =>
slugify(_value, {lower: true, strict: true, replacement: '__'})
}
})
.process(
[
'<section>',
' <h1>Lorem ipsum 😪</h1>',
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>Elit</h5>',
' <h5>Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
(error, file) => {
t.ifErr(error, 'shouldn’t throw')

t.equal(
String(file),
[
'<section>',
' <h1 id="prefix--lorem__ipsum">Lorem ipsum 😪</h1>',
' <h2 id="prefix--dolorsitamet">dolor—sit—amet</h2>',
' <h3 id="prefix--consectetur__and__adipisicing">consectetur &#x26; adipisicing</h3>',
' <h4 id="prefix--elit">elit</h4>',
' <h5 id="prefix--elit">Elit</h5>',
' <h5 id="prefix--elit">Elit </h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"include": ["*.js"],
"exclude": ["test.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
Expand Down