-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.mjs
More file actions
202 lines (155 loc) · 5.97 KB
/
Copy pathindex.mjs
File metadata and controls
202 lines (155 loc) · 5.97 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Process definition lists
//
const ddDepthKey = Symbol('markdown-it-deflist.ddDepth')
export default function deflist_plugin (md) {
const isSpace = md.utils.isSpace
// Search `[:~][\n ]`, returns next pos after marker on success
// or -1 on fail.
function skipMarker (state, line) {
let start = state.bMarks[line] + state.tShift[line]
const max = state.eMarks[line]
if (start >= max) { return -1 }
if (state.sCount[line] - state.blkIndent >= 4) { return -1 }
// Check bullet
const marker = state.src.charCodeAt(start++)
if (marker !== 0x7E/* ~ */ && marker !== 0x3A/* : */) { return -1 }
const pos = state.skipSpaces(start)
// require space after marker if there is inline content
if (start < max && start === pos) { return -1 }
return start
}
function markTightParagraph (state, start, end, level) {
let block = -1
for (let i = start; i < end; i++) {
const token = state.tokens[i]
if (token.level !== level || !token.block || token.nesting < 0) { continue }
if (block >= 0) { return }
block = i
}
if (block < 0 || state.tokens[block].type !== 'paragraph_open') { return }
state.tokens[block + 2].hidden = true
state.tokens[block].hidden = true
}
function deflist (state, startLine, endLine, silent) {
if (silent) {
// quirk: validation mode validates a dd block only, not a whole deflist
if (!state.env[ddDepthKey]) { return false }
const markerPos = skipMarker(state, startLine)
return markerPos >= 0 && state.sCount[startLine] < state.blkIndent
}
let nextLine = startLine + 1
if (nextLine >= endLine) { return false }
let termIsTight = true
if (state.isEmpty(nextLine)) {
nextLine++
termIsTight = false
if (nextLine >= endLine) { return false }
}
if (state.sCount[nextLine] < state.blkIndent) { return false }
let contentStart = skipMarker(state, nextLine)
if (contentStart < 0) { return false }
const token_dl_o = state.push('dl_open', 'dl', 1)
const listLines = [startLine, 0]
token_dl_o.map = listLines
//
// Iterate list items
//
let dtLine = startLine
let ddLine = nextLine
// One definition list can contain multiple DTs,
// and one DT can be followed by multiple DDs.
//
// Thus, there is two loops here, and label is
// needed to break out of the second one
//
/* eslint no-labels:0,block-scoped-var:0 */
OUTER:
for (;;) {
const token_dt_o = state.push('dt_open', 'dt', 1)
token_dt_o.map = [dtLine, dtLine]
const token_i = state.push('inline', '', 0)
token_i.map = [dtLine, dtLine]
token_i.content = state.getLines(dtLine, dtLine + 1, state.blkIndent, false).trim()
token_i.children = []
state.push('dt_close', 'dt', -1)
for (;;) {
const itemTokIdx = state.tokens.length
const token_dd_o = state.push('dd_open', 'dd', 1)
const itemLines = [nextLine, 0]
token_dd_o.map = itemLines
let pos = contentStart
const max = state.eMarks[ddLine]
let offset = state.sCount[ddLine] + contentStart - (state.bMarks[ddLine] + state.tShift[ddLine])
while (pos < max) {
const ch = state.src.charCodeAt(pos)
if (isSpace(ch)) {
if (ch === 0x09) {
offset += 4 - offset % 4
} else {
offset++
}
} else {
break
}
pos++
}
contentStart = pos
const oldTight = state.tight
const oldIndent = state.blkIndent
const oldTShift = state.tShift[ddLine]
const oldSCount = state.sCount[ddLine]
const oldParentType = state.parentType
state.blkIndent = state.sCount[ddLine] + 2
state.tShift[ddLine] = contentStart - state.bMarks[ddLine]
state.sCount[ddLine] = offset
state.tight = true
state.parentType = 'deflist'
state.env[ddDepthKey] = (state.env[ddDepthKey] || 0) + 1
state.md.block.tokenize(state, ddLine, endLine, true)
state.env[ddDepthKey]--
if (termIsTight) {
markTightParagraph(state, itemTokIdx, state.tokens.length, token_dd_o.level + 1)
}
state.tShift[ddLine] = oldTShift
state.sCount[ddLine] = oldSCount
state.tight = oldTight
state.parentType = oldParentType
state.blkIndent = oldIndent
state.push('dd_close', 'dd', -1)
itemLines[1] = nextLine = state.line
if (nextLine >= endLine) { break OUTER }
if (state.sCount[nextLine] < state.blkIndent) { break OUTER }
contentStart = skipMarker(state, nextLine)
if (contentStart < 0) { break }
ddLine = nextLine
// go to the next loop iteration:
// insert DD tag and repeat checking
}
// EOF, empty and outdented lines have already exited through OUTER above.
// Keep these checks as guards for non-standard block parser states.
if (nextLine >= endLine) { break }
dtLine = nextLine
if (state.isEmpty(dtLine)) { break }
if (state.sCount[dtLine] < state.blkIndent) { break }
ddLine = dtLine + 1
if (ddLine >= endLine) { break }
const skippedEmptyLine = state.isEmpty(ddLine)
if (skippedEmptyLine) { ddLine++ }
// A trailing blank reaches EOF here, with the same result as above.
if (ddLine >= endLine) { break }
// Nested block tokenizers normally stop before an outdented line.
if (state.sCount[ddLine] < state.blkIndent) { break }
contentStart = skipMarker(state, ddLine)
if (contentStart < 0) { break }
termIsTight = !skippedEmptyLine
// go to the next loop iteration:
// insert DT and DD tags and repeat checking
}
// Finilize list
state.push('dl_close', 'dl', -1)
listLines[1] = nextLine
state.line = nextLine
return true
}
md.block.ruler.before('paragraph', 'deflist', deflist, { alt: ['paragraph', 'reference', 'blockquote'] })
};