forked from arve0/markdown-it-attrs
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
230 lines (189 loc) · 5.16 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict'
/* eslint-disable no-cond-assign */
var tagExpr = /^<!-- ?\{(?:([a-z0-9]+)(\^[0-9]*)?: ?)?(.*)\} ?-->\n?$/
module.exports = function attributes (md) {
md.core.ruler.push('curly_attributes', curlyAttrs)
}
/*
* List of tag -> token type mappings. Eg, `<li>` is `list_item_open`.
*/
var opening = {
li: ['list_item'],
ul: ['bullet_list'],
p: ['paragraph'],
ol: ['ordered_list'],
blockquote: ['blockquote'],
h1: ['heading'],
h2: ['heading'],
h3: ['heading'],
h4: ['heading'],
h5: ['heading'],
h6: ['heading'],
a: ['link'],
code: ['code_inline', 'code_block', 'fence']
}
var selfClosing = {
hr: true,
image: true
}
/**
* ...
*/
function curlyAttrs (state) {
var tokens = state.tokens
var omissions = []
var parent, m
var stack = { len: 0, contents: [], types: {} }
tokens.forEach(function (token, i) {
// Save breadcrumbs so html_block will pick it up
if (isOpener(token.type) || selfClosing[token.type]) {
spush(stack, token)
}
// "# Hello\n<!--{.classname}-->"
// ...sequence of [heading_open, inline, heading_close, html_block]
if (token.type === 'html_block') {
m = token.content.match(tagExpr)
if (!m) return
parent = findParent(stack, m[1], m[2])
if (parent && applyToToken(parent, m[3])) {
omissions.unshift(i)
}
}
// "# Hello <!--{.classname} -->"
// { type: 'inline', children: { ..., '<!--{...}-->' } }
if (token.type === 'inline') {
curlyInline(token.children, stack)
}
})
// Remove <!--...--> html_block tokens
omissions.forEach(function (idx) { return tokens.splice(idx, 1) })
}
/**
* Internal: checks in a token type is a block opener
*/
function isOpener (type) {
return type.match(/_(open|start)$/) ||
type === 'fence' || type === 'code_block'
}
/**
* Internal: Run through inline and stuff
*/
function curlyInline (children, stack) {
var lastText, m, parent
// Keep a list of sub-tokens to be removed
var omissions = []
children.forEach(function (child, i) {
if (isOpener(child.type) ||
selfClosing[child.type] ||
child.type === 'code_inline') {
spush(stack, child)
}
// Decorate tags are found
if (m = child.content.match(tagExpr)) {
var tag = m[1]
var depth = m[2]
var attrs = m[3]
// Remove the comment, then remove the extra space
parent = findParent(stack, tag, depth)
if (parent && applyToToken(parent, attrs)) {
omissions.unshift(i)
if (lastText) trimRight(lastText, 'content')
}
}
if (child.type === 'text') lastText = child
})
// Remove them in a separate step so we don't
omissions.forEach(function (idx) {
children.splice(idx, 1)
})
}
/**
* Private: given a list of tokens `list` and `lastParent`, find the one that
* matches `tag`.
*/
function findParent (stack, tag, depth) {
if (!tag) return stack.last
if (depth === '^') {
depth = 1
} else if (typeof depth === 'string') { /* '^2' */
depth = +depth.substr(1)
} else {
depth = 0
}
var targets = opening[tag.toLowerCase()] || [tag.toLowerCase()]
var target = targets.filter(function (target) {
return stack.types[target]
})
var list = stack.types[target]
if (!list) return // Can't find tag `tag`
return list[list.length - 1 - depth]
}
/**
* Private: trim the right
*/
function trimRight (obj, attr) {
obj[attr] = obj[attr].replace(/\s*$/, '')
}
/**
* Private: apply tag to token
*
* applyToToken(token, '.classname')
*/
function applyToToken (token, attrs) {
var m
var todo = []
while (attrs.length > 0) {
if (m = attrs.match(/^\s*\.([a-zA-Z0-9\-_]+)/)) {
todo.push([ 'class', m[1], { append: true } ])
shift()
} else if (m = attrs.match(/^\s*#([a-zA-Z0-9\-_]+)/)) {
todo.push([ 'id', m[1] ])
shift()
} else if (m = attrs.match(/^\s*([a-zA-Z0-9\-_]+)="([^"]*)"/)) {
todo.push([ m[1], m[2] ])
shift()
} else if (m = attrs.match(/^\s*([a-zA-Z0-9\-_]+)='([^']*)'/)) {
todo.push([ m[1], m[2] ])
shift()
} else if (m = attrs.match(/^\s*([a-zA-Z0-9\-_]+)=([^ ]*)/)) {
todo.push([ m[1], m[2] ])
shift()
} else if (m = attrs.match(/^\s*([a-zA-Z0-9\-_]+)/)) {
todo.push([ m[1], '' ])
shift()
} else if (m = attrs.match(/^\s+/)) {
shift()
} else {
return
}
}
todo.forEach(function (args) { setAttr.apply(this, [token].concat(args)) })
return true
function shift () {
attrs = attrs.substr(m[0].length)
}
}
/**
* Private: sets an attribute `attr` to `value` in a token. If `options.append`
* is true, append to the old value instead of overwriting it.
*/
function setAttr (token, attr, value, options) {
var idx = token.attrIndex(attr)
if (idx === -1) {
token.attrPush([ attr, value ])
} else if (options && options.append) {
token.attrs[idx][1] =
token.attrs[idx][1] + ' ' + value
} else {
token.attrs[idx][1] = value
}
}
/**
* Private: pushes a token to the stack
*/
function spush (stack, token) {
var type = token.type.replace(/_(open|start)$/, '')
if (!stack.types[type]) { stack.types[type] = [] }
stack.types[type].push(token)
stack.last = token
}