-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix: fix link with angle brackets around href #1851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -457,17 +457,33 @@ module.exports = class Tokenizer { | |
| link(src) { | ||
| const cap = this.rules.inline.link.exec(src); | ||
| if (cap) { | ||
| const lastParenIndex = findClosingBracket(cap[2], '()'); | ||
| if (lastParenIndex > -1) { | ||
| const start = cap[0].indexOf('!') === 0 ? 5 : 4; | ||
| const linkLen = start + cap[1].length + lastParenIndex; | ||
| cap[2] = cap[2].substring(0, lastParenIndex); | ||
| cap[0] = cap[0].substring(0, linkLen).trim(); | ||
| cap[3] = ''; | ||
| const trimmedUrl = cap[2].trim(); | ||
| if (!this.options.pedantic && trimmedUrl.startsWith('<')) { | ||
| // commonmark requires matching angle brackets | ||
| if (!trimmedUrl.endsWith('>')) { | ||
| return; | ||
| } | ||
|
|
||
| // ending angle bracket cannot be escaped | ||
| const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\'); | ||
| if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) { | ||
| return; | ||
| } | ||
| } else { | ||
| // find closing parenthesis | ||
| const lastParenIndex = findClosingBracket(cap[2], '()'); | ||
| if (lastParenIndex > -1) { | ||
| const start = cap[0].indexOf('!') === 0 ? 5 : 4; | ||
| const linkLen = start + cap[1].length + lastParenIndex; | ||
| cap[2] = cap[2].substring(0, lastParenIndex); | ||
| cap[0] = cap[0].substring(0, linkLen).trim(); | ||
| cap[3] = ''; | ||
| } | ||
| } | ||
| let href = cap[2]; | ||
| let title = ''; | ||
| if (this.options.pedantic) { | ||
| // split pedantic href and title | ||
| const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); | ||
|
|
||
| if (link) { | ||
|
|
@@ -479,7 +495,16 @@ module.exports = class Tokenizer { | |
| } else { | ||
| title = cap[3] ? cap[3].slice(1, -1) : ''; | ||
| } | ||
| href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); | ||
|
|
||
| href = href.trim(); | ||
| if (href.startsWith('<')) { | ||
| if (this.options.pedantic && !trimmedUrl.endsWith('>')) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are multiple (I think 3?) checks for the pedantic option. Is there some way all of the pedantic logic can be handled together?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would require duplicating a lot of the logic since most of the logic is the same.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my PR on your branch. Broke out my old boolean logic notes and reordered some things around. Not sure if it's "better", but it's more legible to me. |
||
| // pedantic allows starting angle bracket without ending angle bracket | ||
| href = href.slice(1); | ||
| } else { | ||
| href = href.slice(1, -1); | ||
| } | ||
| } | ||
calculuschild marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const token = outputLink(cap, { | ||
| href: href ? href.replace(this.rules.inline._escapes, '$1') : href, | ||
| title: title ? title.replace(this.rules.inline._escapes, '$1') : title | ||
UziTech marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| <p><a href="%3Ctest">URL</a></p> | ||
| <p><a href="test">URL</a></p> | ||
|
|
||
| <p><a href="test%5C">URL</a></p> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,6 @@ | ||
| --- | ||
| pedantic: true | ||
| --- | ||
| [URL](<test) | ||
|
|
||
| [URL](<test\>) |
Uh oh!
There was an error while loading. Please reload this page.