Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
wooorm committed Aug 26, 2023
1 parent 13c820e commit 65d5f73
Showing 4 changed files with 43 additions and 30 deletions.
30 changes: 18 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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<void>, 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'
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
32 changes: 17 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
@@ -178,13 +178,15 @@ ${`<p>${'Lorem ipsum dolor sit amet. '.repeat(20)}</p>\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)
})
}
}
```
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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) {
'<math><mi xlink:href="data:x,<script>alert(\'echo\')</script>"></mi></math>'
)

assert.equal(file.messages.length, 0)
assert.equal(String(file), '<math><mi></mi></math>')
})
})

0 comments on commit 65d5f73

Please sign in to comment.