From 899b3b73f2b13b10588ef3096d5dd2d5ca40d79f Mon Sep 17 00:00:00 2001 From: Onyx Date: Mon, 31 Oct 2022 05:27:17 -0400 Subject: [PATCH] Add `option.prefix` Related-to: GH-13. Closes GH-14. Closes GH-15. Reviewed-by: Titus Wormer Co-authored-by: Titus Wormer --- index.js | 15 ++++++++++++--- readme.md | 13 ++++++++++--- test.js | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5a8e085..54009b1 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,13 @@ * @typedef {import('hast').Root} Root */ +/** + * @typedef Options + * Configuration (optional). + * @property {string} [prefix=''] + * Prefix to add in front of `id`s. + */ + import Slugger from 'github-slugger' import {hasProperty} from 'hast-util-has-property' import {headingRank} from 'hast-util-heading-rank' @@ -13,15 +20,17 @@ const slugs = new Slugger() /** * Plugin to add `id`s to headings. * - * @type {import('unified').Plugin, Root>} + * @type {import('unified').Plugin<[Options?]|Array, Root>} */ -export default function rehypeSlug() { +export default function rehypeSlug(options = {}) { + const prefix = options.prefix || '' + return (tree) => { slugs.reset() visit(tree, 'element', (node) => { if (headingRank(node) && node.properties && !hasProperty(node, 'id')) { - node.properties.id = slugs.slug(toString(node)) + node.properties.id = prefix + slugs.slug(toString(node)) } }) } diff --git a/readme.md b/readme.md index 0fab04d..286d9c2 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ * [Install](#install) * [Use](#use) * [API](#api) - * [`unified().use(rehypeSlug)`](#unifieduserehypeslug) + * [`unified().use(rehypeSlug[, options])`](#unifieduserehypeslug-options) * [Types](#types) * [Compatibility](#compatibility) * [Security](#security) @@ -117,10 +117,17 @@ Now, running `node example.js` yields: This package exports no identifiers. The default export is `rehypeSlug`. -### `unified().use(rehypeSlug)` +### `unified().use(rehypeSlug[, options])` Add `id`s to headings. -There are no options. + +##### `options` + +Configuration (optional). + +###### `options.prefix` + +Prefix to add in front of `id`s (`string`, default: `''`). ## Types diff --git a/test.js b/test.js index 6899a59..31a3ae8 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,7 @@ import {rehype} from 'rehype' import rehypeSlug from './index.js' test('rehypeSlug', (t) => { - t.plan(2) + t.plan(4) rehype() .data('settings', {fragment: true}) @@ -38,4 +38,38 @@ test('rehypeSlug', (t) => { ) } ) + + rehype() + .data('settings', {fragment: true}) + .use(rehypeSlug, {prefix: 'test-'}) + .process( + [ + '
', + '

Lorem ipsum 😪

', + '

dolor—sit—amet

', + '

consectetur & adipisicing

', + '

elit

', + '
elit
', + '

sed

', + '
' + ].join('\n'), + (error, file) => { + t.ifErr(error, 'shouldn’t throw') + + t.equal( + String(file), + [ + '
', + '

Lorem ipsum 😪

', + '

dolor—sit—amet

', + '

consectetur & adipisicing

', + '

elit

', + '
elit
', + '

sed

', + '
' + ].join('\n'), + 'should match' + ) + } + ) })