Skip to content

Commit c54d30d

Browse files
authored
Brackets in link text (#551)
* Add brackets in link text tests * Update LINK_R to handle link texts with brackets inside Supports links like [[this]](https://example.com), which is perfectly valid markdown by most parsers.
1 parent b5f4701 commit c54d30d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

index.compiler.spec.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,3 +4067,26 @@ it('handles a holistic example', () => {
40674067

40684068
expect(root.innerHTML).toMatchSnapshot()
40694069
})
4070+
4071+
4072+
it('handles <code> brackets in link text', () => {
4073+
render(compiler('[`[text]`](https://example.com)'))
4074+
4075+
expect(root.innerHTML).toMatchInlineSnapshot(`
4076+
<a href="https://example.com">
4077+
<code>
4078+
[text]
4079+
</code>
4080+
</a>
4081+
`)
4082+
})
4083+
4084+
it('handles naked brackets in link text', () => {
4085+
render(compiler('[[text]](https://example.com)'))
4086+
4087+
expect(root.innerHTML).toMatchInlineSnapshot(`
4088+
<a href="https://example.com">
4089+
[text]
4090+
</a>
4091+
`)
4092+
})

index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,13 @@ function generateListRule(
482482
}
483483
}
484484

485-
const LINK_R = /^\[([^\]]*)]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/
486-
const IMAGE_R = /^!\[([^\]]*)]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/
485+
const LINK_INSIDE = "(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*";
486+
const LINK_HREF_AND_TITLE =
487+
"\\s*<?((?:\\([^)]*\\)|[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*";
488+
const LINK_R = new RegExp(
489+
"^\\[(" + LINK_INSIDE + ")\\]\\(" + LINK_HREF_AND_TITLE + "\\)",
490+
)
491+
const IMAGE_R = /^!\[(.*?)\]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/
487492

488493
const NON_PARAGRAPH_BLOCK_SYNTAXES = [
489494
BLOCKQUOTE_R,

0 commit comments

Comments
 (0)