diff --git a/packages/comark/src/internal/stringify/attributes.ts b/packages/comark/src/internal/stringify/attributes.ts index 75881c53..c3b811c0 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 72bd075f..0a7dc61a 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 9e9e8a4e..36dbbfc3 100644 --- a/packages/comark/src/internal/stringify/html-attributes.ts +++ b/packages/comark/src/internal/stringify/html-attributes.ts @@ -122,20 +122,27 @@ 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 = rawKey.startsWith(':') + const key = isBinding && !verbatim ? rawKey.slice(1) : rawKey if (!SAFE_ATTR_NAME.test(key)) continue - if (rawKey.startsWith(':')) { - if (value === 'true') { + if (isBinding) { + if (verbatim ? value === true : 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 47dae02c..5a0a5aa1 100644 --- a/packages/comark/test/html-roundtrip.test.ts +++ b/packages/comark/test/html-roundtrip.test.ts @@ -29,6 +29,9 @@ 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', + 'img with a bare bound attribute': 'text x tail', + 'attribute with a "true" value': '', } for (const [name, md] of Object.entries(cases)) { @@ -38,4 +41,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 ') + }) })