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.
61 lines
1.7 KiB
61 lines
1.7 KiB
10 years ago
|
// Parse backticks
|
||
|
|
||
1 year ago
|
export default function backtick(state, silent) {
|
||
4 years ago
|
var start, max, marker, token, matchStart, matchEnd, openerLength, closerLength,
|
||
10 years ago
|
pos = state.pos,
|
||
|
ch = state.src.charCodeAt(pos);
|
||
|
|
||
|
if (ch !== 0x60/* ` */) { return false; }
|
||
|
|
||
|
start = pos;
|
||
|
pos++;
|
||
|
max = state.posMax;
|
||
|
|
||
4 years ago
|
// scan marker length
|
||
10 years ago
|
while (pos < max && state.src.charCodeAt(pos) === 0x60/* ` */) { pos++; }
|
||
|
|
||
|
marker = state.src.slice(start, pos);
|
||
4 years ago
|
openerLength = marker.length;
|
||
|
|
||
|
if (state.backticksScanned && (state.backticks[openerLength] || 0) <= start) {
|
||
|
if (!silent) state.pending += marker;
|
||
|
state.pos += openerLength;
|
||
4 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
3 years ago
|
matchEnd = pos;
|
||
10 years ago
|
|
||
4 years ago
|
// Nothing found in the cache, scan until the end of the line (or until marker is found)
|
||
10 years ago
|
while ((matchStart = state.src.indexOf('`', matchEnd)) !== -1) {
|
||
|
matchEnd = matchStart + 1;
|
||
10 years ago
|
|
||
4 years ago
|
// scan marker length
|
||
10 years ago
|
while (matchEnd < max && state.src.charCodeAt(matchEnd) === 0x60/* ` */) { matchEnd++; }
|
||
|
|
||
4 years ago
|
closerLength = matchEnd - matchStart;
|
||
4 years ago
|
|
||
4 years ago
|
if (closerLength === openerLength) {
|
||
4 years ago
|
// Found matching closer length.
|
||
4 years ago
|
if (!silent) {
|
||
|
token = state.push('code_inline', 'code', 0);
|
||
|
token.markup = marker;
|
||
|
token.content = state.src.slice(pos, matchStart)
|
||
|
.replace(/\n/g, ' ')
|
||
|
.replace(/^ (.+) $/, '$1');
|
||
|
}
|
||
10 years ago
|
state.pos = matchEnd;
|
||
10 years ago
|
return true;
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
// Some different length found, put it in cache as upper limit of where closer can be found
|
||
|
state.backticks[closerLength] = matchStart;
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// Scanned through the end, didn't find anything
|
||
|
state.backticksScanned = true;
|
||
|
|
||
4 years ago
|
if (!silent) state.pending += marker;
|
||
4 years ago
|
state.pos += openerLength;
|
||
10 years ago
|
return true;
|
||
1 year ago
|
}
|