Skip to content

Commit

Permalink
Update @types/mdast, utilities, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 23, 2023
1 parent 53bbbb4 commit 1946cf3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ export default function remarkHtml(settings = {}) {
clean = true
}

Object.assign(this, {Compiler: compiler})
Object.assign(this, {compiler})

/**
* @type {import('unified').CompilerFunction<Root, string>}
* @type {import('unified').Compiler<Root, string>}
*/
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})
)
Expand Down
28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/entities-named/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sanitize": false,
"entities": {
"characterReferences": {
"useNamedReferences": true
}
}
57 changes: 27 additions & 30 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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'
Expand All @@ -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'}),
'<div></div>',
'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'}]}]
Expand All @@ -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'}],
Expand All @@ -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(),
'<p>changed</p>\n',
'should allow overriding handlers'
)

processorDangerous = remark()
const processorDangerous3 = remark()
.use(
/** @type {import('unified').Plugin<void[], Root>} */
() => (ast) => {
Expand All @@ -109,14 +106,14 @@ test('remarkHtml', (t) => {
.use(remarkHtml, {sanitize: false})

t.equal(
processorDangerous
processorDangerous3
.processSync('![hello](example.jpg "overwritten")')
.toString(),
'<p><img src="example.jpg" alt="hello" title="overwrite"></p>\n',
'should patch and merge attributes'
)

processorDangerous = remark()
const processorDangerous4 = remark()
.use(
/** @type {import('unified').Plugin<void[], Root>} */
() => (ast) => {
Expand All @@ -127,12 +124,12 @@ test('remarkHtml', (t) => {
.use(remarkHtml, {sanitize: false})

t.equal(
processorDangerous.processSync('**Bold!**').toString(),
processorDangerous4.processSync('**Bold!**').toString(),
'<p><b>Bold!</b></p>\n',
'should overwrite a tag-name'
)

processorDangerous = remark()
const processorDangerous5 = remark()
.use(
/** @type {import('unified').Plugin<void[], Root>} */
() => (ast) => {
Expand All @@ -154,12 +151,12 @@ test('remarkHtml', (t) => {
.use(remarkHtml, {sanitize: false})

t.equal(
processorDangerous.processSync('`var`').toString(),
processorDangerous5.processSync('`var`').toString(),
'<p><code><span class="token">var</span></code></p>\n',
'should overwrite content'
)

processorDangerous = remark()
const processorDangerous6 = remark()
.use(
/** @type {import('unified').Plugin<void[], Root>} */
() => (ast) => {
Expand All @@ -181,12 +178,12 @@ test('remarkHtml', (t) => {
.use(remarkHtml, {sanitize: true})

t.equal(
processorDangerous.processSync('`var`').toString(),
processorDangerous6.processSync('`var`').toString(),
'<p><code>var</code></p>\n',
'should not overwrite content in `sanitize` mode'
)

processorDangerous = remark()
const processorDangerous7 = remark()
.use(
/** @type {import('unified').Plugin<void[], Root>} */
() => (ast) => {
Expand All @@ -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(),
'<pre><code class="foo">var\n</code></pre>\n',
'should overwrite classes on code'
)
Expand Down
4 changes: 2 additions & 2 deletions test/integrations/footnotes/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<section data-footnotes class="footnotes"><h2 class="sr-only" id="footnote-label">Footnotes</h2>
<ol>
<li id="user-content-fn-1">
<p>Here is the footnote. <a href="#user-content-fnref-1" data-footnote-backref class="data-footnote-backref" aria-label="Back to content"></a></p>
<p>Here is the footnote. <a href="#user-content-fnref-1" data-footnote-backref="" aria-label="Back to reference 1" class="data-footnote-backref"></a></p>
</li>
<li id="user-content-fn-longnote">
<p>Here’s one with multiple blocks.</p>
Expand All @@ -18,7 +18,7 @@
</code></pre>
<p>The whole paragraph can be indented, or just the first
line. In this way, multi-paragraph footnotes work like
multi-paragraph list items. <a href="#user-content-fnref-longnote" data-footnote-backref class="data-footnote-backref" aria-label="Back to content"></a></p>
multi-paragraph list items. <a href="#user-content-fnref-longnote" data-footnote-backref="" aria-label="Back to reference 2" class="data-footnote-backref"></a></p>
</li>
</ol>
</section>

0 comments on commit 1946cf3

Please sign in to comment.