Skip to content

Commit d2a2aef

Browse files
committed
Fix crash on regexp special characters as markers
1 parent 5ded7ea commit d2a2aef

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import {ok as assert} from 'devlop'
1515
import {toMatters} from 'micromark-extension-frontmatter'
16+
import escapeStringRegexp from 'escape-string-regexp'
1617

1718
/**
1819
* Create an extension for `mdast-util-from-markdown`.
@@ -108,7 +109,7 @@ export function frontmatterToMarkdown(options) {
108109
unsafe.push({
109110
atBreak: true,
110111
character: open.charAt(0),
111-
after: open.charAt(1)
112+
after: escapeStringRegexp(open.charAt(1))
112113
})
113114
}
114115

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"@types/mdast": "^4.0.0",
3939
"devlop": "^1.0.0",
40+
"escape-string-regexp": "^5.0.0",
4041
"mdast-util-from-markdown": "^2.0.0",
4142
"mdast-util-to-markdown": "^2.0.0",
4243
"micromark-extension-frontmatter": "^2.0.0"

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,32 @@ test('frontmatterFromMarkdown', async function (t) {
340340
})
341341
}
342342
)
343+
344+
await t.test(
345+
'should support regexp special characters as markers',
346+
async function () {
347+
const funky = {type: 'funky', marker: '*'}
348+
const tree = fromMarkdown('***\na\n***\n\n*a', {
349+
extensions: [frontmatter(funky)],
350+
mdastExtensions: [frontmatterFromMarkdown(funky)]
351+
})
352+
353+
removePosition(tree, {force: true})
354+
355+
assert.deepEqual(tree, {
356+
type: 'root',
357+
children: [
358+
{type: 'funky', value: 'a'},
359+
{type: 'paragraph', children: [{type: 'text', value: '*a'}]}
360+
]
361+
})
362+
363+
assert.deepEqual(
364+
toMarkdown(tree, {extensions: [frontmatterToMarkdown(funky)]}),
365+
'***\na\n***\n\n\\*a\n'
366+
)
367+
}
368+
)
343369
})
344370

345371
test('frontmatterToMarkdown', async function (t) {

0 commit comments

Comments
 (0)