Browse Source

Use unescapeMd in links instead of custom impl

pull/14/head
Alex Kocharin 10 years ago
parent
commit
473a40f04f
  1. 37
      lib/links.js
  2. 14
      test/fixtures/remarkable/commonmark_extras.txt

37
lib/links.js

@ -1,6 +1,10 @@
'use strict'; 'use strict';
var unescapeMd = require('./common/utils').unescapeMd;
// //
// Parse link label // Parse link label
// //
@ -60,8 +64,8 @@ function parseLinkLabel(state, start) {
// on failure it returns null // on failure it returns null
function parseLinkDestination(state, pos) { function parseLinkDestination(state, pos) {
var code, level, var code, level,
max = state.posMax, start = pos,
href = ''; max = state.posMax;
if (state.src.charCodeAt(pos) === 0x3C /* < */) { if (state.src.charCodeAt(pos) === 0x3C /* < */) {
pos++; pos++;
@ -70,16 +74,15 @@ function parseLinkDestination(state, pos) {
if (code === 0x0A /* \n */) { return false; } if (code === 0x0A /* \n */) { return false; }
if (code === 0x3E /* > */) { if (code === 0x3E /* > */) {
state.pos = pos + 1; state.pos = pos + 1;
state.linkContent = href; state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true; return true;
} }
if (code === 0x5C /* \ */ && pos + 1 < max) { if (code === 0x5C /* \ */ && pos + 1 < max) {
pos++; pos += 2;
href += state.src[pos++];
continue; continue;
} }
href += state.src[pos++]; pos++;
} }
// no closing '>' // no closing '>'
@ -98,8 +101,7 @@ function parseLinkDestination(state, pos) {
if (code < 0x20 || code === 0x7F) { break; } if (code < 0x20 || code === 0x7F) { break; }
if (code === 0x5C /* \ */ && pos + 1 < max) { if (code === 0x5C /* \ */ && pos + 1 < max) {
pos++; pos += 2;
href += state.src[pos++];
continue; continue;
} }
@ -113,15 +115,15 @@ function parseLinkDestination(state, pos) {
if (level < 0) { break; } 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.pos = pos;
state.linkContent = href;
return true; return true;
} }
@ -131,14 +133,14 @@ function parseLinkDestination(state, pos) {
// on success it returns a string and updates state.pos; // on success it returns a string and updates state.pos;
// on failure it returns null // on failure it returns null
function parseLinkTitle(state, pos) { function parseLinkTitle(state, pos) {
var title, code, var code,
start = pos,
max = state.posMax, max = state.posMax,
marker = state.src.charCodeAt(pos); marker = state.src.charCodeAt(pos);
if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return false; } if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return false; }
pos++; pos++;
title = '';
// if opening marker is "(", switch it to closing marker ")" // if opening marker is "(", switch it to closing marker ")"
if (marker === 0x28) { marker = 0x29; } if (marker === 0x28) { marker = 0x29; }
@ -147,16 +149,15 @@ function parseLinkTitle(state, pos) {
code = state.src.charCodeAt(pos); code = state.src.charCodeAt(pos);
if (code === marker) { if (code === marker) {
state.pos = pos + 1; state.pos = pos + 1;
state.linkContent = title; state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true; return true;
} }
if (code === 0x5C /* \ */ && pos + 1 < max) { if (code === 0x5C /* \ */ && pos + 1 < max) {
pos++; pos += 2;
title += state.src[pos++];
continue; continue;
} }
title += state.src[pos++]; pos++;
} }
return false; return false;

14
test/fixtures/remarkable/commonmark_extras.txt

@ -63,3 +63,17 @@ Issue #55:
<p>![test]</p> <p>![test]</p>
<p>![test](foo bar)</p> <p>![test](foo bar)</p>
. .
Should unescape only needed things in link destinations/titles:
.
[test](<\f\o\o\>\\>)
.
<p><a href="%5Cf%5Co%5Co%3E%5C">test</a></p>
.
.
[test](foo "\\\"\b\a\r")
.
<p><a href="foo" title="\&quot;\b\a\r">test</a></p>
.

Loading…
Cancel
Save