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; }
return {
label: str.slice(1, labelEnd).trim().replace(/\s+/g, ' '),
label: links.normalizeReference(str.slice(1, labelEnd)),
title: title,
href: href,
remaining: str.slice(pos)

27
lib/lexer_inline/links.js

@ -164,13 +164,19 @@ function parseLinkTitle(state, pos) {
return null;
}
function normalizeReference(str) {
return str.trim().replace(/\s+/g, ' ').toLowerCase();
}
function links(state) {
var labelStart,
labelEnd,
label,
href,
title,
pos,
ref,
code,
isImage = false,
max = state.posMax,
start = state.pos,
@ -235,7 +241,25 @@ function links(state) {
//
// 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; }
href = ref.href;
title = ref.title;
@ -269,3 +293,4 @@ module.exports = links;
module.exports.parseLinkLabel = parseLinkLabel;
module.exports.parseLinkDestination = parseLinkDestination;
module.exports.parseLinkTitle = parseLinkTitle;
module.exports.normalizeReference = normalizeReference;

Loading…
Cancel
Save