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
1.9 KiB

// HTML block
'use strict';
var isEmpty = require('../helpers').isEmpty;
var getLines = require('../helpers').getLines;
var block_names = require('../common/html_blocks');
var HTML_TAG_OPEN_RE = /^<([a-zA-Z]{1,15})[\s\/>]/;
var HTML_TAG_CLOSE_RE = /^<\/([a-zA-Z]{1,15})[\s>]/;
function isLetter(ch) {
/*eslint no-bitwise:0*/
var lc = ch | 0x20; // to lower case
return (lc >= 0x61/* a */) && (lc <= 0x7a/* z */);
}
module.exports = function htmlblock(state, startLine, endLine, silent) {
var ch, match, nextLine,
pos = state.bMarks[startLine],
max = state.eMarks[startLine],
shift = state.tShift[startLine];
pos += shift;
if (!state.options.html) { return false; }
if (shift > 3 || pos + 2 >= max || state.blkLevel > 0) { return false; }
if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false; }
ch = state.src.charCodeAt(pos + 1);
if (ch === 0x21/* ! */ || ch === 0x3F/* ? */) {
// Directive start / comment start / processing instruction start
if (silent) { return true; }
} else if (ch === 0x2F/* / */ || isLetter(ch)) {
// Probably start or end of tag
if (ch === 0x2F/* \ */) {
// closing tag
match = state.src.slice(pos, max).match(HTML_TAG_CLOSE_RE);
if (!match) { return false; }
} else {
// opening tag
match = state.src.slice(pos, max).match(HTML_TAG_OPEN_RE);
if (!match) { return false; }
}
// Make sure tag name is valid
if (block_names.indexOf(match[1]) < 0) { return false; }
if (silent) { return true; }
} else {
return false;
}
// If we are here - we detected HTML block.
// Let's roll down till empty line (block end).
nextLine = startLine + 1;
while (nextLine < state.lineMax && !isEmpty(state, nextLine)) {
nextLine++;
}
state.tokens.push({
type: 'html',
content: getLines(state, startLine, nextLine, 0, true)
});
state.line = nextLine;
return true;
};