From 09ac8937951659852aa72b3de4d720ed2ae81b5d Mon Sep 17 00:00:00 2001 From: Amp Date: Wed, 11 Nov 2026 13:52:32 +0000 Subject: [PATCH 1/3] fix: add `{.x}` to an element's existing class instead of replacing it Fixes #568 Amp-Thread-ID: https://ampcode.com/threads/T-01a1cade-e791-7145-8374-a0a67a3c1c91 Co-authored-by: Ahad Birang --- packages/comark/src/internal/parse/tree.ts | 8 ++++++- packages/comark/test/misc.test.ts | 27 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/comark/src/internal/parse/tree.ts b/packages/comark/src/internal/parse/tree.ts index 06c813fd..a284ca7b 100644 --- a/packages/comark/src/internal/parse/tree.ts +++ b/packages/comark/src/internal/parse/tree.ts @@ -246,7 +246,13 @@ function processChildren( function processPossibleAttributesSyntax(tokens: Token[], value: { nextIndex: number; node: Node }) { const extractedAttributes = extractAttributes(tokens, value.nextIndex) if (value.nextIndex < extractedAttributes.nextIndex) { - ;(value.node as ElementNode)[1] = Object.assign(value.node[1], extractedAttributes.attrs) + const attrs = extractedAttributes.attrs + const current = value.node[1] as ElementNodeAttributes + // `{.x}` adds to a class the element already has, like `attrJoin` in the markdown-it adapter. + if (typeof current.class === 'string' && typeof attrs.class === 'string') { + attrs.class = `${current.class} ${attrs.class}` + } + ;(value.node as ElementNode)[1] = Object.assign(current, attrs) value.nextIndex = extractedAttributes.nextIndex } diff --git a/packages/comark/test/misc.test.ts b/packages/comark/test/misc.test.ts index 0aab0203..0c678fd5 100644 --- a/packages/comark/test/misc.test.ts +++ b/packages/comark/test/misc.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' -import { parseMarkdown } from '../src/index' +import type { MarkdownItPlugin } from '../src/types' +import { defineComarkPlugin, parseMarkdown } from '../src/index' describe('misc', () => { it('ignore props with invalid chars, only /^[a-z0-9_-:]+$/gi', async () => { @@ -44,6 +45,30 @@ describe('misc', () => { ]) }) + it('adds `{.x}` to a class the element already has (#568)', async () => { + // A plugin that turns `++text++` into ``. + const markdownItKbd: MarkdownItPlugin = (md) => { + md.inline.ruler.before('emphasis', 'kbd', (state: any, silent: boolean) => { + if (!state.src.startsWith('++', state.pos)) return false + const end = state.src.indexOf('++', state.pos + 2) + if (end < 0) return false + if (!silent) { + state.push('mdc_inline_span_open', 'kbd', 1).attrs = [['class', 'key']] + state.push('text', '', 0).content = state.src.slice(state.pos + 2, end) + state.push('mdc_inline_span_close', 'kbd', -1) + } + state.pos = end + 2 + return true + }) + } + const kbd = defineComarkPlugin(() => ({ name: 'kbd', markdownItPlugins: [markdownItKbd] })) + + const doc = await parseMarkdown('++a++{.big .wide #k} ++b++{id="x"}', { plugins: [kbd()] }) + expect(doc.nodes).toEqual([ + ['p', {}, ['kbd', { class: 'key big wide', id: 'k' }, 'a'], ' ', ['kbd', { class: 'key', id: 'x' }, 'b']], + ]) + }) + describe('boolean attribute coercion', () => { it('promotes bare, explicit-true, and explicit-false attrs to :bindings', async () => { const tree = await parseMarkdown('::x{flag other="true" off="false" count="5"}\n::') From c1abc2373c2a8d6e9247f55736d942347bc811b8 Mon Sep 17 00:00:00 2001 From: Amp Date: Wed, 11 Nov 2026 14:01:33 +0000 Subject: [PATCH 2/3] fix: add `{.x}` to the class of plugin-emitted inline components Amp-Thread-ID: https://ampcode.com/threads/T-01a1cade-e791-7145-8374-a0a67a3c1c91 Co-authored-by: Ahad Birang --- packages/comark/src/internal/parse/tree.ts | 13 ++++++++----- packages/comark/test/misc.test.ts | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/comark/src/internal/parse/tree.ts b/packages/comark/src/internal/parse/tree.ts index a284ca7b..e9b01885 100644 --- a/packages/comark/src/internal/parse/tree.ts +++ b/packages/comark/src/internal/parse/tree.ts @@ -243,8 +243,12 @@ function processChildren( return { children, nextIndex: i } } -function processPossibleAttributesSyntax(tokens: Token[], value: { nextIndex: number; node: Node }) { - const extractedAttributes = extractAttributes(tokens, value.nextIndex) +function processPossibleAttributesSyntax( + tokens: Token[], + value: { nextIndex: number; node: Node }, + skipEmptyText: boolean = true +) { + const extractedAttributes = extractAttributes(tokens, value.nextIndex, skipEmptyText) if (value.nextIndex < extractedAttributes.nextIndex) { const attrs = extractedAttributes.attrs const current = value.node[1] as ElementNodeAttributes @@ -553,9 +557,8 @@ const processors: Record = { }, mdc_inline_component(tokens, start) { const token = tokens[start] - const tokenAttrs = processAttributes(token.attrs) - const { attrs, nextIndex } = extractAttributes(tokens, start + 1, false) - return { node: [token.tag, { ...tokenAttrs, ...attrs }] as Node, nextIndex } + const node = [token.tag, processAttributes(token.attrs)] as Node + return processPossibleAttributesSyntax(tokens, { node, nextIndex: start + 1 }, false) }, image(tokens, start) { diff --git a/packages/comark/test/misc.test.ts b/packages/comark/test/misc.test.ts index 0c678fd5..4edf8e91 100644 --- a/packages/comark/test/misc.test.ts +++ b/packages/comark/test/misc.test.ts @@ -46,7 +46,7 @@ describe('misc', () => { }) it('adds `{.x}` to a class the element already has (#568)', async () => { - // A plugin that turns `++text++` into ``. + // A plugin that turns `++text++` into `` and `%%` into ``. const markdownItKbd: MarkdownItPlugin = (md) => { md.inline.ruler.before('emphasis', 'kbd', (state: any, silent: boolean) => { if (!state.src.startsWith('++', state.pos)) return false @@ -60,12 +60,26 @@ describe('misc', () => { state.pos = end + 2 return true }) + md.inline.ruler.before('emphasis', 'icon', (state: any, silent: boolean) => { + if (!state.src.startsWith('%%', state.pos)) return false + if (!silent) state.push('mdc_inline_component', 'icon', 0).attrs = [['class', 'icon']] + state.pos += 2 + return true + }) } const kbd = defineComarkPlugin(() => ({ name: 'kbd', markdownItPlugins: [markdownItKbd] })) - const doc = await parseMarkdown('++a++{.big .wide #k} ++b++{id="x"}', { plugins: [kbd()] }) + const doc = await parseMarkdown('++a++{.big .wide #k} ++b++{id="x"} %%{.big}', { plugins: [kbd()] }) expect(doc.nodes).toEqual([ - ['p', {}, ['kbd', { class: 'key big wide', id: 'k' }, 'a'], ' ', ['kbd', { class: 'key', id: 'x' }, 'b']], + [ + 'p', + {}, + ['kbd', { class: 'key big wide', id: 'k' }, 'a'], + ' ', + ['kbd', { class: 'key', id: 'x' }, 'b'], + ' ', + ['icon', { class: 'icon big' }], + ], ]) }) From 5ede43db3635b59f320c764ad349483596bbaf53 Mon Sep 17 00:00:00 2001 From: Amp Date: Wed, 11 Nov 2026 14:10:47 +0000 Subject: [PATCH 3/3] fix: keep inline component attributes on a plain object Amp-Thread-ID: https://ampcode.com/threads/T-01a1cade-e791-7145-8374-a0a67a3c1c91 Co-authored-by: Ahad Birang --- packages/comark/src/internal/parse/tree.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/comark/src/internal/parse/tree.ts b/packages/comark/src/internal/parse/tree.ts index e9b01885..53b1ffd1 100644 --- a/packages/comark/src/internal/parse/tree.ts +++ b/packages/comark/src/internal/parse/tree.ts @@ -557,7 +557,8 @@ const processors: Record = { }, mdc_inline_component(tokens, start) { const token = tokens[start] - const node = [token.tag, processAttributes(token.attrs)] as Node + // Copy own properties so a `__proto__` attribute from a plugin token cannot set the prototype. + const node = [token.tag, { ...processAttributes(token.attrs) }] as Node return processPossibleAttributesSyntax(tokens, { node, nextIndex: start + 1 }, false) },