From eb95a718a4d8275be895dbc0b866b10dda8f62da Mon Sep 17 00:00:00 2001 From: Federico Soave Date: Tue, 6 Mar 2018 02:34:06 +0100 Subject: [PATCH] [commonmark] BREAKING CHANGE: link nesting is not allowed. If multiple links are nested, the innermost one is used. --- lib/marked.js | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index 373d260700..24c634bc38 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -497,7 +497,7 @@ var inline = { + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. + '|^' // declaration, e.g. + '|^', // CDATA section - link: /^!?\[(label)\]\(href(?:\s+(title))?\s*\)/, + link: /^!?\[(label)\]\(href(title)?\s*\)/, reflink: /^!?\[(label)\]\s*\[([^\]]*)\]/, nolink: /^!?\[((?:\[[^\]]*\]|\\[\[\]]|[^\[\]])*)\]/, strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/, @@ -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) @@ -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.'); @@ -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 @@ -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; } @@ -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; }; /**