Skip to content

Commit

Permalink
[commonmark] BREAKING CHANGE: link nesting is not allowed. If multipl…
Browse files Browse the repository at this point in the history
…e links are nested, the innermost one is used.
  • Loading branch information
Feder1co5oave committed Mar 6, 2018
1 parent 5125739 commit eb95a71
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ var inline = {
+ '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
+ '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
+ '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', // CDATA section
link: /^!?\[(label)\]\(href(?:\s+(title))?\s*\)/,
link: /^!?\[(label)\]\(href(title)?\s*\)/,
reflink: /^!?\[(label)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|\\[\[\]]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
Expand Down Expand Up @@ -526,7 +526,7 @@ inline.tag = edit(inline.tag)

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

inline.link = edit(inline.link)
.replace('label', inline._label)
Expand Down Expand Up @@ -592,6 +592,8 @@ function InlineLexer(links, options) {
this.rules = inline.normal;
this.renderer = this.options.renderer || new Renderer();
this.renderer.options = this.options;
this.inLink = false;
this.nestedLink = false;

if (!this.links) {
throw new Error('Tokens array requires a `links` property.');
Expand Down Expand Up @@ -695,16 +697,15 @@ InlineLexer.prototype.output = function(src) {
// link
if (cap = this.rules.link.exec(src)) {
src = src.substring(cap[0].length);
this.inLink = true;
href = cap[2];
href = href[0] === '<' ? href.substring(1, href.length - 1) : href;
title = cap[3] ? cap[3].substring(1, cap[3].length - 1) : cap[3];
href = InlineLexer.escapes(cap[2]);
href = href.charAt(0) === '<' ? href.substring(1, href.length - 1) : href;
title = cap[3] ? cap[3].trim() : '';
title = InlineLexer.escapes(title.substring(1, title.length - 1));

out += this.outputLink(cap, {
href: InlineLexer.escapes(href),
title: InlineLexer.escapes(title)
href: href,
title: title
});
this.inLink = false;
continue;
}

// reflink, nolink
Expand All @@ -718,9 +719,7 @@ InlineLexer.prototype.output = function(src) {
src = cap[0].substring(1) + src;
continue;
}
this.inLink = true;
out += this.outputLink(cap, link);
this.inLink = false;
continue;
}

Expand Down Expand Up @@ -784,11 +783,27 @@ InlineLexer.escapes = function(text) {

InlineLexer.prototype.outputLink = function(cap, link) {
var href = link.href,
title = link.title ? escape(link.title) : null;
title = link.title ? escape(link.title) : null,
alt = escape(InlineLexer.escapes(cap[1])),
wasInLink = this.inLink,
text,
out;

if (cap[0].charAt(0) === '!') { // image
return this.renderer.image(href, title, alt);
}

return cap[0].charAt(0) !== '!'
? this.renderer.link(href, title, this.output(cap[1]))
: this.renderer.image(href, title, escape(cap[1]));
this.inLink = true;
this.nestedLink = false;
text = this.output(cap[1]);
if (this.nestedLink) {
out = '[' + text + cap[0].substring(1 + cap[1].length);
} else {
out = this.renderer.link(link.href, link.title, text);
}
this.inLink = wasInLink;
this.nestedLink = wasInLink;
return out;
};

/**
Expand Down

0 comments on commit eb95a71

Please sign in to comment.