Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed https://markdown-it.github.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.0 KiB

'use strict';
var StateInline = require('./rules_inline/state_inline');
var parseLinkLabel = require('./links').parseLinkLabel;
var parseLinkDestination = require('./links').parseLinkDestination;
var parseLinkTitle = require('./links').parseLinkTitle;
var normalizeReference = require('./links').normalizeReference;
// Parse link reference definition.
//
module.exports = function parse_reference(str, parser, options, env) {
var state, labelEnd, pos, max, code, start, href, title, label;
if (str.charCodeAt(0) !== 0x5B/* [ */) { return -1; }
if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env);
labelEnd = parseLinkLabel(state, 0);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
max = state.posMax;
// [label]: destination 'title'
// ^^^ skip optional whitespace here
for (pos = labelEnd + 2; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^^^^^ parse this
if (!parseLinkDestination(state, pos)) { return -1; }
href = state.linkContent;
pos = state.pos;
// [label]: destination 'title'
// ^^^ skipping those spaces
start = pos;
for (pos = pos + 1; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^ parse this
if (pos < max && start !== pos && parseLinkTitle(state, pos)) {
title = state.linkContent;
pos = state.pos;
} else {
title = '';
pos = start;
}
// ensure that the end of the line is empty
while (pos < max && state.src.charCodeAt(pos) === 0x20/* space */) { pos++; }
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 };
return pos;
};