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.
79 lines
1.7 KiB
79 lines
1.7 KiB
// Parse link destination
|
|
//
|
|
// on success it returns a string and updates state.pos;
|
|
// on failure it returns null
|
|
//
|
|
'use strict';
|
|
|
|
|
|
var normalizeLink = require('./normalize_link');
|
|
var unescapeMd = require('../common/utils').unescapeMd;
|
|
|
|
|
|
module.exports = function parseLinkDestination(state, pos) {
|
|
var code, level, link,
|
|
start = pos,
|
|
max = state.posMax;
|
|
|
|
if (state.src.charCodeAt(pos) === 0x3C /* < */) {
|
|
pos++;
|
|
while (pos < max) {
|
|
code = state.src.charCodeAt(pos);
|
|
if (code === 0x0A /* \n */) { return false; }
|
|
if (code === 0x3E /* > */) {
|
|
link = normalizeLink(unescapeMd(state.src.slice(start + 1, pos)));
|
|
if (!state.parser.validateLink(link)) { return false; }
|
|
state.pos = pos + 1;
|
|
state.linkContent = link;
|
|
return true;
|
|
}
|
|
if (code === 0x5C /* \ */ && pos + 1 < max) {
|
|
pos += 2;
|
|
continue;
|
|
}
|
|
|
|
pos++;
|
|
}
|
|
|
|
// no closing '>'
|
|
return false;
|
|
}
|
|
|
|
// this should be ... } else { ... branch
|
|
|
|
level = 0;
|
|
while (pos < max) {
|
|
code = state.src.charCodeAt(pos);
|
|
|
|
if (code === 0x20) { break; }
|
|
|
|
// ascii control characters
|
|
if (code < 0x20 || code === 0x7F) { break; }
|
|
|
|
if (code === 0x5C /* \ */ && pos + 1 < max) {
|
|
pos += 2;
|
|
continue;
|
|
}
|
|
|
|
if (code === 0x28 /* ( */) {
|
|
level++;
|
|
if (level > 1) { break; }
|
|
}
|
|
|
|
if (code === 0x29 /* ) */) {
|
|
level--;
|
|
if (level < 0) { break; }
|
|
}
|
|
|
|
pos++;
|
|
}
|
|
|
|
if (start === pos) { return false; }
|
|
|
|
link = normalizeLink(unescapeMd(state.src.slice(start, pos)));
|
|
if (!state.parser.validateLink(link)) { return false; }
|
|
|
|
state.linkContent = link;
|
|
state.pos = pos;
|
|
return true;
|
|
};
|
|
|