/
index.js
66 lines (56 loc) · 1.92 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
const posthtml = require('posthtml')
const {get, omit, has, merge} = require('lodash')
const defaultFilters = require('./defaultFilters')
const PostCSS = require('../../generators/postcss')
const posthtmlContent = require('posthtml-content')
const Tailwind = require('../../generators/tailwindcss')
const safeClassNames = require('posthtml-safe-class-names')
const defaultConfig = require('../../generators/posthtml/defaultConfig')
module.exports = async (html, config = {}, direct = false) => {
const filters = direct ?
merge(defaultFilters, config) :
merge(defaultFilters, get(config, 'filters', {}))
const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
/**
* Compile CSS in <style {post|tailwind}css> tags
*/
const maizzleConfig = omit(config, ['build.tailwind.css', 'css'])
const tailwindConfig = get(config, 'build.tailwind.config', 'tailwind.config.js')
filters.postcss = css => PostCSS.process(css, maizzleConfig)
filters.tailwindcss = css => Tailwind.compile(css, html, tailwindConfig, maizzleConfig)
const posthtmlPlugins = [
styleDataEmbed(),
posthtmlContent(filters)
]
if (get(config, 'safeClassNames') !== false) {
posthtmlPlugins.push(safeClassNames({
replacements: {
'{': '{',
'}': '}'
}
}))
}
return posthtml(posthtmlPlugins)
.process(html, posthtmlOptions)
.then(result => result.html)
}
/**
* Prevent CSS inlining
*
* Add a `data-embed` attribute to <style> tags that we want to preserve.
* Can be used for HTML email client targeting hacks.
*/
const styleDataEmbed = () => tree => {
const process = node => {
if (
node.tag === 'style'
&& node.attrs
&& (has(node.attrs, 'preserve') || has(node.attrs, 'embed'))) {
node.attrs = {...node.attrs, 'data-embed': true}
node.attrs.preserve = false
node.attrs.embed = false
}
return node
}
return tree.walk(process)
}