Browse Source

Add collapsed and full reference links

pull/14/head
Alex Kocharin 10 years ago
parent
commit
7432a6827c
  1. 2
      lib/lexer_inline.js
  2. 27
      lib/lexer_inline/links.js

2
lib/lexer_inline.js

@ -212,7 +212,7 @@ LexerInline.prototype.parse_reference = function (str, options) {
if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return null; } if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return null; }
return { return {
label: str.slice(1, labelEnd).trim().replace(/\s+/g, ' '), label: links.normalizeReference(str.slice(1, labelEnd)),
title: title, title: title,
href: href, href: href,
remaining: str.slice(pos) remaining: str.slice(pos)

27
lib/lexer_inline/links.js

@ -164,13 +164,19 @@ function parseLinkTitle(state, pos) {
return null; return null;
} }
function normalizeReference(str) {
return str.trim().replace(/\s+/g, ' ').toLowerCase();
}
function links(state) { function links(state) {
var labelStart, var labelStart,
labelEnd, labelEnd,
label,
href, href,
title, title,
pos, pos,
ref, ref,
code,
isImage = false, isImage = false,
max = state.posMax, max = state.posMax,
start = state.pos, start = state.pos,
@ -235,7 +241,25 @@ function links(state) {
// //
// Link reference // Link reference
// //
ref = state.env.references[state.src.slice(labelStart, labelEnd).trim().replace(/\s+/g, ' ')];
// [foo] [bar]
// ^^ optional whitespace (can include newlines)
for (; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
start = pos + 1;
pos = parseLinkLabel(state, pos);
label = state.src.slice(start, pos);
}
// covers label === '' and label === undefined
// (collapsed reference link and shortcut reference link respectively)
if (!label) { label = state.src.slice(labelStart, labelEnd); }
ref = state.env.references[normalizeReference(label)];
if (!ref) { return false; } if (!ref) { return false; }
href = ref.href; href = ref.href;
title = ref.title; title = ref.title;
@ -269,3 +293,4 @@ module.exports = links;
module.exports.parseLinkLabel = parseLinkLabel; module.exports.parseLinkLabel = parseLinkLabel;
module.exports.parseLinkDestination = parseLinkDestination; module.exports.parseLinkDestination = parseLinkDestination;
module.exports.parseLinkTitle = parseLinkTitle; module.exports.parseLinkTitle = parseLinkTitle;
module.exports.normalizeReference = normalizeReference;

Loading…
Cancel
Save