Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/comark/src/internal/stringify/attributes.ts
Original file line number Diff line number Diff line change
@@ -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'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped this as it didn't seem to be used anywhere else, but it's not strictly relevant to the PR

export type { ResolveAttributesOptions } from './html-attributes.ts'

// Implicit attributes the parser injects per tag — they're conveyed by the
Expand Down
5 changes: 4 additions & 1 deletion packages/comark/src/internal/stringify/handlers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '')
Expand Down
17 changes: 12 additions & 5 deletions packages/comark/src/internal/stringify/html-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,27 @@ export function resolveAttribute(attrs: Record<string, unknown>, 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<string, unknown>) {
export function htmlAttributes(attributes: Record<string, unknown>, { 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(':')
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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
}
Expand All @@ -147,7 +154,7 @@ export function htmlAttributes(attributes: Record<string, unknown>) {
continue
}

if (value === true || value === 'true') {
if (value === true || (!verbatim && value === 'true')) {
parts.push(key)
continue
}
Expand Down
11 changes: 11 additions & 0 deletions packages/comark/test/html-roundtrip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => {
'single-child video': '<video controls>\n<source src="a.mp4" type="video/mp4">\n</video>',
'mixed text and element children': '<div>\nsome text\n<figcaption>cap</figcaption>\n</div>',
'pre with authored language attribute': '<pre language="ruby">\nputs 1\n</pre>',
'img with a bound attribute': 'text <img src="a.svg" class="h-5" :zoom="false"> tail',
'img with a bare bound attribute': 'text <img :src alt="x"> tail',
'attribute with a "true" value': '<div aria-hidden="true">\nicon\n</div>',
}

for (const [name, md] of Object.entries(cases)) {
Expand All @@ -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 <img src="a.svg" :zoom="false">')
})
})
Loading