forked from markdown-it/markdown-it-container
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
167 lines (134 loc) · 4.64 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Process block-level custom containers
//
'use strict';
module.exports = function container_plugin(md, options) {
function validateDefault() {
return true;
}
function renderDefault(tokens, idx, _options, env, slf) {
// add a class to the opening tag
if (tokens[idx].nesting === 1) {
var params = tokens[idx].info.split(/\s+/);
var id = null, classes = [];
for (var i = 0; i < params.length; i++) {
var className = params[i];
if (className.includes('=')) {
var set = className.split('=', 2);
tokens[idx].attrJoin(set[0], set[1]);
} else if (className[0] === '#') {
id = className.slice(1);
} else if (className[0] === '.') {
classes.push(className.slice(1));
} else {
classes.push(className);
}
}
if (id) {
tokens[idx].attrJoin('id', id);
}
if (classes.length > 0) {
tokens[idx].attrJoin('class', classes.join(' '));
}
}
return slf.renderToken(tokens, idx, _options, env, slf);
}
options = options || {};
var min_markers = 3,
marker_str = options.marker || ':',
marker_char = marker_str.charCodeAt(0),
marker_len = marker_str.length,
validate = options.validate || validateDefault,
render = options.render || renderDefault;
function container(state, startLine, endLine, silent) {
var pos, nextLine, marker_count, markup, params, token,
old_parent, old_line_max, nested = 1,
auto_closed = false,
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// Check out the first character quickly,
// this should filter out most of non-containers
//
if (marker_char !== state.src.charCodeAt(start)) { return false; }
// Check out the rest of the marker string
//
for (pos = start + 1; pos <= max; pos++) {
if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
break;
}
}
marker_count = Math.floor((pos - start) / marker_len);
if (marker_count < min_markers) { return false; }
pos -= (pos - start) % marker_len;
markup = state.src.slice(start, pos);
params = state.src.slice(pos, max).trim();
if (!validate(params)) { return false; }
// 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;
}
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) {
nested++;
continue;
}
// found!
if (--nested === 0) {
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('container', 'div', 1);
token.markup = markup;
token.block = true;
token.info = params;
token.map = [ startLine, nextLine ];
state.md.block.tokenize(state, startLine + 1, nextLine);
token = state.push('container', 'div', -1);
token.markup = state.src.slice(start, pos);
token.block = true;
state.parentType = old_parent;
state.lineMax = old_line_max;
state.line = nextLine + (auto_closed ? 1 : 0);
return true;
}
md.block.ruler.before('fence', 'container', container, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
});
md.renderer.rules.container = render;
};