Skip to content

Commit 57fd68b

Browse files
Add support for afterTransform hook
Reviewed-by: Titus Wormer <tituswormer@gmail.com> Closes GH-14. Closes GH-15.
1 parent 5663ec8 commit 57fd68b

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

lib/index.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,30 @@
88
* @typedef {HastParent['children'][number]} HastChild
99
* @typedef {HastChild|HastRoot} HastNode
1010
*
11+
* @callback AfterTransform
12+
* Function called when a hast node is transformed into a DOM node
13+
* @param {HastNode} hastNode
14+
* The hast node that was handled
15+
* @param {Node} domNode
16+
* The corresponding DOM node
17+
* @returns {void}
18+
*
1119
* @typedef Options
12-
* @property {boolean} [fragment=false] Whether a DOM fragment should be returned
13-
* @property {Document} [document] Document interface to use (default: `globalThis.document`)
14-
* @property {string} [namespace] `namespace` to use to create elements
20+
* @property {boolean} [fragment=false]
21+
* Whether a DOM fragment should be returned
22+
* @property {Document} [document]
23+
* Document interface to use (default: `globalThis.document`)
24+
* @property {string} [namespace]
25+
* `namespace` to use to create elements
26+
* @property {AfterTransform} [afterTransform]
27+
* Callback invoked after each node transformation
1528
*
1629
* @typedef Context
1730
* @property {Document} doc
1831
* @property {boolean} [fragment=false]
1932
* @property {string} [namespace]
2033
* @property {string} [impliedNamespace]
34+
* @property {AfterTransform} [afterTransform]
2135
*/
2236

2337
import {webNamespaces} from 'web-namespaces'
@@ -30,6 +44,16 @@ import {find, html, svg} from 'property-information'
3044
* @param {Context} ctx
3145
*/
3246
function transform(node, ctx) {
47+
const transformed = one(node, ctx)
48+
if (ctx.afterTransform) ctx.afterTransform(node, transformed)
49+
return transformed
50+
}
51+
52+
/**
53+
* @param {HastNode} node
54+
* @param {Context} ctx
55+
*/
56+
function one(node, ctx) {
3357
switch (node.type) {
3458
case 'root':
3559
return root(node, ctx)

readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ Document interface to use (default: `globalThis.document`).
8888

8989
`namespace` to use to create [*elements*][element].
9090

91+
###### `options.afterTransform`
92+
93+
Function called when a hast node is transformed into a DOM node (`Function?`).
94+
Given the hast node that was handled as the first parameter and the
95+
corresponding DOM node as the second parameter.
96+
9197
## Security
9298

9399
Use of `hast-util-to-dom` can open you up to a

test/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,25 @@ test('hast-util-to-dom', (t) => {
359359
'encodes data properties when string'
360360
)
361361

362+
t.deepEqual(
363+
(() => {
364+
/** @type {Array<[HastNode, string]>} */
365+
const calls = []
366+
toDom(h('html', [h('title', 'Hi')]), {
367+
afterTransform: (node, transformed) => {
368+
calls.push([node, serializeNodeToHtmlString(transformed)])
369+
}
370+
})
371+
return calls
372+
})(),
373+
[
374+
[{type: 'text', value: 'Hi'}, 'Hi'],
375+
[h('title', 'Hi'), '<title>Hi</title>'],
376+
[h('html', [h('title', 'Hi')]), '<html><title>Hi</title></html>']
377+
],
378+
'should invoke afterTransform'
379+
)
380+
362381
t.end()
363382
})
364383

0 commit comments

Comments
 (0)