-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
143 lines (117 loc) · 3.54 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Process front matter and pass to cb
'use strict';
module.exports = function front_matter_plugin(md, cb) {
var min_markers = 3,
marker_str = '-',
marker_char = marker_str.charCodeAt(0),
marker_len = marker_str.length;
function frontMatter(state, startLine, endLine, silent) {
var pos,
nextLine,
marker_count,
token,
old_parent,
old_line_max,
start_content,
auto_closed = false,
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// Check out the first character of the first line quickly,
// this should filter out non-front matter
if (startLine !== 0 || marker_char !== state.src.charCodeAt(0)) {
return false;
}
// Check out the rest of the marker string
// while pos <= 3
for (pos = start + 1; pos <= max; pos++) {
if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
start_content = pos + 1;
break;
}
}
marker_count = Math.floor((pos - start) / marker_len);
if (marker_count < min_markers) {
return false;
}
pos -= (pos - start) % marker_len;
// Since start is found, we can report success here in validation mode
if (silent) {
return true;
}
// Search for the end of the 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;
}
if (state.src.slice(start, max) === '...') {
break;
}
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
}
if (marker_char !== state.src.charCodeAt(start)) {
continue;
}
if (state.sCount[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
continue;
}
for (pos = start + 1; pos <= max; pos++) {
if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
break;
}
}
// closing code fence must be at least as long as the opening one
if (Math.floor((pos - start) / marker_len) < marker_count) {
continue;
}
// make sure tail has spaces only
pos -= (pos - start) % marker_len;
pos = state.skipSpaces(pos);
if (pos < max) {
continue;
}
// found!
auto_closed = true;
break;
}
old_parent = state.parentType;
old_line_max = state.lineMax;
state.parentType = 'container';
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
token = state.push('front_matter', null, 0);
token.hidden = true;
token.markup = state.src.slice(startLine, pos);
token.block = true;
token.map = [ startLine, nextLine + (auto_closed ? 1 : 0) ];
token.meta = state.src.slice(start_content, start - 1);
state.parentType = old_parent;
state.lineMax = old_line_max;
state.line = nextLine + (auto_closed ? 1 : 0);
cb(token.meta);
return true;
}
md.block.ruler.before(
'table',
'front_matter',
frontMatter,
{
alt: [
'paragraph',
'reference',
'blockquote',
'list'
]
}
);
};