Skip to content

Commit

Permalink
Brackets in link text (#551)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
zegl authored Mar 21, 2024
1 parent b5f4701 commit c54d30d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4067,3 +4067,26 @@ it('handles a holistic example', () => {

expect(root.innerHTML).toMatchSnapshot()
})


it('handles <code> brackets in link text', () => {
render(compiler('[`[text]`](https://example.com)'))

expect(root.innerHTML).toMatchInlineSnapshot(`
<a href="https://example.com">
<code>
[text]
</code>
</a>
`)
})

it('handles naked brackets in link text', () => {
render(compiler('[[text]](https://example.com)'))

expect(root.innerHTML).toMatchInlineSnapshot(`
<a href="https://example.com">
[text]
</a>
`)
})
9 changes: 7 additions & 2 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,13 @@ function generateListRule(
}
}

const LINK_R = /^\[([^\]]*)]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/
const IMAGE_R = /^!\[([^\]]*)]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/
const LINK_INSIDE = "(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*";
const LINK_HREF_AND_TITLE =
"\\s*<?((?:\\([^)]*\\)|[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*";
const LINK_R = new RegExp(
"^\\[(" + LINK_INSIDE + ")\\]\\(" + LINK_HREF_AND_TITLE + "\\)",
)
const IMAGE_R = /^!\[(.*?)\]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/

const NON_PARAGRAPH_BLOCK_SYNTAXES = [
BLOCKQUOTE_R,
Expand Down

0 comments on commit c54d30d

Please sign in to comment.