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.
58 lines
1.4 KiB
58 lines
1.4 KiB
// heading (#, ##, ...)
|
|
|
|
'use strict';
|
|
|
|
|
|
module.exports = function heading(state, startLine, endLine, silent) {
|
|
var ch, level, tmp,
|
|
pos = state.bMarks[startLine] + state.tShift[startLine],
|
|
max = state.eMarks[startLine];
|
|
|
|
if (pos >= max) { return false; }
|
|
|
|
ch = state.src.charCodeAt(pos);
|
|
|
|
if (ch !== 0x23/* # */ || pos >= max) { return false; }
|
|
|
|
// count heading level
|
|
level = 1;
|
|
ch = state.src.charCodeAt(++pos);
|
|
while (ch === 0x23/* # */ && pos < max && level <= 6) {
|
|
level++;
|
|
ch = state.src.charCodeAt(++pos);
|
|
}
|
|
|
|
if (level > 6 || (pos < max && ch !== 0x20/* space */)) { return false; }
|
|
|
|
if (silent) { return true; }
|
|
|
|
// Let's cut tails like ' ### ' from the end of string
|
|
|
|
max = state.skipCharsBack(max, 0x20, pos); // space
|
|
tmp = state.skipCharsBack(max, 0x23, pos); // #
|
|
if (tmp > pos && state.src.charCodeAt(tmp - 1) === 0x20/* space */) {
|
|
max = tmp;
|
|
}
|
|
|
|
state.line = startLine + 1;
|
|
|
|
state.tokens.push({ type: 'heading_open',
|
|
hLevel: level,
|
|
lines: [ startLine, state.line ],
|
|
level: state.level
|
|
});
|
|
|
|
// only if header is not empty
|
|
if (pos < max) {
|
|
state.tokens.push({
|
|
type: 'inline',
|
|
content: state.src.slice(pos, max).trim(),
|
|
level: state.level + 1,
|
|
lines: [ startLine, state.line ],
|
|
children: []
|
|
});
|
|
}
|
|
state.tokens.push({ type: 'heading_close', hLevel: level, level: state.level });
|
|
|
|
return true;
|
|
};
|
|
|