Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
* @typedef {import('./lib/index.js').CompileContext} CompileContext
*/

export {fromMarkdown} from './lib/index.js'
export {fromMarkdown, md} from './lib/index.js'
26 changes: 26 additions & 0 deletions dev/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {toString} from 'mdast-util-to-string'
import {parse} from 'micromark/lib/parse.js'
import {preprocess} from 'micromark/lib/preprocess.js'
import {postprocess} from 'micromark/lib/postprocess.js'
import {directive} from 'micromark-extension-directive'
import {decodeNumericCharacterReference} from 'micromark-util-decode-numeric-character-reference'
import {decodeString} from 'micromark-util-decode-string'
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
Expand Down Expand Up @@ -1142,3 +1143,28 @@ function extension(combined, extension) {
}
}
}

/**
* @param {TemplateStringsArray} strings
* @param {...Node} values
* @returns {Root}
*/
export function md(strings, ...values) {
// Interleave strings with `:expression`.
return fromMarkdown(strings.join(':expression'), {
extensions: [directive()],
mdastExtensions: [{enter: {directiveText: enter}}]
})

/**
* @this {CompileContext}
* @returns {void}
*/
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())
Comment on lines +1178 to +1182
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from:

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)

}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/unist": "^2.0.0",
"mdast-util-to-string": "^3.1.0",
"micromark": "^3.0.0",
"micromark-extension-directive": "^2.0.1",
"micromark-util-decode-numeric-character-reference": "^1.0.0",
"micromark-util-decode-string": "^1.0.0",
"micromark-util-normalize-identifier": "^1.0.0",
Expand Down
53 changes: 51 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/

import assert from 'assert'
import {Buffer} from 'node:buffer'
import fs from 'fs'
import path from 'path'
import {Buffer} from 'node:buffer'
import test from 'tape'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
Expand All @@ -14,7 +14,7 @@ import {toHast} from 'mdast-util-to-hast'
import {toString} from 'mdast-util-to-string'
import {toHtml} from 'hast-util-to-html'
import {commonmark} from 'commonmark.json'
import {fromMarkdown} from '../dev/index.js'
import {fromMarkdown, md} from '../dev/index.js'

const join = path.join

Expand Down Expand Up @@ -1012,6 +1012,55 @@ test('mdast-util-from-markdown', (t) => {
'should parse a thematic break'
)

t.deepEqual(
md`*${fromMarkdown('a*b')}*`.children[0],
{
type: 'paragraph',
children: [
{
type: 'emphasis',
children: [
{
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'a*b',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 4, offset: 3}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 4, offset: 3}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 4, offset: 3}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 14, offset: 13}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 14, offset: 13}
}
},
'should substitute an expression'
)

t.end()
})

Expand Down