-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
277 lines (249 loc) · 5.95 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict'
/*
* remark-macro
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const macroRegex = /^(\s{2})?\[(\w+)(.*)?\](?!\()\n?/
const propBuilder = require('./prop')
const visit = require('unist-util-visit')
/**
* Returns a bad node. This will be later used to
* show linting errors
*
* @method badNode
*
* @param {String} message
*
* @return {Object}
*/
function badNode (message) {
return {
type: 'BadMacroNode',
data: {
hName: 'div',
hChildren: [
{
type: 'text',
value: message
}
]
}
}
}
/**
* Used to detect bad nodes and report them to
* the linter
*
* @method linterFn
*
* @param {Object} tree
* @param {Object} file
*
* @return {void}
*/
function linterFn (tree, file) {
visit(tree, 'BadMacroNode', visitor)
function visitor (node) {
const message = file.message(node.data.hChildren[0].value, node.position.start)
message.fatal = true
}
}
/**
* Processes the inline macros
*
* @method processInline
*
* @param {Function} eat
* @param {String} value
* @param {String} options.$
* @param {String} options.spaces
* @param {String} options.macroName
* @param {Object} options.macro
* @param {String} options.props
* @param {Object} macroFnPayload
*
* @return {void}
*/
function processInline (eat, value, { $, spaces, macroName, macro, props }, macroFnPayload) {
const propsHash = props ? propBuilder(props) : {}
const astNode = macro.fn(propsHash, macroFnPayload)
astNode ? eat($)(astNode) : eat($)
}
/**
* Processes the block node
*
* @method processBlock
*
* @param {Function} eat
* @param {String} value
* @param {String} options.$
* @param {String} options.spaces
* @param {String} options.macroName
* @param {Object} options.macro
* @param {String} options.props
* @param {Object} macroFnPayload
*
* @return {void}
*/
function processBlock (eat, value, { $, spaces, macroName, macro, props }, macroFnPayload) {
/**
* Keeping a hold whether the tag was ever closed
* or not
*
* @type {Boolean}
*/
let isClosed = false
/**
* Body is everything including opening/closing macro
* tags and the inner content
*
* @type {Array}
*/
const body = []
/**
* Children is the inner content of the macro
*
* @type {Array}
*/
const children = []
const lines = value.split('\n')
/**
* Loop until the ending block is found or the
* content is over
*/
while (lines.length) {
const line = lines.shift()
body.push(line)
/**
* Found ending tag. So break
* the loop
*/
if (`${line}` === `${spaces}[/${macroName}]`) {
isClosed = true
break
}
/**
* Push the lines, unless the line is same as the macro
* starting block
*/
if (!line.startsWith(`${spaces}[${macroName}`)) {
children.push(line.replace(spaces, ''))
}
}
/**
* Done with all the content, but the tag was never
* closed
*/
if (!isClosed) {
eat($)(badNode(`Unclosed macro: ${macroName}`))
return
}
/**
* Converting props string to an object
*/
const propsHash = props ? propBuilder(props) : {}
const astNode = macro.fn(children.join('\n'), propsHash, macroFnPayload)
astNode ? eat(body.join('\n'))(astNode) : eat(body.join('\n'))
}
module.exports = function () {
/**
* Hash of registered macros
*
* @type {Object}
*/
const macros = {}
function transformNodes (eat, value, silent) {
/**
* Regex match for each line can be pretty expensive, this way
* we return right away when line doesn't start with a
* macro node
*/
if (!value.trim().startsWith('[')) {
return
}
const match = macroRegex.exec(value)
if (!match || match.index !== 0 || silent) {
return
}
const $ = match[0]
const spaces = typeof (match[1]) === 'undefined' ? '' : match[1]
const macroName = match[2].trim()
const props = match[3]
const macro = macros[macroName]
if (!macro) {
return
}
/**
* Payload to be passed to the macro closure
*/
const macroFnPayload = {
transformer: this,
eat,
badNode
}
if (macro.inline) {
return processInline(eat, value, { $, spaces, macroName, props, macro }, macroFnPayload)
}
return processBlock(eat, value, { $, spaces, macroName, props, macro }, macroFnPayload)
}
return {
/**
* Adds a new macro to the macros list
*
* @method addMacro
*
* @param {String} name
* @param {Function} fn
* @param {Boolean} inline
*
* @example
* ```
* addMacro('alert', function (content, props, { transfomer, eat }) {
* return {
* type: 'AlertNode',
* data: {
* hName: 'div',
* children: transfomer.tokenizeBlock(content, eat.now()),
* properties: {
* className: ['']
* }
* }
* }
* })
* ```
*/
addMacro (name, fn, inline) {
if (macros[name]) {
throw new Error(`Cannot redefine the macro ${name}. One already exists`)
}
if (typeof (fn) !== 'function') {
throw new Error('addMacro expects 2nd argument to be a function')
}
macros[name] = { fn: fn, inline: inline || false }
return this
},
/**
* The method to the passed to remark that simply
* hoosk into the blockTokenizers
*
* @method transform
*
* @return {void}
*
* @example
* ```
* remark.use(macro.transformer)
* ```
*/
transformer () {
const { blockMethods, blockTokenizers } = this.Parser.prototype
blockMethods.splice(blockMethods.indexOf('paragraph'), 0, 'macro')
blockTokenizers.macro = transformNodes
return linterFn
}
}
}