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
76 changes: 76 additions & 0 deletions packages/comark/SPEC/COMARK/link-with-span.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Input

```md
[[1]{} Document](#)

[[link-name]{.cls} more](https://example.com)
```

## AST

```json

{
"frontmatter":{

},
"meta":{

},
"nodes":[
[
"p",
{

},
[
"a",
{
"href":"#"
},
[
"span",
{},
"1"
],
" Document"
]
],
[
"p",
{

},
[
"a",
{
"href":"https://example.com"
},
[
"span",
{
"class": "cls"
},
"link-name"
],
" more"
]
]
]
}
```

## HTML

```html
<p><a href="#"><span>1</span> Document</a></p>
<p><a href="https://example.com"><span class="cls">link-name</span> more</a></p>
```

## Markdown

```md
[[1]{} Document](#)

[[link-name]{.cls} more](https://example.com)
```
82 changes: 82 additions & 0 deletions packages/comark/SPEC/common-mark/links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
## Input

```md
[Document](#)

[[1] Document](#)

[[link-name] more](https://example.com)
```

## AST

```json

{
"frontmatter":{

},
"meta":{

},
"nodes":[
[
"p",
{

},
[
"a",
{
"href":"#"
},
"Document"
]
],
[
"p",
{

},
[
"a",
{
"href":"#"
},
"[1] Document"
]
],
[
"p",
{

},
[
"a",
{
"href":"https://example.com"
},
"[link-name] more"
]
]
]
}
```

## HTML

```html
<p><a href="#">Document</a></p>
<p><a href="#">[1] Document</a></p>
<p><a href="https://example.com">[link-name] more</a></p>
```

## Markdown

```md
[Document](#)

[\[1\] Document](#)

[\[link-name\] more](https://example.com)
```
7 changes: 6 additions & 1 deletion packages/comark/src/internal/stringify/handlers/mdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ export async function mdc(node: ComarkElement, state: State, parent?: ComarkElem
}
content = content.trimEnd()

const attrs = attributeEntries.length > 0 ? comarkAttributes(attributes) : ''
let attrs = attributeEntries.length > 0 ? comarkAttributes(attributes) : ''

if (tag === 'span') {
if (!attrs && parent?.[0] == 'a') {
// Add empty attributes syntax to end of spans inside Anchor links
// This will force Comark to parse them as span and prevent conflict
attrs ||= '{}'
}
return `[${content}]${attrs}` + (inline ? '' : state.context.blockSeparator)
}

Expand Down
7 changes: 6 additions & 1 deletion packages/comark/src/plugins/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,12 @@ const markdownItInlineSpan: PluginSimple = (md) => {
const nextChar = state.src[index + 1]
if (nextChar === '(' || nextChar === '[') return false

if (silent) return true
// Inside a link label, bare `[text]` stays literal (plain-markdown behavior,
// e.g. `[[1] Document](#)`); only an explicit `[text]{attrs}` span matches
if (state.linkLevel > 0 && nextChar !== '{') return false

// Returning `false` lets `parseLinkLabel`'s own depth tracking consume nested brackets and the outer link parse
if (silent) return false

state.push('mdc_inline_span', 'span', 1)

Expand Down
Loading