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.
142 lines
4.0 KiB
142 lines
4.0 KiB
10 years ago
|
/** internal
|
||
|
* class ParserBlock
|
||
|
*
|
||
|
* Block-level tokenizer.
|
||
|
**/
|
||
10 years ago
|
|
||
12 months ago
|
import Ruler from './ruler.mjs';
|
||
|
import StateBlock from './rules_block/state_block.mjs';
|
||
|
|
||
|
import r_table from './rules_block/table.mjs';
|
||
|
import r_code from './rules_block/code.mjs';
|
||
|
import r_fence from './rules_block/fence.mjs';
|
||
|
import r_blockquote from './rules_block/blockquote.mjs';
|
||
|
import r_hr from './rules_block/hr.mjs';
|
||
|
import r_list from './rules_block/list.mjs';
|
||
|
import r_reference from './rules_block/reference.mjs';
|
||
|
import r_html_block from './rules_block/html_block.mjs';
|
||
|
import r_heading from './rules_block/heading.mjs';
|
||
|
import r_lheading from './rules_block/lheading.mjs';
|
||
|
import r_paragraph from './rules_block/paragraph.mjs';
|
||
10 years ago
|
|
||
10 years ago
|
var _rules = [
|
||
10 years ago
|
// First 2 params - rule name & source. Secondary array - list of rules,
|
||
|
// which can be terminated by this one.
|
||
12 months ago
|
[ 'table', r_table, [ 'paragraph', 'reference' ] ],
|
||
|
[ 'code', r_code ],
|
||
|
[ 'fence', r_fence, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
|
||
|
[ 'blockquote', r_blockquote, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
|
||
|
[ 'hr', r_hr, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
|
||
|
[ 'list', r_list, [ 'paragraph', 'reference', 'blockquote' ] ],
|
||
|
[ 'reference', r_reference ],
|
||
|
[ 'html_block', r_html_block, [ 'paragraph', 'reference', 'blockquote' ] ],
|
||
|
[ 'heading', r_heading, [ 'paragraph', 'reference', 'blockquote' ] ],
|
||
|
[ 'lheading', r_lheading ],
|
||
|
[ 'paragraph', r_paragraph ]
|
||
10 years ago
|
];
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* new ParserBlock()
|
||
|
**/
|
||
10 years ago
|
function ParserBlock() {
|
||
10 years ago
|
/**
|
||
|
* ParserBlock#ruler -> Ruler
|
||
|
*
|
||
|
* [[Ruler]] instance. Keep configuration of block rules.
|
||
|
**/
|
||
10 years ago
|
this.ruler = new Ruler();
|
||
10 years ago
|
|
||
10 years ago
|
for (var i = 0; i < _rules.length; i++) {
|
||
10 years ago
|
this.ruler.push(_rules[i][0], _rules[i][1], { alt: (_rules[i][2] || []).slice() });
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// Generate tokens for input range
|
||
|
//
|
||
10 years ago
|
ParserBlock.prototype.tokenize = function (state, startLine, endLine) {
|
||
2 years ago
|
var ok, i, prevLine,
|
||
10 years ago
|
rules = this.ruler.getRules(''),
|
||
|
len = rules.length,
|
||
10 years ago
|
line = startLine,
|
||
10 years ago
|
hasEmptyLines = false,
|
||
|
maxNesting = state.md.options.maxNesting;
|
||
10 years ago
|
|
||
10 years ago
|
while (line < endLine) {
|
||
10 years ago
|
state.line = line = state.skipEmptyLines(line);
|
||
10 years ago
|
if (line >= endLine) { break; }
|
||
10 years ago
|
|
||
10 years ago
|
// Termination condition for nested calls.
|
||
|
// Nested calls currently used for blockquotes & lists
|
||
9 years ago
|
if (state.sCount[line] < state.blkIndent) { break; }
|
||
10 years ago
|
|
||
10 years ago
|
// If nesting level exceeded - skip tail to the end. That's not ordinary
|
||
|
// situation and we should not care about content.
|
||
|
if (state.level >= maxNesting) {
|
||
|
state.line = endLine;
|
||
|
break;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Try all possible rules.
|
||
|
// On success, rule should:
|
||
|
//
|
||
10 years ago
|
// - update `state.line`
|
||
10 years ago
|
// - update `state.tokens`
|
||
|
// - return true
|
||
2 years ago
|
prevLine = state.line;
|
||
10 years ago
|
|
||
|
for (i = 0; i < len; i++) {
|
||
10 years ago
|
ok = rules[i](state, line, endLine, false);
|
||
2 years ago
|
if (ok) {
|
||
|
if (prevLine >= state.line) {
|
||
|
throw new Error("block rule didn't increment state.line");
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
2 years ago
|
// this can only happen if user disables paragraph rule
|
||
|
if (!ok) throw new Error('none of the block rules matched');
|
||
|
|
||
8 years ago
|
// set state.tight if we had an empty line before current tag
|
||
10 years ago
|
// i.e. latest empty line should not count
|
||
|
state.tight = !hasEmptyLines;
|
||
|
|
||
10 years ago
|
// paragraph might "eat" one newline after it in nested lists
|
||
10 years ago
|
if (state.isEmpty(state.line - 1)) {
|
||
10 years ago
|
hasEmptyLines = true;
|
||
|
}
|
||
|
|
||
|
line = state.line;
|
||
|
|
||
10 years ago
|
if (line < endLine && state.isEmpty(line)) {
|
||
10 years ago
|
hasEmptyLines = true;
|
||
10 years ago
|
line++;
|
||
|
state.line = line;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
};
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
10 years ago
|
* ParserBlock.parse(str, md, env, outTokens)
|
||
10 years ago
|
*
|
||
|
* Process input string and push block tokens into `outTokens`
|
||
|
**/
|
||
10 years ago
|
ParserBlock.prototype.parse = function (src, md, env, outTokens) {
|
||
10 years ago
|
var state;
|
||
10 years ago
|
|
||
9 years ago
|
if (!src) { return; }
|
||
10 years ago
|
|
||
10 years ago
|
state = new this.State(src, md, env, outTokens);
|
||
10 years ago
|
|
||
|
this.tokenize(state, state.line, state.lineMax);
|
||
10 years ago
|
};
|
||
|
|
||
|
|
||
12 months ago
|
ParserBlock.prototype.State = StateBlock;
|
||
10 years ago
|
|
||
|
|
||
12 months ago
|
export default ParserBlock;
|