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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Component content
## Markdown HTML

```md
<component title="Hello World" count="42" enabled hidden="false" tags="[&quot;markdown&quot;,&quot;docs&quot;]" config="{&quot;theme&quot;:&quot;dark&quot;,&quot;debug&quot;:false}">
<component title="Hello World" :count="42" enabled :hidden="false" :tags="[&quot;markdown&quot;,&quot;docs&quot;]" :config="{&quot;theme&quot;:&quot;dark&quot;,&quot;debug&quot;:false}">

Component content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ full-width: true
```md
<page-section full-width>

<multi-column columns="3">
<multi-column :columns="3">

<container display="flex">

Expand Down
6 changes: 3 additions & 3 deletions packages/comark/SPEC/COMARK/component-nested4.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,19 @@ timeout:

{{ $doc.snippet.description }}

<button to="$doc.snippet.link" appearance="primary">
<button :to="$doc.snippet.link" appearance="primary">

Button Text

</button>

<button to="$doc.snippet.link" appearance="primary">
<button :to="$doc.snippet.link" appearance="primary">

Button Text

</button>

<button data-testid="$doc.snippet.description" external to="$doc.snippet.link" appearance="primary">
<button :data-testid="$doc.snippet.description" external :to="$doc.snippet.link" appearance="primary">

Button Text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ My button
## Markdown HTML

```md
<my-component block reverse square="false" disabled>
<my-component block reverse :square="false" disabled>

My button

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ My button
## Markdown HTML

```md
<my-component block reverse square="false" disabled>
<my-component block reverse :square="false" disabled>

My button

Expand Down
2 changes: 1 addition & 1 deletion packages/comark/SPEC/COMARK/component-yaml-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Second Paragraph
## Markdown HTML

```md
<component attr="value" object-attr="{&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}" array="[&quot;item 1&quot;,&quot;item 2&quot;]">
<component attr="value" :object-attr="{&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}" :array="[&quot;item 1&quot;,&quot;item 2&quot;]">

First Paragraph

Expand Down
2 changes: 1 addition & 1 deletion packages/comark/SPEC/COMARK/data-binding-frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ site:
name: My Blog
---

<alert title="frontmatter.site.name" type="info">
<alert :title="frontmatter.site.name" type="info">

Hello

Expand Down
2 changes: 1 addition & 1 deletion packages/comark/SPEC/COMARK/data-binding-inline.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ user:
name: Ada
---

Hello <badge label="frontmatter.user.name"></badge>!
Hello <badge :label="frontmatter.user.name"></badge>!
```
2 changes: 1 addition & 1 deletion packages/comark/SPEC/COMARK/data-binding-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
```md
<card title="Hello" variant="primary">

<badge label="props.title" color="props.variant"></badge>
<badge :label="props.title" :color="props.variant"></badge>

</card>
```
2 changes: 1 addition & 1 deletion packages/comark/SPEC/COMARK/data-binding-unresolved.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
## Markdown HTML

```md
<card to="$doc.snippet.link"></card>
<card :to="$doc.snippet.link"></card>
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
## Markdown HTML

```md
<component reverse square="false" disabled></component>
<component reverse :square="false" disabled></component>
```
1 change: 1 addition & 0 deletions packages/comark/src/internal/stringify/handlers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode
? ` ${htmlAttributes(nativeAttributes, {
verbatim: !state.context.html && $.html === 1,
encodeNewlines: !state.context.html,
keepBindings: !state.context.html,
})}`
: ''

Expand Down
10 changes: 7 additions & 3 deletions packages/comark/src/internal/stringify/html-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,16 @@ interface HtmlAttributesOptions {
verbatim?: boolean
/** Write line breaks as `&#10;` / `&#13;`, since a CommonMark HTML block needs its open tag on one line */
encodeNewlines?: boolean
/** Keep the `:` prefix of unresolved bindings, so Markdown output reparses them as bindings */
keepBindings?: boolean
}

const NEWLINE_RE = /[\n\r]/g

/** Serialize an attribute bag into an HTML attribute string. */
export function htmlAttributes(
attributes: Record<string, unknown>,
{ verbatim = false, encodeNewlines = false }: HtmlAttributesOptions = {}
{ verbatim = false, encodeNewlines = false, keepBindings = false }: HtmlAttributesOptions = {}
) {
const escapeValue = (value: string) => {
const escaped = escapeHtml(value)
Expand All @@ -144,15 +146,17 @@ export function htmlAttributes(
if (!SAFE_ATTR_NAME.test(key)) continue

if (isBinding) {
// A bare attribute reads back as `true`, the same value as `:key="true"`.
Comment thread
larbish marked this conversation as resolved.
Resolved
if (verbatim ? value === true : value === 'true') {
parts.push(key)
continue
}
const name = keepBindings ? rawKey : key
if (typeof value === 'object' && value !== null) {
parts.push(`${key}="${escapeValue(JSON.stringify(value))}"`)
parts.push(`${name}="${escapeValue(JSON.stringify(value))}"`)
continue
}
parts.push(`${key}="${escapeValue(String(value))}"`)
parts.push(`${name}="${escapeValue(String(value))}"`)
continue
}

Expand Down
13 changes: 13 additions & 0 deletions packages/comark/test/render-markdown-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ describe('render: markdown/html', () => {
expect(types).toContain('bullet_list_open')
})

it('keeps binding props as bindings', async () => {
const source = '::card{title="Hello"}\n:::badge{:label="props.title" :count="5"}\n:::\n::'
const output = await render(await parseMarkdown(source), { format: 'markdown/html' })
const [[, , badge]] = (await parseMarkdown(output)).nodes as any
expect(badge[1]).toMatchObject({ ':label': 'props.title', ':count': '5' })

const tokens = (await reparse(source)).flatMap((token) => token.children ?? [])
expect(tokens.find((token) => token.content.startsWith('<badge'))).toMatchObject({
type: 'html_inline',
content: '<badge :label="props.title" :count="5">',
})
})

it('keeps a code block holding indented fences whole', async () => {
const code = '::tabs\n :::tabs-item\n ```mdc\n ::callout\n ::\n ```\n :::\n\n::'
const tokens = await reparse(`::code-preview\n#code\n\`\`\`\`mdc\n${code}\n\`\`\`\`\n::\n\nAfter`)
Expand Down
Loading