/
posthtml.js
67 lines (60 loc) · 1.81 KB
/
posthtml.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
const fm = require('front-matter')
const posthtml = require('posthtml')
const {get, merge} = require('lodash')
const fetch = require('posthtml-fetch')
const layouts = require('posthtml-extend')
const modules = require('posthtml-modules')
const expressions = require('posthtml-expressions')
module.exports = async (html, config) => {
const layoutsOptions = get(config, 'build.layouts', {})
const modulesOptions = get(config, 'build.components', {})
// Fake `from` option so we can reference modules relatively
const modulesRoot = modulesOptions.root || './'
const modulesFrom = modulesOptions.from || `${modulesRoot}/fake`
const posthtmlOptions = get(config, 'build.posthtml.options', {})
const posthtmlPlugins = get(config, 'build.posthtml.plugins', [])
const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {}))
const locals = merge(
get(expressionsOptions, 'locals', {}),
get(config, 'locals', {}),
{page: config}
)
const fetchPlugin = fetch(
merge(
{
expressions: merge({...expressionsOptions, locals})
},
get(config, 'build.posthtml.fetch', {})
)
)
return posthtml([
fetchPlugin,
expressions({...expressionsOptions, locals}),
layouts(
merge(
{
strict: false,
expressions: merge({...expressionsOptions, locals})
},
layoutsOptions
)
),
modules({
expressions: expressionsOptions,
parser: posthtmlOptions,
attributeAsLocals: true,
from: modulesFrom,
root: modulesRoot,
tag: 'component',
attribute: 'src',
plugins: [
fetchPlugin
],
locals,
...modulesOptions
}),
...posthtmlPlugins
])
.process(html, {...posthtmlOptions})
.then(result => fm(result.html).body)
}