From 1ff69abebac8a43654d03d8301acf4afbad9287f Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 11 Dec 2014 01:53:14 +0300 Subject: [PATCH] Fix arbitrary keys in refs/footnotes/abbrs --- lib/rules_block/footnote.js | 4 +- lib/rules_core/abbr.js | 4 +- lib/rules_core/abbr2.js | 6 ++- lib/rules_core/footnote_tail.js | 6 +-- lib/rules_core/references.js | 4 +- lib/rules_inline/footnote_ref.js | 8 ++-- lib/rules_inline/links.js | 2 +- test/fixtures/remarkable/proto.txt | 62 ++++++++++++++++++++++++++++++ 8 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 test/fixtures/remarkable/proto.txt diff --git a/lib/rules_block/footnote.js b/lib/rules_block/footnote.js index fe410ba..7c8c5e4 100644 --- a/lib/rules_block/footnote.js +++ b/lib/rules_block/footnote.js @@ -28,9 +28,9 @@ module.exports = function footnote(state, startLine, endLine, silent) { pos++; if (!state.env.footnotes) { state.env.footnotes = {}; } - if (!state.env.footnotes.refs) { state.env.footnotes.refs = Object.create(null); } + if (!state.env.footnotes.refs) { state.env.footnotes.refs = {}; } label = state.src.slice(start + 2, pos - 2); - state.env.footnotes.refs[label] = -1; + state.env.footnotes.refs[':' + label] = -1; state.tokens.push({ type: 'footnote_reference_open', diff --git a/lib/rules_core/abbr.js b/lib/rules_core/abbr.js index 838c184..235d9f9 100644 --- a/lib/rules_core/abbr.js +++ b/lib/rules_core/abbr.js @@ -32,7 +32,9 @@ function parseAbbr(str, parserInline, options, env) { title = str.slice(labelEnd + 2, pos).trim(); if (title.length === 0) { return -1; } if (!env.abbreviations) { env.abbreviations = {}; } - env.abbreviations[label] = env.abbreviations[label] || title; + if (env.abbreviations[':' + label] === undefined) { + env.abbreviations[':' + label] = title; + } return pos; } diff --git a/lib/rules_core/abbr2.js b/lib/rules_core/abbr2.js index ec76f38..cdc03ce 100644 --- a/lib/rules_core/abbr2.js +++ b/lib/rules_core/abbr2.js @@ -20,7 +20,9 @@ module.exports = function abbr2(state) { if (!state.env.abbreviations) { return; } if (!state.env.abbrRegExp) { regText = '(^|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])' - + '(' + Object.keys(state.env.abbreviations).sort(function (a, b) { + + '(' + Object.keys(state.env.abbreviations).map(function (x) { + return x.substr(1); + }).sort(function (a, b) { return b.length - a.length; }).map(regEscape).join('|') + ')' + '($|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])'; @@ -54,7 +56,7 @@ module.exports = function abbr2(state) { nodes.push({ type: 'abbr_open', - title: state.env.abbreviations[m[2]], + title: state.env.abbreviations[':' + m[2]], level: level++ }); nodes.push({ diff --git a/lib/rules_core/footnote_tail.js b/lib/rules_core/footnote_tail.js index c3064f3..2be3445 100644 --- a/lib/rules_core/footnote_tail.js +++ b/lib/rules_core/footnote_tail.js @@ -5,7 +5,7 @@ module.exports = function footnote_block(state) { var i, l, j, t, lastParagraph, list, tokens, current, currentLabel, level = 0, insideRef = false, - refTokens = Object.create(null); + refTokens = {}; if (!state.env.footnotes) { return; } @@ -18,7 +18,7 @@ module.exports = function footnote_block(state) { } if (tok.type === 'footnote_reference_close') { insideRef = false; - refTokens[currentLabel] = current; + refTokens[':' + currentLabel] = current; return false; } if (insideRef) { current.push(tok); } @@ -58,7 +58,7 @@ module.exports = function footnote_block(state) { level: --level }); } else if (list[i].label) { - tokens = refTokens[list[i].label]; + tokens = refTokens[':' + list[i].label]; } state.tokens = state.tokens.concat(tokens); diff --git a/lib/rules_core/references.js b/lib/rules_core/references.js index e393892..df76185 100644 --- a/lib/rules_core/references.js +++ b/lib/rules_core/references.js @@ -58,7 +58,9 @@ function parseReference(str, parser, options, env) { if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return -1; } label = normalizeReference(str.slice(1, labelEnd)); - env.references[label] = env.references[label] || { title: title, href: href }; + if (env.references[':' + label] === undefined) { + env.references[':' + label] = { title: title, href: href }; + } return pos; } diff --git a/lib/rules_inline/footnote_ref.js b/lib/rules_inline/footnote_ref.js index 12816a4..e79f9d5 100644 --- a/lib/rules_inline/footnote_ref.js +++ b/lib/rules_inline/footnote_ref.js @@ -32,17 +32,17 @@ module.exports = function footnote_ref(state, silent) { pos++; label = state.src.slice(start + 2, pos - 1); - if (state.env.footnotes.refs[label] === undefined) { return false; } + if (state.env.footnotes.refs[':' + label] === undefined) { return false; } if (!silent) { if (!state.env.footnotes.list) { state.env.footnotes.list = []; } - if (state.env.footnotes.refs[label] < 0) { + if (state.env.footnotes.refs[':' + label] < 0) { footnoteId = state.env.footnotes.list.length; state.env.footnotes.list[footnoteId] = { label: label, count: 0 }; - state.env.footnotes.refs[label] = footnoteId; + state.env.footnotes.refs[':' + label] = footnoteId; } else { - footnoteId = state.env.footnotes.refs[label]; + footnoteId = state.env.footnotes.refs[':' + label]; } footnoteSubId = state.env.footnotes.list[footnoteId].count; diff --git a/lib/rules_inline/links.js b/lib/rules_inline/links.js index eef45b9..fd2ce11 100644 --- a/lib/rules_inline/links.js +++ b/lib/rules_inline/links.js @@ -120,7 +120,7 @@ module.exports = function links(state, silent) { // (collapsed reference link and shortcut reference link respectively) if (!label) { label = state.src.slice(labelStart, labelEnd); } - ref = state.env.references[normalizeReference(label)]; + ref = state.env.references[':' + normalizeReference(label)]; if (!ref) { state.pos = oldPos; return false; diff --git a/test/fixtures/remarkable/proto.txt b/test/fixtures/remarkable/proto.txt new file mode 100644 index 0000000..cad943a --- /dev/null +++ b/test/fixtures/remarkable/proto.txt @@ -0,0 +1,62 @@ + +. +[__proto__] + +[__proto__]: blah +. +

proto

+. + +. +[^__proto__] + +[^__proto__]: blah +. +

[1]

+
+
+
    +
  1. blah

    +
  2. +
+
+. + +. +*[__proto__]: blah + +__proto__ \_\_proto\_\_ +. +

proto __proto__

+. + +. +[hasOwnProperty] + +[hasOwnProperty]: blah +. +

hasOwnProperty

+. + +. +[^hasOwnProperty] + +[^hasOwnProperty]: blah +. +

[1]

+
+
+
    +
  1. blah

    +
  2. +
+
+. + +. +*[hasOwnProperty]: blah + +hasOwnProperty +. +

hasOwnProperty

+.