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.
75 lines
2.0 KiB
75 lines
2.0 KiB
// Parse abbreviation definitions, i.e. `*[abbr]: description`
|
|
//
|
|
|
|
'use strict';
|
|
|
|
|
|
function parseAbbr(str, parserInline, options, env) {
|
|
var pos, label, title, ch,
|
|
max = str.length,
|
|
labelEnd = -1;
|
|
|
|
if (str.charCodeAt(0) !== 0x2A/* * */) { return -1; }
|
|
if (str.charCodeAt(1) !== 0x5B/* [ */) { return -1; }
|
|
|
|
if (str.indexOf(']:') === -1) { return -1; }
|
|
|
|
for (pos = 2; pos < max; pos++) {
|
|
ch = str.charCodeAt(pos);
|
|
if (ch === 0x5B /* [ */) {
|
|
return -1;
|
|
} else if (ch === 0x5D /* ] */) {
|
|
labelEnd = pos;
|
|
break;
|
|
} else if (ch === 0x5C /* \ */) {
|
|
pos++;
|
|
}
|
|
}
|
|
|
|
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
|
|
|
|
// abbr title is always one line, so looking for ending "\n" here
|
|
for (pos = labelEnd + 2; pos < max; pos++) {
|
|
if (str.charCodeAt(pos) === 0x0A) { break; }
|
|
}
|
|
|
|
label = str.slice(2, labelEnd).replace(/\\(.)/g, '$1');
|
|
title = str.slice(labelEnd + 2, pos).trim();
|
|
if (title.length === 0) { return -1; }
|
|
if (!env.abbreviations) { env.abbreviations = {}; }
|
|
// prepend ':' to avoid conflict with Object.prototype members
|
|
if (typeof env.abbreviations[':' + label] === 'undefined') {
|
|
env.abbreviations[':' + label] = title;
|
|
}
|
|
|
|
return pos;
|
|
}
|
|
|
|
module.exports = function abbr(state) {
|
|
var tokens = state.tokens, i, l, content, pos;
|
|
|
|
if (state.inlineMode) {
|
|
return;
|
|
}
|
|
|
|
// Parse inlines
|
|
for (i = 1, l = tokens.length - 1; i < l; i++) {
|
|
if (tokens[i - 1].type === 'paragraph_open' &&
|
|
tokens[i].type === 'inline' &&
|
|
tokens[i + 1].type === 'paragraph_close') {
|
|
|
|
content = tokens[i].content;
|
|
while (content.length) {
|
|
pos = parseAbbr(content, state.inline, state.options, state.env);
|
|
if (pos < 0) { break; }
|
|
content = content.slice(pos).trim();
|
|
}
|
|
|
|
tokens[i].content = content;
|
|
if (!content.length) {
|
|
tokens[i - 1].tight = true;
|
|
tokens[i + 1].tight = true;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|