Skip to content

Commit

Permalink
Skip intermediate TextDirective node
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Oct 1, 2021
1 parent 06aa0aa commit 41d66ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
26 changes: 9 additions & 17 deletions dev/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
*/

import assert from 'assert'
import {directiveFromMarkdown} from 'mdast-util-directive'
import {toString} from 'mdast-util-to-string'
import {parse} from 'micromark/lib/parse.js'
import {preprocess} from 'micromark/lib/preprocess.js'
Expand Down Expand Up @@ -1152,27 +1151,20 @@ function extension(combined, extension) {
*/
export function md(strings, ...values) {
// Interleave strings with `:expression`.
const tree = fromMarkdown(strings.join(':expression'), {
return fromMarkdown(strings.join(':expression'), {
extensions: [directive()],
mdastExtensions: [directiveFromMarkdown]
mdastExtensions: [{enter: {directiveText: enter}}]
})
// Swap `:expression` nodes for values.
visit(tree)
return tree

/**
* @param {Node} node
* @this {CompileContext}
* @returns {void}
*/
function visit(node) {
if ('children' in node) {
for (const [i, child] of node.children.entries()) {
if (child.type === 'textDirective' && child.name === 'expression') {
node.children[i] = /** @type {Content} */ (values.shift())
} else {
visit(child)
}
}
}
function enter() {
const parent = this.stack[this.stack.length - 1]
assert(parent, 'expected `parent`')
assert('children' in parent, 'expected `parent`')
// @ts-expect-error: Assume `Node` can exist as a child of `parent`.
parent.children.push(values.shift())
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"dependencies": {
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"mdast-util-directive": "^2.1.1",
"mdast-util-to-string": "^3.1.0",
"micromark": "^3.0.0",
"micromark-extension-directive": "^2.0.1",
Expand Down

0 comments on commit 41d66ef

Please sign in to comment.