Skip to content

Commit

Permalink
Merge pull request #1414 from UziTech/multi-bracket-link
Browse files Browse the repository at this point in the history
nested parentheses link
  • Loading branch information
UziTech authored Feb 18, 2019
2 parents da67561 + ceee178 commit a908d08
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ inline.tag = edit(inline.tag)
.getRegex();

inline._label = /(?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?/;
inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\]*\)|[^\s\x00-\x1f()\\])*?)/;
inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)/;
inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;

inline.link = edit(inline.link)
Expand Down Expand Up @@ -719,6 +719,12 @@ InlineLexer.prototype.output = function(src) {

// link
if (cap = this.rules.link.exec(src)) {
var lastParenIndex = findClosingBracket(cap[2], '()');
if (lastParenIndex > -1) {
var removeChars = cap[2].length - lastParenIndex;
cap[2] = cap[2].substring(0, lastParenIndex);
cap[0] = cap[0].substring(0, cap[0].length - removeChars);
}
src = src.substring(cap[0].length);
this.inLink = true;
href = cap[2];
Expand Down Expand Up @@ -1503,6 +1509,26 @@ function rtrim(str, c, invert) {
return str.substr(0, str.length - suffLen);
}

function findClosingBracket(str, b) {
if (str.indexOf(b[1]) === -1) {
return -1;
}
var level = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === '\\') {
i++;
} else if (str[i] === b[0]) {
level++;
} else if (str[i] === b[1]) {
level--;
if (level < 0) {
return i;
}
}
}
return -1;
}

/**
* Marked
*/
Expand Down
5 changes: 5 additions & 0 deletions test/new/link_redos.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
<li>伪类:<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:active">:active</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link">:any-link</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:blank">:blank</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:checked">:checked</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:current">:current</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:default">:default</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:defined">:defined</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:dir">:dir()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:disabled">:disabled</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:drop">:drop</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:empty">:empty</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:enabled">:enabled</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:first">:first</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child">:first-child</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type">:first-of-type</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:fullscreen">:fullscreen</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:future">:future</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus">:focus</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible">:focus-visible</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within">:focus-within</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has">:has()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:host">:host</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:host()">:host()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:host-context()">:host-context()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:hover">:hover</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:indeterminate">:indeterminate</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:in-range">:in-range</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid">:invalid</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:is">:is()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:lang">:lang()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child">:last-child</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type">:last-of-type</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:left">:left</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:link">:link</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:local-link">:local-link</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:not">:not()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child">:nth-child()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-col">:nth-col()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child">:nth-last-child()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-col">:nth-last-col()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-of-type">:nth-last-of-type()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type">:nth-of-type()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child">:only-child</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type">:only-of-type</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:optional">:optional</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:out-of-range">:out-of-range</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:past">:past</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown">:placeholder-shown</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:read-only">:read-only</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:read-write">:read-write</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:required">:required</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:right">:right</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:root">:root</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:scope">:scope</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:target">:target</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:target-within">:target-within</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid">:user-invalid</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:valid">:valid</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:visited">:visited</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:where">:where()</a></li>
<li>伪元素:<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::after">::after (:after)</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop">::backdrop</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::before">::before (:before)</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::cue">::cue (:cue)</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter">::first-letter (:first-letter)</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::first-line">::first-line (:first-line)</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::grammar-error">::grammar-error</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::marker">::marker</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder">::placeholder</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::selection">::selection</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted">::slotted()</a><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::spelling-error">::spelling-error</a> </li>
</ul>

2 changes: 2 additions & 0 deletions test/new/link_redos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* 伪类:[:active](https://developer.mozilla.org/en-US/docs/Web/CSS/:active)、[:any-link](https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link)、[:blank](https://developer.mozilla.org/en-US/docs/Web/CSS/:blank)、[:checked](https://developer.mozilla.org/en-US/docs/Web/CSS/:checked)、[:current](https://developer.mozilla.org/en-US/docs/Web/CSS/:current)、[:default](https://developer.mozilla.org/en-US/docs/Web/CSS/:default)、[:defined](https://developer.mozilla.org/en-US/docs/Web/CSS/:defined)、[:dir()](https://developer.mozilla.org/en-US/docs/Web/CSS/:dir)、[:disabled](https://developer.mozilla.org/en-US/docs/Web/CSS/:disabled)、[:drop](https://developer.mozilla.org/en-US/docs/Web/CSS/:drop)、[:empty](https://developer.mozilla.org/en-US/docs/Web/CSS/:empty)、[:enabled](https://developer.mozilla.org/en-US/docs/Web/CSS/:enabled)、[:first](https://developer.mozilla.org/en-US/docs/Web/CSS/:first)、[:first-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child)、[:first-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type)、[:fullscreen](https://developer.mozilla.org/en-US/docs/Web/CSS/:fullscreen)、[:future](https://developer.mozilla.org/en-US/docs/Web/CSS/:future)、[:focus](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus)、[:focus-visible](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible)、[:focus-within](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within)、[:has()](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)、[:host](https://developer.mozilla.org/en-US/docs/Web/CSS/:host)、[:host()](https://developer.mozilla.org/en-US/docs/Web/CSS/:host())、[:host-context()](https://developer.mozilla.org/en-US/docs/Web/CSS/:host-context())、[:hover](https://developer.mozilla.org/en-US/docs/Web/CSS/:hover)、[:indeterminate](https://developer.mozilla.org/en-US/docs/Web/CSS/:indeterminate)、[:in-range](https://developer.mozilla.org/en-US/docs/Web/CSS/:in-range)、[:invalid](https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid)、[:is()](https://developer.mozilla.org/en-US/docs/Web/CSS/:is)、[:lang()](https://developer.mozilla.org/en-US/docs/Web/CSS/:lang)、[:last-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child)、[:last-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type)、[:left](https://developer.mozilla.org/en-US/docs/Web/CSS/:left)、[:link](https://developer.mozilla.org/en-US/docs/Web/CSS/:link)、[:local-link](https://developer.mozilla.org/en-US/docs/Web/CSS/:local-link)、[:not()](https://developer.mozilla.org/en-US/docs/Web/CSS/:not)、[:nth-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child)、[:nth-col()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-col)、[:nth-last-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child)、[:nth-last-col()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-col)、[:nth-last-of-type()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-of-type)、[:nth-of-type()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type)、[:only-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child)、[:only-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type)、[:optional](https://developer.mozilla.org/en-US/docs/Web/CSS/:optional)、[:out-of-range](https://developer.mozilla.org/en-US/docs/Web/CSS/:out-of-range)、[:past](https://developer.mozilla.org/en-US/docs/Web/CSS/:past)、[:placeholder-shown](https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown)、[:read-only](https://developer.mozilla.org/en-US/docs/Web/CSS/:read-only)、[:read-write](https://developer.mozilla.org/en-US/docs/Web/CSS/:read-write)、[:required](https://developer.mozilla.org/en-US/docs/Web/CSS/:required)、[:right](https://developer.mozilla.org/en-US/docs/Web/CSS/:right)、[:root](https://developer.mozilla.org/en-US/docs/Web/CSS/:root)、[:scope](https://developer.mozilla.org/en-US/docs/Web/CSS/:scope)、[:target](https://developer.mozilla.org/en-US/docs/Web/CSS/:target)、[:target-within](https://developer.mozilla.org/en-US/docs/Web/CSS/:target-within)、[:user-invalid](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid)、[:valid](https://developer.mozilla.org/en-US/docs/Web/CSS/:valid)、[:visited](https://developer.mozilla.org/en-US/docs/Web/CSS/:visited)、[:where()](https://developer.mozilla.org/en-US/docs/Web/CSS/:where)
* 伪元素:[::after (:after)](https://developer.mozilla.org/en-US/docs/Web/CSS/::after)[::backdrop](https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop)[::before (:before)](https://developer.mozilla.org/en-US/docs/Web/CSS/::before)[::cue (:cue)](https://developer.mozilla.org/en-US/docs/Web/CSS/::cue)[::first-letter (:first-letter)](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter)[::first-line (:first-line)](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-line)[::grammar-error](https://developer.mozilla.org/en-US/docs/Web/CSS/::grammar-error)[::marker](https://developer.mozilla.org/en-US/docs/Web/CSS/::marker)[::placeholder](https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder)[::selection](https://developer.mozilla.org/en-US/docs/Web/CSS/::selection)[::slotted()](https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted)[::spelling-error](https://developer.mozilla.org/en-US/docs/Web/CSS/::spelling-error)

0 comments on commit a908d08

Please sign in to comment.