diff --git a/lib/index.js b/lib/index.js index f7f3d71..9ecf8d9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,22 +1,28 @@ /** * @typedef {import('hast').Root} Root - * - * @typedef {import('hast-util-sanitize').Schema} Options - * The sanitation schema defines how and if nodes and properties should be cleaned. - * See `hast-util-sanitize`. - * The default schema is exported as `defaultSchema`. + * @typedef {import('hast-util-sanitize').Schema} Schema */ -import {sanitize as hastUtilSanitize, defaultSchema} from 'hast-util-sanitize' +import {sanitize} from 'hast-util-sanitize' /** * Plugin to sanitize HTML. * - * @type {import('unified').Plugin<[Options?] | Array, Root, Root>} + * @param {Schema | null | undefined} [options] + * Configuration (optional). + * @returns + * Transform. */ -export default function rehypeSanitize(options = defaultSchema) { - // @ts-expect-error: assume input `root` matches output root. - return (tree) => hastUtilSanitize(tree, options) +export default function rehypeSanitize(options) { + /** + * @param {Root} tree + * Tree. + * @returns {Root} + * New tree. + */ + return function (tree) { + // Assume root in -> root out. + const result = /** @type {Root} */ (sanitize(tree, options)) + return result + } } - -export {defaultSchema} from 'hast-util-sanitize' diff --git a/package.json b/package.json index 6c4db11..216ead4 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,7 @@ ], "dependencies": { "@types/hast": "^3.0.0", - "hast-util-sanitize": "^5.0.0", - "unified": "^11.0.0" + "hast-util-sanitize": "^5.0.0" }, "devDependencies": { "@types/node": "^20.0.0", diff --git a/readme.md b/readme.md index 0f6c528..83c1afe 100644 --- a/readme.md +++ b/readme.md @@ -178,13 +178,15 @@ ${`

${'Lorem ipsum dolor sit amet. '.repeat(20)}

\n`.repeat(20)} const file = await unified() .use(rehypeParse, {fragment: true}) - .use(() => (tree) => { - tree.children.push({ - type: 'element', - tagName: 'script', - properties: {type: 'module'}, - children: [{type: 'text', value: browser}] - }) + .use(function () { + return function (tree) { + tree.children.push({ + type: 'element', + tagName: 'script', + properties: {type: 'module'}, + children: [{type: 'text', value: browser}] + }) + } }) .use(rehypeStringify) .process(document) @@ -221,9 +223,9 @@ Changing `example.js`: const file = await unified() .use(rehypeParse, {fragment: true}) + .use(rehypeSanitize) - .use(() => (tree) => { - tree.children.push({ - type: 'element', + .use(function () { + return function (tree) { + tree.children.push({ ``` Now yields: @@ -255,14 +257,14 @@ window.addEventListener('hashchange', hashchange) // doesn’t emit `hashchange`. document.addEventListener( 'click', - (event) => { + function (event) { if ( event.target && event.target instanceof HTMLAnchorElement && event.target.href === location.href && location.hash.length > 1 ) { - setTimeout(() => { + setImmediate(function () { if (!event.defaultPrevented) { hashchange() } @@ -273,7 +275,7 @@ document.addEventListener( ) function hashchange() { - /** @type {string|undefined} */ + /** @type {string | undefined} */ let hash try { @@ -287,9 +289,9 @@ function hashchange() { document.getElementById(name) || document.getElementsByName(name)[0] if (target) { - setTimeout(() => { + setImmediate(function () { target.scrollIntoView() - }, 0) + }) } } ``` diff --git a/test.js b/test.js index 08622e0..7582a3f 100644 --- a/test.js +++ b/test.js @@ -5,6 +5,13 @@ import {rehype} from 'rehype' import rehypeSanitize, {defaultSchema} from './index.js' test('rehypeSanitize', async function (t) { + await t.test('should expose the public api', async function () { + assert.deepEqual(Object.keys(await import('./index.js')).sort(), [ + 'default', + 'defaultSchema' + ]) + }) + await t.test('should work', async function () { const file = await rehype() .use(rehypeSanitize) @@ -21,7 +28,6 @@ test('rehypeSanitize', async function (t) { '' ) - assert.equal(file.messages.length, 0) assert.equal(String(file), '') }) })