diff --git a/packages/comark/src/internal/parse/syntax/props.ts b/packages/comark/src/internal/parse/syntax/props.ts index 99da509f..0339e1c1 100644 --- a/packages/comark/src/internal/parse/syntax/props.ts +++ b/packages/comark/src/internal/parse/syntax/props.ts @@ -24,6 +24,7 @@ export function searchProps(content: string, index = 0) { const props: [string, string][] = [] let closed = false + let hasInvalid = false // Skip Vue mustache `{{ }}` syntax if (content[index + 1] === '{') return undefined @@ -32,6 +33,7 @@ export function searchProps(content: string, index = 0) { while (index < content.length) { if (content[index] === '\\') { + hasInvalid = true index += 2 } else if (content[index] === '}') { index += 1 @@ -41,10 +43,14 @@ export function searchProps(content: string, index = 0) { index += 1 } else if (content[index] === '.') { index += 1 - props.push(['class', searchUntil(' #.}', true)]) + const cls = searchUntil(' #.}', true) + if (cls) props.push(['class', cls]) + else hasInvalid = true } else if (content[index] === '#') { index += 1 - props.push(['id', searchUntil(' #.}')]) + const id = searchUntil(' #.}') + if (id) props.push(['id', id]) + else hasInvalid = true } else { const start = index while (index < content.length) { @@ -64,6 +70,8 @@ export function searchProps(content: string, index = 0) { } if (key.match(/^:?[a-z_][a-z0-9_-]*$/gi)) { props.push([key, value]) + } else { + hasInvalid = true } } } @@ -126,5 +134,6 @@ export function searchProps(content: string, index = 0) { props, index, closed, + hasInvalid, } } diff --git a/packages/comark/src/plugins/attributes.ts b/packages/comark/src/plugins/attributes.ts index 0c96163d..13ca9fd0 100644 --- a/packages/comark/src/plugins/attributes.ts +++ b/packages/comark/src/plugins/attributes.ts @@ -59,8 +59,11 @@ const markdownItInlineProps: PluginSimple = (md) => { return false } - const { props, index: end } = search - if (end === start) return false + const { props, index: end, hasInvalid } = search + if (end === start || (hasInvalid && (afterText || props.length === 0))) { + if (afterText && !afterCloser) rejectedUntil.set(state, end) + return false + } if (afterText && !afterCloser) { let searchEnd = end diff --git a/packages/comark/test/misc.test.ts b/packages/comark/test/misc.test.ts index 4edf8e91..d9c1f50f 100644 --- a/packages/comark/test/misc.test.ts +++ b/packages/comark/test/misc.test.ts @@ -119,4 +119,24 @@ describe('misc', () => { }) }) }) + + it('preserves invalid inline attribute groups as text (#522)', async () => { + const slash = await parseMarkdown('**Result** {a/b}') + expect(slash.nodes).toEqual([['p', {}, ['strong', {}, 'Result'], ' {a/b}']]) + + const tex = await parseMarkdown(String.raw`**Result** {(3\,574+5\,852)\times9/44}`) + expect(tex.nodes).toEqual([['p', {}, ['strong', {}, 'Result'], ' {(3,574+5,852)\\times9/44}']]) + + const mixed = await parseMarkdown('**Result** {isDev, isServer}') + expect(mixed.nodes).toEqual([['p', {}, ['strong', {}, 'Result'], ' {isDev, isServer}']]) + + const afterStrong = await parseMarkdown('**Result**{a/b}') + expect(afterStrong.nodes).toEqual([['p', {}, ['strong', {}, 'Result'], '{a/b}']]) + + const afterLink = await parseMarkdown('[Result](/x){a/b}') + expect(afterLink.nodes).toEqual([['p', {}, ['a', { href: '/x' }, 'Result'], '{a/b}']]) + + const valid = await parseMarkdown('**Result** {title="valid"}') + expect(valid.nodes).toEqual([['p', { title: 'valid' }, ['strong', {}, 'Result']]]) + }) })