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.
131 lines
2.9 KiB
131 lines
2.9 KiB
/** internal
|
|
* class ParserInline
|
|
*
|
|
* Tokenizes paragraph content.
|
|
**/
|
|
'use strict';
|
|
|
|
|
|
var Ruler = require('./ruler');
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Parser rules
|
|
|
|
var _rules = [
|
|
[ 'text', require('./rules_inline/text') ],
|
|
[ 'newline', require('./rules_inline/newline') ],
|
|
[ 'escape', require('./rules_inline/escape') ],
|
|
[ 'backticks', require('./rules_inline/backticks') ],
|
|
[ 'strikethrough', require('./rules_inline/strikethrough') ],
|
|
[ 'emphasis', require('./rules_inline/emphasis') ],
|
|
[ 'link', require('./rules_inline/link') ],
|
|
[ 'image', require('./rules_inline/image') ],
|
|
[ 'autolink', require('./rules_inline/autolink') ],
|
|
[ 'html_inline', require('./rules_inline/html_inline') ],
|
|
[ 'entity', require('./rules_inline/entity') ]
|
|
];
|
|
|
|
|
|
/**
|
|
* new ParserInline()
|
|
**/
|
|
function ParserInline() {
|
|
/**
|
|
* ParserInline#ruler -> Ruler
|
|
*
|
|
* [[Ruler]] instance. Keep configuration of inline rules.
|
|
**/
|
|
this.ruler = new Ruler();
|
|
|
|
for (var i = 0; i < _rules.length; i++) {
|
|
this.ruler.push(_rules[i][0], _rules[i][1]);
|
|
}
|
|
}
|
|
|
|
|
|
// Skip single token by running all rules in validation mode;
|
|
// returns `true` if any rule reported success
|
|
//
|
|
ParserInline.prototype.skipToken = function (state) {
|
|
var i, pos = state.pos,
|
|
rules = this.ruler.getRules(''),
|
|
len = rules.length,
|
|
maxNesting = state.md.options.maxNesting,
|
|
cache = state.cache;
|
|
|
|
|
|
if (typeof cache[pos] !== 'undefined') {
|
|
state.pos = cache[pos];
|
|
return;
|
|
}
|
|
|
|
/*istanbul ignore else*/
|
|
if (state.level < maxNesting) {
|
|
for (i = 0; i < len; i++) {
|
|
if (rules[i](state, true)) {
|
|
cache[pos] = state.pos;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
state.pos++;
|
|
cache[pos] = state.pos;
|
|
};
|
|
|
|
|
|
// Generate tokens for input range
|
|
//
|
|
ParserInline.prototype.tokenize = function (state) {
|
|
var ok, i,
|
|
rules = this.ruler.getRules(''),
|
|
len = rules.length,
|
|
end = state.posMax,
|
|
maxNesting = state.md.options.maxNesting;
|
|
|
|
while (state.pos < end) {
|
|
// Try all possible rules.
|
|
// On success, rule should:
|
|
//
|
|
// - update `state.pos`
|
|
// - update `state.tokens`
|
|
// - return true
|
|
|
|
if (state.level < maxNesting) {
|
|
for (i = 0; i < len; i++) {
|
|
ok = rules[i](state, false);
|
|
if (ok) { break; }
|
|
}
|
|
}
|
|
|
|
if (ok) {
|
|
if (state.pos >= end) { break; }
|
|
continue;
|
|
}
|
|
|
|
state.pending += state.src[state.pos++];
|
|
}
|
|
|
|
if (state.pending) {
|
|
state.pushPending();
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* ParserInline.parse(str, md, env, outTokens)
|
|
*
|
|
* Process input string and push inline tokens into `outTokens`
|
|
**/
|
|
ParserInline.prototype.parse = function (str, md, env, outTokens) {
|
|
var state = new this.State(str, md, env, outTokens);
|
|
|
|
this.tokenize(state);
|
|
};
|
|
|
|
|
|
ParserInline.prototype.State = require('./rules_inline/state_inline');
|
|
|
|
|
|
module.exports = ParserInline;
|
|
|