Browse Source

Fix arbitrary keys in refs/footnotes/abbrs

pull/14/head
Alex Kocharin 10 years ago
parent
commit
1ff69abeba
  1. 4
      lib/rules_block/footnote.js
  2. 4
      lib/rules_core/abbr.js
  3. 6
      lib/rules_core/abbr2.js
  4. 6
      lib/rules_core/footnote_tail.js
  5. 4
      lib/rules_core/references.js
  6. 8
      lib/rules_inline/footnote_ref.js
  7. 2
      lib/rules_inline/links.js
  8. 62
      test/fixtures/remarkable/proto.txt

4
lib/rules_block/footnote.js

@ -28,9 +28,9 @@ module.exports = function footnote(state, startLine, endLine, silent) {
pos++; pos++;
if (!state.env.footnotes) { state.env.footnotes = {}; } 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); label = state.src.slice(start + 2, pos - 2);
state.env.footnotes.refs[label] = -1; state.env.footnotes.refs[':' + label] = -1;
state.tokens.push({ state.tokens.push({
type: 'footnote_reference_open', type: 'footnote_reference_open',

4
lib/rules_core/abbr.js

@ -32,7 +32,9 @@ function parseAbbr(str, parserInline, options, env) {
title = str.slice(labelEnd + 2, pos).trim(); title = str.slice(labelEnd + 2, pos).trim();
if (title.length === 0) { return -1; } if (title.length === 0) { return -1; }
if (!env.abbreviations) { env.abbreviations = {}; } if (!env.abbreviations) { env.abbreviations = {}; }
env.abbreviations[label] = env.abbreviations[label] || title; if (env.abbreviations[':' + label] === undefined) {
env.abbreviations[':' + label] = title;
}
return pos; return pos;
} }

6
lib/rules_core/abbr2.js

@ -20,7 +20,9 @@ module.exports = function abbr2(state) {
if (!state.env.abbreviations) { return; } if (!state.env.abbreviations) { return; }
if (!state.env.abbrRegExp) { if (!state.env.abbrRegExp) {
regText = '(^|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])' 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; return b.length - a.length;
}).map(regEscape).join('|') + ')' }).map(regEscape).join('|') + ')'
+ '($|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])'; + '($|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])';
@ -54,7 +56,7 @@ module.exports = function abbr2(state) {
nodes.push({ nodes.push({
type: 'abbr_open', type: 'abbr_open',
title: state.env.abbreviations[m[2]], title: state.env.abbreviations[':' + m[2]],
level: level++ level: level++
}); });
nodes.push({ nodes.push({

6
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, var i, l, j, t, lastParagraph, list, tokens, current, currentLabel,
level = 0, level = 0,
insideRef = false, insideRef = false,
refTokens = Object.create(null); refTokens = {};
if (!state.env.footnotes) { return; } if (!state.env.footnotes) { return; }
@ -18,7 +18,7 @@ module.exports = function footnote_block(state) {
} }
if (tok.type === 'footnote_reference_close') { if (tok.type === 'footnote_reference_close') {
insideRef = false; insideRef = false;
refTokens[currentLabel] = current; refTokens[':' + currentLabel] = current;
return false; return false;
} }
if (insideRef) { current.push(tok); } if (insideRef) { current.push(tok); }
@ -58,7 +58,7 @@ module.exports = function footnote_block(state) {
level: --level level: --level
}); });
} else if (list[i].label) { } else if (list[i].label) {
tokens = refTokens[list[i].label]; tokens = refTokens[':' + list[i].label];
} }
state.tokens = state.tokens.concat(tokens); state.tokens = state.tokens.concat(tokens);

4
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; } if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return -1; }
label = normalizeReference(str.slice(1, labelEnd)); 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; return pos;
} }

8
lib/rules_inline/footnote_ref.js

@ -32,17 +32,17 @@ module.exports = function footnote_ref(state, silent) {
pos++; pos++;
label = state.src.slice(start + 2, pos - 1); 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 (!silent) {
if (!state.env.footnotes.list) { state.env.footnotes.list = []; } 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; footnoteId = state.env.footnotes.list.length;
state.env.footnotes.list[footnoteId] = { label: label, count: 0 }; state.env.footnotes.list[footnoteId] = { label: label, count: 0 };
state.env.footnotes.refs[label] = footnoteId; state.env.footnotes.refs[':' + label] = footnoteId;
} else { } else {
footnoteId = state.env.footnotes.refs[label]; footnoteId = state.env.footnotes.refs[':' + label];
} }
footnoteSubId = state.env.footnotes.list[footnoteId].count; footnoteSubId = state.env.footnotes.list[footnoteId].count;

2
lib/rules_inline/links.js

@ -120,7 +120,7 @@ module.exports = function links(state, silent) {
// (collapsed reference link and shortcut reference link respectively) // (collapsed reference link and shortcut reference link respectively)
if (!label) { label = state.src.slice(labelStart, labelEnd); } if (!label) { label = state.src.slice(labelStart, labelEnd); }
ref = state.env.references[normalizeReference(label)]; ref = state.env.references[':' + normalizeReference(label)];
if (!ref) { if (!ref) {
state.pos = oldPos; state.pos = oldPos;
return false; return false;

62
test/fixtures/remarkable/proto.txt

@ -0,0 +1,62 @@
.
[__proto__]
[__proto__]: blah
.
<p><a href="blah"><strong>proto</strong></a></p>
.
.
[^__proto__]
[^__proto__]: blah
.
<p><sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup></p>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>blah <a href="#fnref1" class="footnote-backref">↩</a></p>
</li>
</ol>
</section>
.
.
*[__proto__]: blah
__proto__ \_\_proto\_\_
.
<p><strong>proto</strong> <abbr title="blah">__proto__</abbr></p>
.
.
[hasOwnProperty]
[hasOwnProperty]: blah
.
<p><a href="blah">hasOwnProperty</a></p>
.
.
[^hasOwnProperty]
[^hasOwnProperty]: blah
.
<p><sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup></p>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>blah <a href="#fnref1" class="footnote-backref">↩</a></p>
</li>
</ol>
</section>
.
.
*[hasOwnProperty]: blah
hasOwnProperty
.
<p><abbr title="blah">hasOwnProperty</abbr></p>
.
Loading…
Cancel
Save