diff --git a/index.js b/index.js index 291874f..5ddfac8 100644 --- a/index.js +++ b/index.js @@ -37,20 +37,19 @@ export default function remarkHtml(settings = {}) { clean = true } - Object.assign(this, {Compiler: compiler}) + Object.assign(this, {compiler}) /** - * @type {import('unified').CompilerFunction} + * @type {import('unified').Compiler} */ function compiler(node, file) { const hast = toHast(node, { allowDangerousHtml: !clean, handlers: options.handlers }) - // @ts-expect-error: assume root. + // @ts-expect-error: to do: no longer boolean. const cleanHast = clean ? sanitize(hast, options.sanitize) : hast const result = toHtml( - // @ts-expect-error: assume root. cleanHast, Object.assign({}, options, {allowDangerousHtml: !clean}) ) diff --git a/package.json b/package.json index 77790c9..3fca8ab 100644 --- a/package.json +++ b/package.json @@ -37,32 +37,34 @@ "index.js" ], "dependencies": { - "@types/mdast": "^3.0.0", - "hast-util-sanitize": "^4.0.0", - "hast-util-to-html": "^8.0.0", - "mdast-util-to-hast": "^12.0.0", - "unified": "^10.0.0" + "@types/mdast": "^4.0.0", + "hast-util-sanitize": "^5.0.0", + "hast-util-to-html": "^9.0.0", + "mdast-util-to-hast": "^13.0.0", + "unified": "^11.0.0" }, "devDependencies": { + "@types/hast": "^3.0.0", "@types/tape": "^5.0.0", "c8": "^8.0.0", "commonmark.json": "^0.30.0", "is-hidden": "^2.0.0", "prettier": "^3.0.0", - "rehype-parse": "^8.0.0", - "rehype-stringify": "^9.0.0", - "remark": "^14.0.0", + "rehype-parse": "^9.0.0", + "rehype-stringify": "^10.0.0", + "remark": "^15.0.0", "remark-cli": "^11.0.0", - "remark-frontmatter": "^4.0.0", - "remark-gfm": "^3.0.0", - "remark-github": "^11.0.0", + "remark-frontmatter": "^5.0.0", + "remark-gfm": "^4.0.0", + "remark-github": "^12.0.0", "remark-preset-wooorm": "^9.0.0", "remark-slug": "^7.0.0", - "remark-toc": "^8.0.0", + "remark-toc": "^9.0.0", "tape": "^5.0.0", - "to-vfile": "^7.0.0", + "to-vfile": "^8.0.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", + "vfile": "^6.0.0", "xo": "^0.56.0" }, "scripts": { diff --git a/test/fixtures/entities-named/config.json b/test/fixtures/entities-named/config.json index 17b7979..d0df503 100644 --- a/test/fixtures/entities-named/config.json +++ b/test/fixtures/entities-named/config.json @@ -1,6 +1,6 @@ { "sanitize": false, - "entities": { + "characterReferences": { "useNamedReferences": true } } diff --git a/test/index.js b/test/index.js index e0a676f..2258722 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,7 @@ /** * @typedef {import('mdast').Root} Root * @typedef {import('mdast').Paragraph} Paragraph + * @typedef {import('hast').Element} Element * @typedef {import('vfile').VFile} VFile * @typedef {import('../index.js').Options} Options */ @@ -11,7 +12,6 @@ import test from 'tape' import {isHidden} from 'is-hidden' import {commonmark} from 'commonmark.json' import {toVFile} from 'to-vfile' -import {all} from 'mdast-util-to-hast' import {unified} from 'unified' import {remark} from 'remark' import remarkParse from 'remark-parse' @@ -29,28 +29,17 @@ test('remarkHtml', (t) => { remark().use(remarkHtml).freeze() }, 'should not throw if not passed options') - t.throws( - () => { - remark() - .use(remarkHtml) - // @ts-expect-error: not a node. - .stringify({type: 'root', children: [{value: 'baz'}]}) - }, - /Expected node, got `\[object Object]`/, - 'should throw when not given a node' - ) - - let processorDangerous = remark().use(remarkHtml, {sanitize: false}) + const processorDangerous1 = remark().use(remarkHtml, {sanitize: false}) t.equal( // @ts-expect-error: unknown node. - processorDangerous.stringify({type: 'alpha'}), + processorDangerous1.stringify({type: 'alpha'}), '
', 'should stringify unknown nodes' ) t.equal( - processorDangerous.stringify({ + processorDangerous1.stringify({ // @ts-expect-error: unknown node. type: 'alpha', children: [{type: 'strong', children: [{type: 'text', value: 'bravo'}]}] @@ -60,7 +49,7 @@ test('remarkHtml', (t) => { ) t.equal( - processorDangerous.stringify({ + processorDangerous1.stringify({ // @ts-expect-error: unknown node. type: 'alpha', children: [{type: 'text', value: 'bravo'}], @@ -74,29 +63,37 @@ test('remarkHtml', (t) => { 'should stringify unknown nodes' ) - processorDangerous = remark().use(remarkHtml, { + const processorDangerous2 = remark().use(remarkHtml, { sanitize: false, handlers: { /** @param {Paragraph} node */ - paragraph(h, node) { + paragraph(state, node) { const head = node.children[0] if (head.type === 'text') { head.value = 'changed' } - return h(node, 'p', all(h, node)) + /** @type {Element} */ + const result = { + type: 'element', + tagName: 'p', + properties: {}, + children: state.all(node) + } + state.patch(node, result) + return state.applyData(node, result) } } }) t.equal( - processorDangerous.processSync('paragraph text').toString(), + processorDangerous2.processSync('paragraph text').toString(), '

changed

\n', 'should allow overriding handlers' ) - processorDangerous = remark() + const processorDangerous3 = remark() .use( /** @type {import('unified').Plugin} */ () => (ast) => { @@ -109,14 +106,14 @@ test('remarkHtml', (t) => { .use(remarkHtml, {sanitize: false}) t.equal( - processorDangerous + processorDangerous3 .processSync('![hello](example.jpg "overwritten")') .toString(), '

hello

\n', 'should patch and merge attributes' ) - processorDangerous = remark() + const processorDangerous4 = remark() .use( /** @type {import('unified').Plugin} */ () => (ast) => { @@ -127,12 +124,12 @@ test('remarkHtml', (t) => { .use(remarkHtml, {sanitize: false}) t.equal( - processorDangerous.processSync('**Bold!**').toString(), + processorDangerous4.processSync('**Bold!**').toString(), '

Bold!

\n', 'should overwrite a tag-name' ) - processorDangerous = remark() + const processorDangerous5 = remark() .use( /** @type {import('unified').Plugin} */ () => (ast) => { @@ -154,12 +151,12 @@ test('remarkHtml', (t) => { .use(remarkHtml, {sanitize: false}) t.equal( - processorDangerous.processSync('`var`').toString(), + processorDangerous5.processSync('`var`').toString(), '

var

\n', 'should overwrite content' ) - processorDangerous = remark() + const processorDangerous6 = remark() .use( /** @type {import('unified').Plugin} */ () => (ast) => { @@ -181,12 +178,12 @@ test('remarkHtml', (t) => { .use(remarkHtml, {sanitize: true}) t.equal( - processorDangerous.processSync('`var`').toString(), + processorDangerous6.processSync('`var`').toString(), '

var

\n', 'should not overwrite content in `sanitize` mode' ) - processorDangerous = remark() + const processorDangerous7 = remark() .use( /** @type {import('unified').Plugin} */ () => (ast) => { @@ -198,7 +195,7 @@ test('remarkHtml', (t) => { .use(remarkHtml, {sanitize: false}) t.equal( - processorDangerous.processSync('```js\nvar\n```\n').toString(), + processorDangerous7.processSync('```js\nvar\n```\n').toString(), '
var\n
\n', 'should overwrite classes on code' ) diff --git a/test/integrations/footnotes/output.html b/test/integrations/footnotes/output.html index 377fd67..f789a38 100644 --- a/test/integrations/footnotes/output.html +++ b/test/integrations/footnotes/output.html @@ -8,7 +8,7 @@

Footnotes

  1. -

    Here is the footnote.

    +

    Here is the footnote.

  2. Here’s one with multiple blocks.

    @@ -18,7 +18,7 @@

    The whole paragraph can be indented, or just the first line. In this way, multi-paragraph footnotes work like -multi-paragraph list items.

    +multi-paragraph list items.