Skip to content

Commit

Permalink
allow backslash-escapes in link text, href, title and link definition…
Browse files Browse the repository at this point in the history
… references.

InlineLexer.escapes() handles markdown escapes sequences.
TODO handle escapes everywhere in the document.
  • Loading branch information
Feder1co5oave committed Mar 5, 2018
1 parent e66f7aa commit fc17a2c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Lexer.prototype.token = function(src, top) {
if (top && (cap = this.rules.def.exec(src))) {
src = src.substring(cap[0].length);
if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
tag = cap[1].toLowerCase();
tag = InlineLexer.escapes(cap[1].toLowerCase());
if (!this.tokens.links[tag]) {
this.tokens.links[tag] = {
href: cap[2],
Expand Down Expand Up @@ -508,9 +508,11 @@ var inline = {
text: /^[\s\S]+?(?=[\\<!\[`*]|\b_| {2,}\n|$)/
};


inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;

inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
inline._email = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/;

inline.autolink = edit(inline.autolink)
.replace('scheme', inline._scheme)
.replace('email', inline._email)
Expand All @@ -524,7 +526,7 @@ inline.tag = edit(inline.tag)
.getRegex();

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

inline.link = edit(inline.link)
.replace('inside', inline._inside)
Expand Down Expand Up @@ -693,8 +695,8 @@ InlineLexer.prototype.output = function(src) {
src = src.substring(cap[0].length);
this.inLink = true;
out += this.outputLink(cap, {
href: cap[2],
title: cap[3]
href: InlineLexer.escapes(cap[2]),
title: InlineLexer.escapes(cap[3])
});
this.inLink = false;
continue;
Expand All @@ -704,7 +706,7 @@ InlineLexer.prototype.output = function(src) {
if ((cap = this.rules.reflink.exec(src))
|| (cap = this.rules.nolink.exec(src))) {
src = src.substring(cap[0].length);
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = InlineLexer.escapes(cap[2] || cap[1]).replace(/\s+/g, ' ');
link = this.links[link.toLowerCase()];
if (!link || !link.href) {
out += cap[0].charAt(0);
Expand Down Expand Up @@ -767,6 +769,10 @@ InlineLexer.prototype.output = function(src) {
return out;
};

InlineLexer.escapes = function(text) {
return text ? text.replace(InlineLexer.rules._escapes, '$1') : text;
}

/**
* Compile Link
*/
Expand Down

0 comments on commit fc17a2c

Please sign in to comment.