forked from knubie/remarkable-brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_parser.js
113 lines (85 loc) · 2.91 KB
/
block_parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
module.exports = function make_block_parser(opts) {
var name = opts.name;
var opener = opts.opener;
var closer = opts.closer;
var allowSpace = opts.allowSpace;
var openerCharCode = opener.charCodeAt(0);
var closerCharCode = closer.charCodeAt(0);
return function parser(state, startLine, endLine, silent) {
var marker, len, params, nextLine, mem,
haveEndMarker = false,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// If the line length is shorter than the opener.
if (pos + opener.length > max) { return false; }
marker = state.src.charCodeAt(pos);
if (marker !== openerCharCode) {
return false;
}
// scan marker length
mem = pos;
pos = state.skipChars(pos, marker);
len = pos - mem;
if (len < opener.length) { return false; }
params = state.src.slice(pos, max).trim();
if (params.indexOf(closer) >= 0) { return false; }
// Since start is found, we can report success here in validation mode
if (silent) { return true; }
// search end of block
nextLine = startLine;
for (;;) {
nextLine++;
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
break;
}
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (pos < max && state.tShift[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
}
if (state.src.charCodeAt(pos) !== closerCharCode) { continue; }
if (state.tShift[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
continue;
}
pos = state.skipChars(pos, closerCharCode);
// closing code fence must be at least as long as the opening one
if (pos - mem < len) { continue; }
// make sure tail has spaces only
pos = state.skipSpaces(pos);
if (pos < max) { continue; }
haveEndMarker = true;
// found!
break;
}
// If a fence has heading spaces, they should be removed from its inner block
len = state.tShift[startLine];
const oldMax = state.lineMax;
state.lineMax = nextLine + (haveEndMarker ? 0 : -1);
const oldParentType = state.parentType;
state.parentType = name;
let lines;
state.tokens.push({
type: name + '_open',
lines: lines = [startLine, 0],
level: state.level,
block: true
});
state.parser.tokenize(state, startLine + 1, nextLine);
state.tokens.push({
type: name + '_close',
level: state.level
});
lines[1] = nextLine;
state.line = nextLine + (haveEndMarker ? 1 : 0);
state.lineMax = oldMax;
state.parentType = oldParentType;
return true;
}
};