Skip to content

Commit 41d66ef

Browse files
committed
Skip intermediate TextDirective node
1 parent 06aa0aa commit 41d66ef

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

dev/lib/index.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
*/
8181

8282
import assert from 'assert'
83-
import {directiveFromMarkdown} from 'mdast-util-directive'
8483
import {toString} from 'mdast-util-to-string'
8584
import {parse} from 'micromark/lib/parse.js'
8685
import {preprocess} from 'micromark/lib/preprocess.js'
@@ -1152,27 +1151,20 @@ function extension(combined, extension) {
11521151
*/
11531152
export function md(strings, ...values) {
11541153
// Interleave strings with `:expression`.
1155-
const tree = fromMarkdown(strings.join(':expression'), {
1154+
return fromMarkdown(strings.join(':expression'), {
11561155
extensions: [directive()],
1157-
mdastExtensions: [directiveFromMarkdown]
1156+
mdastExtensions: [{enter: {directiveText: enter}}]
11581157
})
1159-
// Swap `:expression` nodes for values.
1160-
visit(tree)
1161-
return tree
11621158

11631159
/**
1164-
* @param {Node} node
1160+
* @this {CompileContext}
11651161
* @returns {void}
11661162
*/
1167-
function visit(node) {
1168-
if ('children' in node) {
1169-
for (const [i, child] of node.children.entries()) {
1170-
if (child.type === 'textDirective' && child.name === 'expression') {
1171-
node.children[i] = /** @type {Content} */ (values.shift())
1172-
} else {
1173-
visit(child)
1174-
}
1175-
}
1176-
}
1163+
function enter() {
1164+
const parent = this.stack[this.stack.length - 1]
1165+
assert(parent, 'expected `parent`')
1166+
assert('children' in parent, 'expected `parent`')
1167+
// @ts-expect-error: Assume `Node` can exist as a child of `parent`.
1168+
parent.children.push(values.shift())
11771169
}
11781170
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"dependencies": {
4444
"@types/mdast": "^3.0.0",
4545
"@types/unist": "^2.0.0",
46-
"mdast-util-directive": "^2.1.1",
4746
"mdast-util-to-string": "^3.1.0",
4847
"micromark": "^3.0.0",
4948
"micromark-extension-directive": "^2.0.1",

0 commit comments

Comments
 (0)