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.
224 lines
6.1 KiB
224 lines
6.1 KiB
10 years ago
|
// Block quotes
|
||
|
|
||
12 months ago
|
import { isSpace } from '../common/utils.mjs';
|
||
10 years ago
|
|
||
12 months ago
|
export default function blockquote(state, startLine, endLine, silent) {
|
||
8 years ago
|
var adjustTab,
|
||
|
ch,
|
||
|
i,
|
||
|
initial,
|
||
|
l,
|
||
|
lastLineEmpty,
|
||
|
lines,
|
||
|
nextLine,
|
||
|
offset,
|
||
|
oldBMarks,
|
||
|
oldBSCount,
|
||
|
oldIndent,
|
||
|
oldParentType,
|
||
|
oldSCount,
|
||
|
oldTShift,
|
||
|
spaceAfterMarker,
|
||
|
terminate,
|
||
|
terminatorRules,
|
||
|
token,
|
||
4 years ago
|
isOutdented,
|
||
8 years ago
|
oldLineMax = state.lineMax,
|
||
10 years ago
|
pos = state.bMarks[startLine] + state.tShift[startLine],
|
||
|
max = state.eMarks[startLine];
|
||
|
|
||
8 years ago
|
// if it's indented more than 3 spaces, it should be a code block
|
||
|
if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
|
||
|
|
||
10 years ago
|
// check the block quote marker
|
||
3 years ago
|
if (state.src.charCodeAt(pos) !== 0x3E/* > */) { return false; }
|
||
10 years ago
|
|
||
|
// we know that it's going to be a valid blockquote,
|
||
|
// so no point trying to find the end of it in silent mode
|
||
|
if (silent) { return true; }
|
||
|
|
||
3 years ago
|
oldBMarks = [];
|
||
|
oldBSCount = [];
|
||
|
oldSCount = [];
|
||
|
oldTShift = [];
|
||
10 years ago
|
|
||
10 years ago
|
terminatorRules = state.md.block.ruler.getRules('blockquote');
|
||
10 years ago
|
|
||
8 years ago
|
oldParentType = state.parentType;
|
||
|
state.parentType = 'blockquote';
|
||
|
|
||
10 years ago
|
// Search the end of the block
|
||
|
//
|
||
|
// Block ends with either:
|
||
|
// 1. an empty line outside:
|
||
|
// ```
|
||
|
// > test
|
||
|
//
|
||
|
// ```
|
||
|
// 2. an empty line inside:
|
||
|
// ```
|
||
|
// >
|
||
|
// test
|
||
|
// ```
|
||
8 years ago
|
// 3. another tag:
|
||
10 years ago
|
// ```
|
||
|
// > test
|
||
|
// - - -
|
||
|
// ```
|
||
3 years ago
|
for (nextLine = startLine; nextLine < endLine; nextLine++) {
|
||
8 years ago
|
// check if it's outdented, i.e. it's inside list item and indented
|
||
|
// less than said list item:
|
||
|
//
|
||
|
// ```
|
||
|
// 1. anything
|
||
|
// > current blockquote
|
||
|
// 2. checking this line
|
||
|
// ```
|
||
4 years ago
|
isOutdented = state.sCount[nextLine] < state.blkIndent;
|
||
10 years ago
|
|
||
10 years ago
|
pos = state.bMarks[nextLine] + state.tShift[nextLine];
|
||
|
max = state.eMarks[nextLine];
|
||
|
|
||
|
if (pos >= max) {
|
||
|
// Case 1: line is not inside the blockquote, and this line is empty.
|
||
|
break;
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
if (state.src.charCodeAt(pos++) === 0x3E/* > */ && !isOutdented) {
|
||
10 years ago
|
// This line is inside the blockquote.
|
||
|
|
||
4 years ago
|
// set offset past spaces and ">"
|
||
3 years ago
|
initial = state.sCount[nextLine] + 1;
|
||
9 years ago
|
|
||
8 years ago
|
// skip one optional space after '>'
|
||
|
if (state.src.charCodeAt(pos) === 0x20 /* space */) {
|
||
|
// ' > test '
|
||
|
// ^ -- position start of line here:
|
||
|
pos++;
|
||
|
initial++;
|
||
|
adjustTab = false;
|
||
|
spaceAfterMarker = true;
|
||
|
} else if (state.src.charCodeAt(pos) === 0x09 /* tab */) {
|
||
|
spaceAfterMarker = true;
|
||
|
|
||
3 years ago
|
if ((state.bsCount[nextLine] + initial) % 4 === 3) {
|
||
8 years ago
|
// ' >\t test '
|
||
|
// ^ -- position start of line here (tab has width===1)
|
||
|
pos++;
|
||
|
initial++;
|
||
|
adjustTab = false;
|
||
|
} else {
|
||
|
// ' >\t test '
|
||
|
// ^ -- position start of line here + shift bsCount slightly
|
||
|
// to make extra space appear
|
||
|
adjustTab = true;
|
||
|
}
|
||
|
} else {
|
||
|
spaceAfterMarker = false;
|
||
|
}
|
||
|
|
||
3 years ago
|
offset = initial;
|
||
10 years ago
|
oldBMarks.push(state.bMarks[nextLine]);
|
||
|
state.bMarks[nextLine] = pos;
|
||
10 years ago
|
|
||
9 years ago
|
while (pos < max) {
|
||
|
ch = state.src.charCodeAt(pos);
|
||
|
|
||
|
if (isSpace(ch)) {
|
||
|
if (ch === 0x09) {
|
||
8 years ago
|
offset += 4 - (offset + state.bsCount[nextLine] + (adjustTab ? 1 : 0)) % 4;
|
||
9 years ago
|
} else {
|
||
|
offset++;
|
||
|
}
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
pos++;
|
||
|
}
|
||
|
|
||
10 years ago
|
lastLineEmpty = pos >= max;
|
||
|
|
||
8 years ago
|
oldBSCount.push(state.bsCount[nextLine]);
|
||
|
state.bsCount[nextLine] = state.sCount[nextLine] + 1 + (spaceAfterMarker ? 1 : 0);
|
||
|
|
||
9 years ago
|
oldSCount.push(state.sCount[nextLine]);
|
||
|
state.sCount[nextLine] = offset - initial;
|
||
|
|
||
10 years ago
|
oldTShift.push(state.tShift[nextLine]);
|
||
|
state.tShift[nextLine] = pos - state.bMarks[nextLine];
|
||
|
continue;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
// Case 2: line is not inside the blockquote, and the last line was empty.
|
||
|
if (lastLineEmpty) { break; }
|
||
|
|
||
|
// Case 3: another tag found.
|
||
10 years ago
|
terminate = false;
|
||
|
for (i = 0, l = terminatorRules.length; i < l; i++) {
|
||
|
if (terminatorRules[i](state, nextLine, endLine, true)) {
|
||
|
terminate = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
if (terminate) {
|
||
8 years ago
|
// Quirk to enforce "hard termination mode" for paragraphs;
|
||
|
// normally if you call `tokenize(state, startLine, nextLine)`,
|
||
|
// paragraphs will look below nextLine for paragraph continuation,
|
||
|
// but if blockquote is terminated by another tag, they shouldn't
|
||
|
state.lineMax = nextLine;
|
||
|
|
||
|
if (state.blkIndent !== 0) {
|
||
8 years ago
|
// state.blkIndent was non-zero, we now set it to zero,
|
||
|
// so we need to re-calculate all offsets to appear as
|
||
|
// if indent wasn't changed
|
||
|
oldBMarks.push(state.bMarks[nextLine]);
|
||
|
oldBSCount.push(state.bsCount[nextLine]);
|
||
|
oldTShift.push(state.tShift[nextLine]);
|
||
|
oldSCount.push(state.sCount[nextLine]);
|
||
8 years ago
|
state.sCount[nextLine] -= state.blkIndent;
|
||
8 years ago
|
}
|
||
|
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
|
||
|
oldBMarks.push(state.bMarks[nextLine]);
|
||
8 years ago
|
oldBSCount.push(state.bsCount[nextLine]);
|
||
10 years ago
|
oldTShift.push(state.tShift[nextLine]);
|
||
9 years ago
|
oldSCount.push(state.sCount[nextLine]);
|
||
10 years ago
|
|
||
9 years ago
|
// A negative indentation means that this is a paragraph continuation
|
||
10 years ago
|
//
|
||
9 years ago
|
state.sCount[nextLine] = -1;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
oldIndent = state.blkIndent;
|
||
|
state.blkIndent = 0;
|
||
|
|
||
10 years ago
|
token = state.push('blockquote_open', 'blockquote', 1);
|
||
|
token.markup = '>';
|
||
|
token.map = lines = [ startLine, 0 ];
|
||
10 years ago
|
|
||
10 years ago
|
state.md.block.tokenize(state, startLine, nextLine);
|
||
10 years ago
|
|
||
10 years ago
|
token = state.push('blockquote_close', 'blockquote', -1);
|
||
|
token.markup = '>';
|
||
10 years ago
|
|
||
8 years ago
|
state.lineMax = oldLineMax;
|
||
10 years ago
|
state.parentType = oldParentType;
|
||
10 years ago
|
lines[1] = state.line;
|
||
10 years ago
|
|
||
10 years ago
|
// Restore original tShift; this might not be necessary since the parser
|
||
|
// has already been here, but just to make sure we can do that.
|
||
|
for (i = 0; i < oldTShift.length; i++) {
|
||
|
state.bMarks[i + startLine] = oldBMarks[i];
|
||
|
state.tShift[i + startLine] = oldTShift[i];
|
||
9 years ago
|
state.sCount[i + startLine] = oldSCount[i];
|
||
8 years ago
|
state.bsCount[i + startLine] = oldBSCount[i];
|
||
10 years ago
|
}
|
||
10 years ago
|
state.blkIndent = oldIndent;
|
||
10 years ago
|
|
||
10 years ago
|
return true;
|
||
12 months ago
|
}
|