diff --git a/lib/links.js b/lib/links.js index 7158e40..04111f2 100644 --- a/lib/links.js +++ b/lib/links.js @@ -1,6 +1,10 @@ 'use strict'; + +var unescapeMd = require('./common/utils').unescapeMd; + + // // Parse link label // @@ -60,8 +64,8 @@ function parseLinkLabel(state, start) { // on failure it returns null function parseLinkDestination(state, pos) { var code, level, - max = state.posMax, - href = ''; + start = pos, + max = state.posMax; if (state.src.charCodeAt(pos) === 0x3C /* < */) { pos++; @@ -70,16 +74,15 @@ function parseLinkDestination(state, pos) { if (code === 0x0A /* \n */) { return false; } if (code === 0x3E /* > */) { state.pos = pos + 1; - state.linkContent = href; + state.linkContent = unescapeMd(state.src.slice(start + 1, pos)); return true; } if (code === 0x5C /* \ */ && pos + 1 < max) { - pos++; - href += state.src[pos++]; + pos += 2; continue; } - href += state.src[pos++]; + pos++; } // no closing '>' @@ -98,8 +101,7 @@ function parseLinkDestination(state, pos) { if (code < 0x20 || code === 0x7F) { break; } if (code === 0x5C /* \ */ && pos + 1 < max) { - pos++; - href += state.src[pos++]; + pos += 2; continue; } @@ -113,15 +115,15 @@ function parseLinkDestination(state, pos) { if (level < 0) { break; } } - href += state.src[pos++]; + pos++; } - if (!href.length) { return false; } + if (start === pos) { return false; } - if (!state.parser.validateLink(href)) { return false; } + state.linkContent = unescapeMd(state.src.slice(start, pos)); + if (!state.parser.validateLink(state.linkContent)) { return false; } state.pos = pos; - state.linkContent = href; return true; } @@ -131,14 +133,14 @@ function parseLinkDestination(state, pos) { // on success it returns a string and updates state.pos; // on failure it returns null function parseLinkTitle(state, pos) { - var title, code, + var code, + start = pos, max = state.posMax, marker = state.src.charCodeAt(pos); if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return false; } pos++; - title = ''; // if opening marker is "(", switch it to closing marker ")" if (marker === 0x28) { marker = 0x29; } @@ -147,16 +149,15 @@ function parseLinkTitle(state, pos) { code = state.src.charCodeAt(pos); if (code === marker) { state.pos = pos + 1; - state.linkContent = title; + state.linkContent = unescapeMd(state.src.slice(start + 1, pos)); return true; } if (code === 0x5C /* \ */ && pos + 1 < max) { - pos++; - title += state.src[pos++]; + pos += 2; continue; } - title += state.src[pos++]; + pos++; } return false; diff --git a/test/fixtures/remarkable/commonmark_extras.txt b/test/fixtures/remarkable/commonmark_extras.txt index 89624b6..314e9a7 100644 --- a/test/fixtures/remarkable/commonmark_extras.txt +++ b/test/fixtures/remarkable/commonmark_extras.txt @@ -63,3 +63,17 @@ Issue #55:

![test]

![test](foo bar)

. + +Should unescape only needed things in link destinations/titles: + +. +[test](<\f\o\o\>\\>) +. +

test

+. + +. +[test](foo "\\\"\b\a\r") +. +

test

+.