/
index.js
68 lines (53 loc) · 1.51 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
const babel = require('babel-core')
const pug = require('pug')
const mixins = [
`
mixin skip(id)
.__fig-skip(id=id)
block
`
]
module.exports = (input, opts = {}) => {
const res = {}
const lines = input.split(/\r?\n/)
const tagContent = tag => {
let index = lines.findIndex(x => x.indexOf(tag) === 0)
if (index === -1) {
return ''
}
index += 1
const endIndex = lines.slice(index)
.findIndex(x => /^\S/.test(x))
const content = (endIndex === -1) ?
lines.slice(index) :
lines.slice(index, index + endIndex)
// Slice off leading indentation if needed
if (content.length > 0) {
const indentIndex = content[0].search(/\S/)
content.forEach((x, i) => {
content[i] = x.slice(indentIndex)
})
}
content.unshift(lines[index - 1].slice(tag.length + 1))
return content.join('\n').trim()
}
// Template
const mixinString = mixins.map(x => x.trim()).join('\n') + '\n'
const compiled = pug.compileClient(mixinString + tagContent('template'), {
filename: opts.filePath || 'Pug',
debug: Boolean(opts.debug),
compileDebug: Boolean(opts.debug)
})
// Wrap string of pug & its runtime into a function
res.template = Function('locals', // eslint-disable-line no-new-func
compiled + '\nreturn template(locals);')
// Style
res.style = tagContent('style').split('\n').join('').split('\t').join('')
// Script
res.script = babel.transform(tagContent('script'), {
presets: require('babel-preset-es2015')
}).code
// Name
res.name = tagContent('label') || opts.defaultName
return res
}