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.
77 lines
1.6 KiB
77 lines
1.6 KiB
// Inline parser state
|
|
|
|
'use strict';
|
|
|
|
|
|
var Token = require('../token');
|
|
|
|
function StateInline(src, md, env, outTokens) {
|
|
this.src = src;
|
|
this.env = env;
|
|
this.md = md;
|
|
this.tokens = outTokens;
|
|
|
|
this.pos = 0;
|
|
this.posMax = this.src.length;
|
|
this.level = 0;
|
|
this.pending = '';
|
|
this.pendingLevel = 0;
|
|
|
|
this.cache = []; // Stores { start: end } pairs. Useful for backtrack
|
|
// optimization of pairs parse (emphasis, strikes).
|
|
}
|
|
|
|
|
|
// Flush pending text
|
|
//
|
|
StateInline.prototype.pushPending = function () {
|
|
var token = new Token('text', '', 0);
|
|
token.content = this.pending;
|
|
token.level = this.pendingLevel;
|
|
this.tokens.push(token);
|
|
this.pending = '';
|
|
return token;
|
|
};
|
|
|
|
|
|
// Push new token to "stream".
|
|
// If pending text exists - flush it as text token
|
|
//
|
|
StateInline.prototype.push = function (type, tag, nesting) {
|
|
if (this.pending) {
|
|
this.pushPending();
|
|
}
|
|
|
|
var token = new Token(type, tag, nesting);
|
|
|
|
if (nesting < 0) { this.level--; }
|
|
token.level = this.level;
|
|
if (nesting > 0) { this.level++; }
|
|
|
|
this.pendingLevel = this.level;
|
|
this.tokens.push(token);
|
|
return token;
|
|
};
|
|
|
|
|
|
// Store value to cache.
|
|
// !!! Implementation has parser-specific optimizations
|
|
// !!! keys MUST be integer, >= 0; values MUST be integer, > 0
|
|
//
|
|
StateInline.prototype.cacheSet = function (key, val) {
|
|
for (var i = this.cache.length; i <= key; i++) {
|
|
this.cache.push(0);
|
|
}
|
|
|
|
this.cache[key] = val;
|
|
};
|
|
|
|
|
|
// Get cache value
|
|
//
|
|
StateInline.prototype.cacheGet = function (key) {
|
|
return key < this.cache.length ? this.cache[key] : 0;
|
|
};
|
|
|
|
|
|
module.exports = StateInline;
|
|
|