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.
227 lines
5.9 KiB
227 lines
5.9 KiB
10 years ago
|
// Parser state class
|
||
|
|
||
1 year ago
|
import Token from '../token.mjs';
|
||
|
import { isSpace } from '../common/utils.mjs';
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
function StateBlock(src, md, env, tokens) {
|
||
10 years ago
|
var ch, s, start, pos, len, indent, offset, indent_found;
|
||
10 years ago
|
|
||
10 years ago
|
this.src = src;
|
||
10 years ago
|
|
||
10 years ago
|
// link to parser instance
|
||
|
this.md = md;
|
||
10 years ago
|
|
||
10 years ago
|
this.env = env;
|
||
|
|
||
10 years ago
|
//
|
||
|
// Internal state vartiables
|
||
|
//
|
||
|
|
||
10 years ago
|
this.tokens = tokens;
|
||
10 years ago
|
|
||
10 years ago
|
this.bMarks = []; // line begin offsets for fast jumps
|
||
|
this.eMarks = []; // line end offsets for fast jumps
|
||
10 years ago
|
this.tShift = []; // offsets of the first non-space characters (tabs not expanded)
|
||
|
this.sCount = []; // indents for each line (tabs expanded)
|
||
10 years ago
|
|
||
8 years ago
|
// An amount of virtual spaces (tabs expanded) between beginning
|
||
|
// of each line (bMarks) and real beginning of that line.
|
||
|
//
|
||
|
// It exists only as a hack because blockquotes override bMarks
|
||
|
// losing information in the process.
|
||
|
//
|
||
|
// It's used only when expanding tabs, you can think about it as
|
||
|
// an initial tab length, e.g. bsCount=21 applied to string `\t123`
|
||
|
// means first tab should be expanded to 4-21%4 === 3 spaces.
|
||
|
//
|
||
|
this.bsCount = [];
|
||
|
|
||
10 years ago
|
// block parser variables
|
||
6 years ago
|
this.blkIndent = 0; // required block content indent (for example, if we are
|
||
|
// inside a list, it would be positioned after list marker)
|
||
10 years ago
|
this.line = 0; // line index in src
|
||
|
this.lineMax = 0; // lines count
|
||
10 years ago
|
this.tight = false; // loose/tight mode for lists
|
||
10 years ago
|
this.ddIndent = -1; // indent of the current dd block (-1 if there isn't any)
|
||
6 years ago
|
this.listIndent = -1; // indent of the current list block (-1 if there isn't any)
|
||
10 years ago
|
|
||
8 years ago
|
// can be 'blockquote', 'list', 'root', 'paragraph' or 'reference'
|
||
|
// used in lists to determine if they interrupt a paragraph
|
||
|
this.parentType = 'root';
|
||
|
|
||
10 years ago
|
this.level = 0;
|
||
|
|
||
|
// Create caches
|
||
10 years ago
|
// Generate markers.
|
||
|
s = this.src;
|
||
|
indent_found = false;
|
||
|
|
||
10 years ago
|
for (start = pos = indent = offset = 0, len = s.length; pos < len; pos++) {
|
||
10 years ago
|
ch = s.charCodeAt(pos);
|
||
|
|
||
10 years ago
|
if (!indent_found) {
|
||
10 years ago
|
if (isSpace(ch)) {
|
||
10 years ago
|
indent++;
|
||
10 years ago
|
|
||
|
if (ch === 0x09) {
|
||
|
offset += 4 - offset % 4;
|
||
|
} else {
|
||
|
offset++;
|
||
|
}
|
||
10 years ago
|
continue;
|
||
|
} else {
|
||
|
indent_found = true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (ch === 0x0A || pos === len - 1) {
|
||
10 years ago
|
if (ch !== 0x0A) { pos++; }
|
||
10 years ago
|
this.bMarks.push(start);
|
||
|
this.eMarks.push(pos);
|
||
10 years ago
|
this.tShift.push(indent);
|
||
10 years ago
|
this.sCount.push(offset);
|
||
8 years ago
|
this.bsCount.push(0);
|
||
10 years ago
|
|
||
10 years ago
|
indent_found = false;
|
||
|
indent = 0;
|
||
10 years ago
|
offset = 0;
|
||
10 years ago
|
start = pos + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// Push fake entry to simplify cache bounds checks
|
||
|
this.bMarks.push(s.length);
|
||
|
this.eMarks.push(s.length);
|
||
|
this.tShift.push(0);
|
||
10 years ago
|
this.sCount.push(0);
|
||
8 years ago
|
this.bsCount.push(0);
|
||
10 years ago
|
|
||
10 years ago
|
this.lineMax = this.bMarks.length - 1; // don't count last fake line
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Push new token to "stream".
|
||
|
//
|
||
|
StateBlock.prototype.push = function (type, tag, nesting) {
|
||
|
var token = new Token(type, tag, nesting);
|
||
|
token.block = true;
|
||
|
|
||
6 years ago
|
if (nesting < 0) this.level--; // closing tag
|
||
10 years ago
|
token.level = this.level;
|
||
6 years ago
|
if (nesting > 0) this.level++; // opening tag
|
||
10 years ago
|
|
||
|
this.tokens.push(token);
|
||
|
return token;
|
||
|
};
|
||
|
|
||
10 years ago
|
StateBlock.prototype.isEmpty = function isEmpty(line) {
|
||
10 years ago
|
return this.bMarks[line] + this.tShift[line] >= this.eMarks[line];
|
||
|
};
|
||
|
|
||
10 years ago
|
StateBlock.prototype.skipEmptyLines = function skipEmptyLines(from) {
|
||
10 years ago
|
for (var max = this.lineMax; from < max; from++) {
|
||
|
if (this.bMarks[from] + this.tShift[from] < this.eMarks[from]) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return from;
|
||
|
};
|
||
|
|
||
|
// Skip spaces from given position.
|
||
10 years ago
|
StateBlock.prototype.skipSpaces = function skipSpaces(pos) {
|
||
10 years ago
|
var ch;
|
||
|
|
||
10 years ago
|
for (var max = this.src.length; pos < max; pos++) {
|
||
10 years ago
|
ch = this.src.charCodeAt(pos);
|
||
|
if (!isSpace(ch)) { break; }
|
||
|
}
|
||
|
return pos;
|
||
|
};
|
||
|
|
||
|
// Skip spaces from given position in reverse.
|
||
|
StateBlock.prototype.skipSpacesBack = function skipSpacesBack(pos, min) {
|
||
|
if (pos <= min) { return pos; }
|
||
|
|
||
|
while (pos > min) {
|
||
|
if (!isSpace(this.src.charCodeAt(--pos))) { return pos + 1; }
|
||
10 years ago
|
}
|
||
|
return pos;
|
||
|
};
|
||
|
|
||
|
// Skip char codes from given position
|
||
10 years ago
|
StateBlock.prototype.skipChars = function skipChars(pos, code) {
|
||
10 years ago
|
for (var max = this.src.length; pos < max; pos++) {
|
||
|
if (this.src.charCodeAt(pos) !== code) { break; }
|
||
|
}
|
||
|
return pos;
|
||
|
};
|
||
|
|
||
|
// Skip char codes reverse from given position - 1
|
||
10 years ago
|
StateBlock.prototype.skipCharsBack = function skipCharsBack(pos, code, min) {
|
||
10 years ago
|
if (pos <= min) { return pos; }
|
||
|
|
||
|
while (pos > min) {
|
||
|
if (code !== this.src.charCodeAt(--pos)) { return pos + 1; }
|
||
|
}
|
||
|
return pos;
|
||
|
};
|
||
|
|
||
|
// cut lines range from source.
|
||
10 years ago
|
StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF) {
|
||
10 years ago
|
var i, lineIndent, ch, first, last, queue, lineStart,
|
||
10 years ago
|
line = begin;
|
||
|
|
||
|
if (begin >= end) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
queue = new Array(end - begin);
|
||
|
|
||
|
for (i = 0; line < end; line++, i++) {
|
||
10 years ago
|
lineIndent = 0;
|
||
|
lineStart = first = this.bMarks[line];
|
||
10 years ago
|
|
||
|
if (line + 1 < end || keepLastLF) {
|
||
|
// No need for bounds check because we have fake entry on tail.
|
||
|
last = this.eMarks[line] + 1;
|
||
|
} else {
|
||
|
last = this.eMarks[line];
|
||
|
}
|
||
|
|
||
10 years ago
|
while (first < last && lineIndent < indent) {
|
||
|
ch = this.src.charCodeAt(first);
|
||
|
|
||
|
if (isSpace(ch)) {
|
||
|
if (ch === 0x09) {
|
||
8 years ago
|
lineIndent += 4 - (lineIndent + this.bsCount[line]) % 4;
|
||
10 years ago
|
} else {
|
||
|
lineIndent++;
|
||
|
}
|
||
|
} else if (first - lineStart < this.tShift[line]) {
|
||
|
// patched tShift masked characters to look like spaces (blockquotes, list markers)
|
||
|
lineIndent++;
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
first++;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (lineIndent > indent) {
|
||
|
// partially expanding tabs in code blocks, e.g '\t\tfoobar'
|
||
|
// with indent=2 becomes ' \tfoobar'
|
||
|
queue[i] = new Array(lineIndent - indent + 1).join(' ') + this.src.slice(first, last);
|
||
|
} else {
|
||
|
queue[i] = this.src.slice(first, last);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
return queue.join('');
|
||
|
};
|
||
10 years ago
|
|
||
10 years ago
|
// re-export Token class to use in block rules
|
||
10 years ago
|
StateBlock.prototype.Token = Token;
|
||
|
|
||
10 years ago
|
|
||
1 year ago
|
export default StateBlock;
|