-
-
Notifications
You must be signed in to change notification settings - Fork 21
Add template literal tag #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
related previous discussion on use of js template syntax unifiedjs/unified#40 |
b3310ed
to
2df3ea6
Compare
Interesting stuff!
|
Thanks for this background!
Makes sense.
My goal is to get an AST, so raw Markdown -> mdast AST.
Okay, will do.
I've now added a test, to solicit feedback. I'm very happy to write docs if this is acceptable in principle, but yeah depends on earlier points: Should it be at this level? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of constructing TextDirective
nodes, then walking the tree and swapping them for the template args, I've now replaced the mdast extension with one that inserts the template args in the first place. This eliminates mdast-util-directive and the visitor.
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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from:
mdast-util-from-markdown/dev/lib/index.js
Lines 546 to 550 in c79a430
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(node) |
I've now replaced micromark-extension-directive. I first tried inserting events for expressions directly, something like: const prep = preprocess()
const tokenize = parse().document().write
const comp = compiler({
mdastExtensions: [
/** @type {Extension} */ (/** @type {unknown} */ ({expression}))
]
})
return comp(
postprocess(
strings.flatMap((string, i) => {
const end = i === strings.length - 1
const events = tokenize(prep(string, undefined, end))
if (!end)
events.push(
/** @type {Event} */ (/** @type {unknown} */ (['expression']))
)
return events
})
)
) However I found the resulting mdast (of Next I tried inserting into chunks, something like: return comp(
postprocess(
strings.flatMap((string, i) => {
const chunks = preprocess()(string, undefined, true)
if (i < strings.length - 1) chunks[chunks.length - 1] = -6
return tokenize(chunks)
})
)
) However I found if So I'm back to inserting into the input, like interleaving the strings with What do you think? |
(i was reminded of this thread due to https://twitter.com/wooorm/status/1446353015389237265) |
What’s the reason you’re focussing this on AST building, and not for example HTML or so building? |
I’m definitely open to discussing this more—it’s quite interesting—but I don’t think this is the place to add such code. |
Initial checklist
Description of changes
What do you think about adding a template literal tag, perhaps like so?
It allows me to wrap an existing mdast AST in a Markdown "template". For example:
Combined AST:
Possible approaches discussed in: syntax-tree/unist#54
The approach in this PR is tentatively mdast-util-directive: It interleaves the template strings with
:expression
, parses, then walks the result and swaps the:expression
nodes for the template args.