Skip to content

Commit b5b0b44

Browse files
committed
Add another improved error when misnesting JSX in markdown
Related-to: e6661a6. Closes GH-5.
1 parent 188e258 commit b5b0b44

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
88
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
99
* @typedef {import('mdast-util-to-markdown').Map} ToMarkdownMap
10-
* @typedef {import('mdast-util-from-markdown').OnError} OnError
10+
* @typedef {import('mdast-util-from-markdown').OnEnterError} OnEnterError
11+
* @typedef {import('mdast-util-from-markdown').OnExitError} OnExitError
1112
* @typedef {import('estree-jsx').Program} Program
1213
* @typedef {import('./complex-types').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
1314
* @typedef {import('./complex-types').MdxJsxAttribute} MdxJsxAttribute
@@ -312,19 +313,19 @@ function exitMdxJsxTag(token) {
312313
children: []
313314
},
314315
token,
315-
onError
316+
onErrorRightIsTag
316317
)
317318
}
318319

319320
if (tag.selfClosing || tag.close) {
320-
this.exit(token)
321+
this.exit(token, onErrorLeftIsTag)
321322
} else {
322323
stack.push(tag)
323324
}
324325
}
325326

326-
/** @type {OnError} */
327-
function onError(closing, open) {
327+
/** @type {OnEnterError} */
328+
function onErrorRightIsTag(closing, open) {
328329
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
329330
const place = closing ? ' before the end of `' + closing.type + '`' : ''
330331
const position = closing
@@ -343,6 +344,26 @@ function onError(closing, open) {
343344
)
344345
}
345346

347+
/** @type {OnExitError} */
348+
function onErrorLeftIsTag(a, b) {
349+
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
350+
throw new VFileMessage(
351+
'Expected the closing tag `' +
352+
serializeAbbreviatedTag(tag) +
353+
'` either after the end of `' +
354+
b.type +
355+
'` (' +
356+
stringifyPosition(b.end) +
357+
') or another opening tag after the start of `' +
358+
b.type +
359+
'` (' +
360+
stringifyPosition(b.start) +
361+
')',
362+
{start: a.start, end: a.end},
363+
'mdast-util-mdx-jsx:end-tag-mismatch'
364+
)
365+
}
366+
346367
/**
347368
* Serialize a tag, excluding attributes.
348369
* `self-closing` is not supported, because we don’t need it yet.

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,17 @@ test('markdown -> mdast', (t) => {
11131113
'should crash when misnesting w/ label (image)'
11141114
)
11151115

1116+
t.throws(
1117+
() => {
1118+
fromMarkdown('<b> a *open </b> close* d.', {
1119+
extensions: [mdxJsx()],
1120+
mdastExtensions: [mdxJsxFromMarkdown]
1121+
})
1122+
},
1123+
/Expected the closing tag `<\/b>` either after the end of `emphasis` \(1:24\) or another opening tag after the start of `emphasis` \(1:7\)/,
1124+
'should crash when misnesting w/ attention (emphasis)'
1125+
)
1126+
11161127
t.deepEqual(
11171128
removePosition(
11181129
fromMarkdown('> a <b>\n> c </b> d.', {

0 commit comments

Comments
 (0)