diff --git a/lib/index.js b/lib/index.js index 2b24764..d3392cb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -152,8 +152,12 @@ export default function remarkRehype(destination, options) { */ return function (tree, file) { // Cast because root in -> root out. + // To do: in the future, disallow ` || options` fallback. + // With `unified-engine`, `destination` can be `undefined` but + // `options` will be the file set. + // We should not pass that as `options`. return /** @type {HastRoot} */ ( - toHast(tree, {file, ...(options || destination)}) + toHast(tree, {file, ...(destination || options)}) ) } } diff --git a/package.json b/package.json index 8157372..bb5e15b 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@types/node": "^22.0.0", "c8": "^10.0.0", "prettier": "^3.0.0", + "rehype-slug": "^6.0.0", "rehype-stringify": "^10.0.0", "remark-cli": "^12.0.0", "remark-parse": "^11.0.0", diff --git a/test.js b/test.js index 7cd3b09..37ee527 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,9 @@ +/** + * @import {Text as HastText} from 'hast' + * @import {Handler} from 'mdast-util-to-hast' + * @import {Text as MdastText} from 'mdast' + */ + import assert from 'node:assert/strict' import test from 'node:test' import rehypeStringify from 'rehype-stringify' @@ -23,25 +29,42 @@ test('remarkRehype', async function (t) { .use(remarkParse) .use(remarkRehype) .use(rehypeStringify) - .process('## Hello, world! ##') + .process('# hi') ), - '

Hello, world!

' + '

hi

' ) }) - await t.test('should mutate with options', async function () { + await t.test('should mutate w/ options', async function () { assert.equal( String( await unified() .use(remarkParse) - .use(remarkRehype, {allowDangerousHtml: true}) - .use(rehypeStringify, {allowDangerousHtml: true}) - .process('## Hello, world! ##') + .use(remarkRehype, {handlers: {text}}) + .use(rehypeStringify) + .process('# hi') ), - '

Hello, world!

' + '

HI

' ) }) + await t.test( + 'should mutate w/ options and explicit unknown 2nd parameter', + async function () { + assert.equal( + String( + await unified() + .use(remarkParse) + // @ts-expect-error: this tests the file set passed by `unified-engine`. + .use(remarkRehype, {handlers: {text}}, {some: 'option'}) + .use(rehypeStringify) + .process('# hi') + ), + '

HI

' + ) + } + ) + await t.test( 'should mutate with `processor: undefined` and options', async function () { @@ -49,12 +72,12 @@ test('remarkRehype', async function (t) { String( await unified() .use(remarkParse) - // @ts-expect-error: this is not typed as being supported w/ the overload, but always was. - .use(remarkRehype, undefined, {allowDangerousHtml: true}) - .use(rehypeStringify, {allowDangerousHtml: true}) - .process('## Hello, world! ##') + // @ts-expect-error: this tests the file set passed by `unified-engine`. + .use(remarkRehype, undefined, {handlers: {text}}) + .use(rehypeStringify) + .process('# hi') ), - '

Hello, world!

' + '

HI

' ) } ) @@ -103,3 +126,12 @@ test('remarkRehype', async function (t) { assert.equal(document, '

hi

') }) }) + +/** + * @type {Handler} + * @param {MdastText} node + * @returns {HastText} + */ +function text(_, node) { + return {type: 'text', value: node.value.toUpperCase()} +}