From 66cb2a8e90ecedf387b92c6bd0bf7b11ed693718 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 1 Nov 2026 13:42:23 +0000 Subject: [PATCH 1/3] fix(render): keep raw HTML attribute names and values verbatim in markdown --- .../comark/src/internal/stringify/attributes.ts | 2 +- .../src/internal/stringify/handlers/html.ts | 5 ++++- .../src/internal/stringify/html-attributes.ts | 15 +++++++++++---- packages/comark/test/html-roundtrip.test.ts | 2 ++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/comark/src/internal/stringify/attributes.ts b/packages/comark/src/internal/stringify/attributes.ts index 75881c533..c3b811c01 100644 --- a/packages/comark/src/internal/stringify/attributes.ts +++ b/packages/comark/src/internal/stringify/attributes.ts @@ -1,7 +1,7 @@ import { stringifyYaml } from '../yaml.ts' import { pickFence } from './fence.ts' -export { resolveAttributes, resolveAttribute, htmlAttributes } from './html-attributes.ts' +export { resolveAttributes, resolveAttribute } from './html-attributes.ts' export type { ResolveAttributesOptions } from './html-attributes.ts' // Implicit attributes the parser injects per tag — they're conveyed by the diff --git a/packages/comark/src/internal/stringify/handlers/html.ts b/packages/comark/src/internal/stringify/handlers/html.ts index 72bd075f9..0a7dc61ae 100644 --- a/packages/comark/src/internal/stringify/handlers/html.ts +++ b/packages/comark/src/internal/stringify/handlers/html.ts @@ -100,7 +100,10 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode } const nativeAttributes = toNativeAttributes(String(tag), node, attributes) - const attrs = Object.keys(nativeAttributes).length > 0 ? ` ${htmlAttributes(nativeAttributes)}` : '' + const attrs = + Object.keys(nativeAttributes).length > 0 + ? ` ${htmlAttributes(nativeAttributes, { verbatim: !state.context.html && $.html === 1 })}` + : '' if (isSelfClose) { return `<${tag}${attrs}>` + (!parent && !isInline ? state.context.blockSeparator : '') diff --git a/packages/comark/src/internal/stringify/html-attributes.ts b/packages/comark/src/internal/stringify/html-attributes.ts index 9e9e8a4e6..2e1ae4176 100644 --- a/packages/comark/src/internal/stringify/html-attributes.ts +++ b/packages/comark/src/internal/stringify/html-attributes.ts @@ -122,19 +122,26 @@ export function resolveAttribute(attrs: Record, renderData: Nod // could break out of the attribute list, so such keys are dropped entirely. const SAFE_ATTR_NAME = /^[a-zA-Z_:][a-zA-Z0-9_:.-]*$/ +interface HtmlAttributesOptions { + /** Keep `:` prefixes and string `"true"` values as written */ + verbatim?: boolean +} + /** * Convert attributes to a string of HTML attributes * * @param attributes - The attributes to stringify + * @param options - Serialization options * @returns The stringified attributes */ -export function htmlAttributes(attributes: Record) { +export function htmlAttributes(attributes: Record, { verbatim = false }: HtmlAttributesOptions = {}) { const parts: string[] = [] for (const [rawKey, value] of Object.entries(attributes)) { - const key = rawKey.startsWith(':') ? rawKey.slice(1) : rawKey + const isBinding = !verbatim && rawKey.startsWith(':') + const key = isBinding ? rawKey.slice(1) : rawKey if (!SAFE_ATTR_NAME.test(key)) continue - if (rawKey.startsWith(':')) { + if (isBinding) { if (value === 'true') { parts.push(key) continue @@ -147,7 +154,7 @@ export function htmlAttributes(attributes: Record) { continue } - if (value === true || value === 'true') { + if (value === true || (!verbatim && value === 'true')) { parts.push(key) continue } diff --git a/packages/comark/test/html-roundtrip.test.ts b/packages/comark/test/html-roundtrip.test.ts index 47dae02c3..0ee400997 100644 --- a/packages/comark/test/html-roundtrip.test.ts +++ b/packages/comark/test/html-roundtrip.test.ts @@ -29,6 +29,8 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => { 'single-child video': '', 'mixed text and element children': '
\nsome text\n
cap
\n
', 'pre with authored language attribute': '
\nputs 1\n
', + 'img with a bound attribute': 'text tail', + 'attribute with a "true" value': '', } for (const [name, md] of Object.entries(cases)) { From 62f69110bddbff5a9d859ad2c3e4569405a8881e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 2 Nov 2026 09:54:06 +0000 Subject: [PATCH 2/3] fix(render): keep non-string bound values on raw HTML --- packages/comark/src/internal/stringify/html-attributes.ts | 6 +++--- packages/comark/test/html-roundtrip.test.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/comark/src/internal/stringify/html-attributes.ts b/packages/comark/src/internal/stringify/html-attributes.ts index 2e1ae4176..be1005d9a 100644 --- a/packages/comark/src/internal/stringify/html-attributes.ts +++ b/packages/comark/src/internal/stringify/html-attributes.ts @@ -137,12 +137,12 @@ interface HtmlAttributesOptions { export function htmlAttributes(attributes: Record, { verbatim = false }: HtmlAttributesOptions = {}) { const parts: string[] = [] for (const [rawKey, value] of Object.entries(attributes)) { - const isBinding = !verbatim && rawKey.startsWith(':') - const key = isBinding ? rawKey.slice(1) : rawKey + const isBinding = rawKey.startsWith(':') + const key = isBinding && !verbatim ? rawKey.slice(1) : rawKey if (!SAFE_ATTR_NAME.test(key)) continue if (isBinding) { - if (value === 'true') { + if (value === 'true' && !verbatim) { parts.push(key) continue } diff --git a/packages/comark/test/html-roundtrip.test.ts b/packages/comark/test/html-roundtrip.test.ts index 0ee400997..b979631e8 100644 --- a/packages/comark/test/html-roundtrip.test.ts +++ b/packages/comark/test/html-roundtrip.test.ts @@ -40,4 +40,12 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => { expect(t2.nodes).toEqual(t1.nodes) }) } + + it('writes a non-string bound attribute value', async () => { + const rendered = await renderMarkdown({ + nodes: [['p', {}, 'text ', ['img', { $: { html: 1, block: 0 }, src: 'a.svg', ':zoom': false }]]], + }) + + expect(rendered.trim()).toBe('text ') + }) }) From 960a477cfe28d2d3e209a4fec7e86ef2ca9bbda3 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 2 Nov 2026 10:13:17 +0000 Subject: [PATCH 3/3] fix(render): keep bare bindings bare on raw HTML --- packages/comark/src/internal/stringify/html-attributes.ts | 2 +- packages/comark/test/html-roundtrip.test.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/comark/src/internal/stringify/html-attributes.ts b/packages/comark/src/internal/stringify/html-attributes.ts index be1005d9a..36dbbfc30 100644 --- a/packages/comark/src/internal/stringify/html-attributes.ts +++ b/packages/comark/src/internal/stringify/html-attributes.ts @@ -142,7 +142,7 @@ export function htmlAttributes(attributes: Record, { verbatim = if (!SAFE_ATTR_NAME.test(key)) continue if (isBinding) { - if (value === 'true' && !verbatim) { + if (verbatim ? value === true : value === 'true') { parts.push(key) continue } diff --git a/packages/comark/test/html-roundtrip.test.ts b/packages/comark/test/html-roundtrip.test.ts index b979631e8..5a0a5aa14 100644 --- a/packages/comark/test/html-roundtrip.test.ts +++ b/packages/comark/test/html-roundtrip.test.ts @@ -30,6 +30,7 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => { 'mixed text and element children': '
\nsome text\n
cap
\n
', 'pre with authored language attribute': '
\nputs 1\n
', 'img with a bound attribute': 'text tail', + 'img with a bare bound attribute': 'text x tail', 'attribute with a "true" value': '', }