Skip to content

Commit 65d5f73

Browse files
committed
Refactor code-style
1 parent 13c820e commit 65d5f73

4 files changed

Lines changed: 43 additions & 30 deletions

File tree

lib/index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
/**
22
* @typedef {import('hast').Root} Root
3-
*
4-
* @typedef {import('hast-util-sanitize').Schema} Options
5-
* The sanitation schema defines how and if nodes and properties should be cleaned.
6-
* See `hast-util-sanitize`.
7-
* The default schema is exported as `defaultSchema`.
3+
* @typedef {import('hast-util-sanitize').Schema} Schema
84
*/
95

10-
import {sanitize as hastUtilSanitize, defaultSchema} from 'hast-util-sanitize'
6+
import {sanitize} from 'hast-util-sanitize'
117

128
/**
139
* Plugin to sanitize HTML.
1410
*
15-
* @type {import('unified').Plugin<[Options?] | Array<void>, Root, Root>}
11+
* @param {Schema | null | undefined} [options]
12+
* Configuration (optional).
13+
* @returns
14+
* Transform.
1615
*/
17-
export default function rehypeSanitize(options = defaultSchema) {
18-
// @ts-expect-error: assume input `root` matches output root.
19-
return (tree) => hastUtilSanitize(tree, options)
16+
export default function rehypeSanitize(options) {
17+
/**
18+
* @param {Root} tree
19+
* Tree.
20+
* @returns {Root}
21+
* New tree.
22+
*/
23+
return function (tree) {
24+
// Assume root in -> root out.
25+
const result = /** @type {Root} */ (sanitize(tree, options))
26+
return result
27+
}
2028
}
21-
22-
export {defaultSchema} from 'hast-util-sanitize'

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
],
3535
"dependencies": {
3636
"@types/hast": "^3.0.0",
37-
"hast-util-sanitize": "^5.0.0",
38-
"unified": "^11.0.0"
37+
"hast-util-sanitize": "^5.0.0"
3938
},
4039
"devDependencies": {
4140
"@types/node": "^20.0.0",

readme.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,15 @@ ${`<p>${'Lorem ipsum dolor sit amet. '.repeat(20)}</p>\n`.repeat(20)}
178178

179179
const file = await unified()
180180
.use(rehypeParse, {fragment: true})
181-
.use(() => (tree) => {
182-
tree.children.push({
183-
type: 'element',
184-
tagName: 'script',
185-
properties: {type: 'module'},
186-
children: [{type: 'text', value: browser}]
187-
})
181+
.use(function () {
182+
return function (tree) {
183+
tree.children.push({
184+
type: 'element',
185+
tagName: 'script',
186+
properties: {type: 'module'},
187+
children: [{type: 'text', value: browser}]
188+
})
189+
}
188190
})
189191
.use(rehypeStringify)
190192
.process(document)
@@ -221,9 +223,9 @@ Changing `example.js`:
221223
const file = await unified()
222224
.use(rehypeParse, {fragment: true})
223225
+ .use(rehypeSanitize)
224-
.use(() => (tree) => {
225-
tree.children.push({
226-
type: 'element',
226+
.use(function () {
227+
return function (tree) {
228+
tree.children.push({
227229
```
228230

229231
Now yields:
@@ -255,14 +257,14 @@ window.addEventListener('hashchange', hashchange)
255257
// doesn’t emit `hashchange`.
256258
document.addEventListener(
257259
'click',
258-
(event) => {
260+
function (event) {
259261
if (
260262
event.target &&
261263
event.target instanceof HTMLAnchorElement &&
262264
event.target.href === location.href &&
263265
location.hash.length > 1
264266
) {
265-
setTimeout(() => {
267+
setImmediate(function () {
266268
if (!event.defaultPrevented) {
267269
hashchange()
268270
}
@@ -273,7 +275,7 @@ document.addEventListener(
273275
)
274276

275277
function hashchange() {
276-
/** @type {string|undefined} */
278+
/** @type {string | undefined} */
277279
let hash
278280

279281
try {
@@ -287,9 +289,9 @@ function hashchange() {
287289
document.getElementById(name) || document.getElementsByName(name)[0]
288290

289291
if (target) {
290-
setTimeout(() => {
292+
setImmediate(function () {
291293
target.scrollIntoView()
292-
}, 0)
294+
})
293295
}
294296
}
295297
```

test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import {rehype} from 'rehype'
55
import rehypeSanitize, {defaultSchema} from './index.js'
66

77
test('rehypeSanitize', async function (t) {
8+
await t.test('should expose the public api', async function () {
9+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
10+
'default',
11+
'defaultSchema'
12+
])
13+
})
14+
815
await t.test('should work', async function () {
916
const file = await rehype()
1017
.use(rehypeSanitize)
@@ -21,7 +28,6 @@ test('rehypeSanitize', async function (t) {
2128
'<math><mi xlink:href="data:x,<script>alert(\'echo\')</script>"></mi></math>'
2229
)
2330

24-
assert.equal(file.messages.length, 0)
2531
assert.equal(String(file), '<math><mi></mi></math>')
2632
})
2733
})

0 commit comments

Comments
 (0)