Skip to content

Commit

Permalink
Fix mutate support in unified-engine
Browse files Browse the repository at this point in the history
A bug was introduced in a refactor in 4009084.
Where `destination || options` was mistakingly changed for
`options || destination`.
This adds a test to prevent that.
Though, in the next major, this behavior should be dropped.

Closes GH-35.
  • Loading branch information
wooorm committed Sep 19, 2024
1 parent e4576c0 commit f0cce2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 44 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -23,38 +29,55 @@ test('remarkRehype', async function (t) {
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process('## Hello, world! ##')
.process('# hi')
),
'<h2>Hello, world!</h2>'
'<h1>hi</h1>'
)
})

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, <i>world</i>! ##')
.use(remarkRehype, {handlers: {text}})
.use(rehypeStringify)
.process('# hi')
),
'<h2>Hello, <i>world</i>!</h2>'
'<h1>HI</h1>'
)
})

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')
),
'<h1>HI</h1>'
)
}
)

await t.test(
'should mutate with `processor: undefined` and options',
async function () {
assert.equal(
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, <i>world</i>! ##')
// @ts-expect-error: this tests the file set passed by `unified-engine`.
.use(remarkRehype, undefined, {handlers: {text}})
.use(rehypeStringify)
.process('# hi')
),
'<h2>Hello, <i>world</i>!</h2>'
'<h1>HI</h1>'
)
}
)
Expand Down Expand Up @@ -103,3 +126,12 @@ test('remarkRehype', async function (t) {
assert.equal(document, '<p>hi</p>')
})
})

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

0 comments on commit f0cce2d

Please sign in to comment.