/
to-string.js
69 lines (51 loc) · 1.89 KB
/
to-string.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
const fm = require('front-matter')
const {get, merge} = require('lodash')
const posthtml = require('../posthtml')
const Tailwind = require('../tailwindcss')
const Transformers = require('../../transformers')
const posthtmlMSO = require('../../transformers/posthtmlMso')
const Config = require('../config')
module.exports = async (html, options) => {
process.env.NODE_ENV = get(options, 'maizzle.env', 'local')
if (typeof html !== 'string') {
throw new TypeError(`first argument must be an HTML string, received ${html}`)
}
if (html.length === 0) {
throw new RangeError('received empty string')
}
const fileConfig = get(options, 'useFileConfig')
? await Config.getMerged(process.env.NODE_ENV)
: {}
let config = merge(fileConfig, get(options, 'maizzle', {}))
const tailwindConfig = get(options, 'tailwind.config', {})
const cssString = get(options, 'tailwind.css', '')
const {frontmatter} = fm(html)
html = `---\n${frontmatter}\n---\n\n${fm(html).body}`
config = merge({applyTransformers: true}, config, fm(html).attributes)
if (typeof get(options, 'tailwind.compiled') === 'string') {
config.css = options.tailwind.compiled
} else {
config.css = await Tailwind.compile(cssString, html, tailwindConfig, config)
}
if (options && typeof options.beforeRender === 'function') {
html = await options.beforeRender(html, config)
}
html = await posthtml(html, config)
while (Object.keys(fm(html).attributes).length > 0) {
html = fm(html).body
}
if (options && typeof options.afterRender === 'function') {
html = await options.afterRender(html, config)
}
if (config.applyTransformers) {
html = await Transformers.process(html, config)
}
if (options && typeof options.afterTransformers === 'function') {
html = await options.afterTransformers(html, config)
}
html = await posthtmlMSO(html, config)
return {
html,
config
}
}