@maizzle/framework 4.0.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +9 -9
- package/.github/workflows/nodejs.yml +28 -28
- package/LICENSE +21 -21
- package/bin/maizzle +3 -3
- package/package.json +2 -2
- package/src/generators/config.js +32 -32
- package/src/generators/output/index.js +4 -4
- package/src/generators/output/to-disk.js +208 -208
- package/src/generators/output/to-string.js +67 -67
- package/src/generators/postcss.js +29 -29
- package/src/generators/posthtml.js +66 -66
- package/src/index.js +17 -17
- package/src/transformers/attributeToStyle.js +90 -90
- package/src/transformers/baseUrl.js +69 -69
- package/src/transformers/extraAttributes.js +26 -26
- package/src/transformers/filters/defaultFilters.js +126 -126
- package/src/transformers/filters/index.js +55 -55
- package/src/transformers/inlineCss.js +37 -37
- package/src/transformers/markdown.js +19 -19
- package/src/transformers/minify.js +21 -21
- package/src/transformers/plaintext.js +23 -23
- package/src/transformers/posthtmlMso.js +10 -10
- package/src/transformers/prettify.js +27 -27
- package/src/transformers/preventWidows.js +13 -13
- package/src/transformers/removeAttributes.js +17 -17
- package/src/transformers/removeInlineBackgroundColor.js +52 -52
- package/src/transformers/removeInlineSizes.js +41 -41
- package/src/transformers/replaceStrings.js +14 -14
- package/src/transformers/safeClassNames.js +24 -24
- package/src/transformers/shorthandInlineCSS.js +19 -19
- package/src/transformers/sixHex.js +33 -33
- package/src/transformers/urlParameters.js +17 -17
- package/src/utils/helpers.js +17 -17
- package/test/expected/posthtml/extend-template.html +2 -2
- package/test/expected/posthtml/fetch.html +5 -5
- package/test/expected/posthtml/layout.html +3 -3
- package/test/expected/transformers/base-url.html +99 -99
- package/test/fixtures/posthtml/extend-template.html +7 -7
- package/test/fixtures/posthtml/fetch.html +9 -9
- package/test/fixtures/posthtml/layout.html +11 -11
- package/test/fixtures/transformers/base-url.html +101 -101
- package/test/stubs/assets/foo.bar +1 -1
- package/test/stubs/breaking/bad.html +5 -5
- package/test/stubs/config/config.js +10 -10
- package/test/stubs/config/config.maizzle-ci.js +10 -10
- package/test/stubs/data.json +14 -14
- package/test/stubs/events/before-create.html +1 -1
- package/test/stubs/layouts/basic.html +1 -1
- package/test/stubs/layouts/full.html +12 -12
- package/test/stubs/layouts/template.html +5 -5
- package/test/stubs/main.css +5 -5
- package/test/stubs/tailwind/content-source.html +1 -1
- package/test/stubs/tailwind/tailwind.css +3 -3
- package/test/stubs/template.html +10 -10
- package/test/test-posthtml.js +72 -72
- package/test/test-todisk.js +511 -511
- package/test/test-transformers.js +1 -0
- package/xo.config.js +22 -22
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
const fm = require('front-matter')
|
|
2
|
-
const {get, merge} = require('lodash')
|
|
3
|
-
const posthtml = require('../posthtml')
|
|
4
|
-
const Tailwind = require('../tailwindcss')
|
|
5
|
-
const Transformers = require('../../transformers')
|
|
6
|
-
const posthtmlMSO = require('../../transformers/posthtmlMso')
|
|
7
|
-
const Config = require('../config')
|
|
8
|
-
|
|
9
|
-
module.exports = async (html, options) => {
|
|
10
|
-
process.env.NODE_ENV = get(options, 'maizzle.env', 'local')
|
|
11
|
-
|
|
12
|
-
if (typeof html !== 'string') {
|
|
13
|
-
throw new TypeError(`first argument must be an HTML string, received ${html}`)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (html.length === 0) {
|
|
17
|
-
throw new RangeError('received empty string')
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const fileConfig = await Config.getMerged(process.env.NODE_ENV)
|
|
21
|
-
|
|
22
|
-
let config = merge(fileConfig, get(options, 'maizzle', {}))
|
|
23
|
-
|
|
24
|
-
const tailwindConfig = get(options, 'tailwind.config', {})
|
|
25
|
-
const cssString = get(options, 'tailwind.css', '')
|
|
26
|
-
|
|
27
|
-
const {frontmatter} = fm(html)
|
|
28
|
-
|
|
29
|
-
html = `---\n${frontmatter}\n---\n\n${fm(html).body}`
|
|
30
|
-
|
|
31
|
-
config = merge({applyTransformers: true}, config, fm(html).attributes)
|
|
32
|
-
|
|
33
|
-
if (typeof get(options, 'tailwind.compiled') === 'string') {
|
|
34
|
-
config.css = options.tailwind.compiled
|
|
35
|
-
} else {
|
|
36
|
-
config.css = await Tailwind.compile(cssString, html, tailwindConfig, config)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (options && typeof options.beforeRender === 'function') {
|
|
40
|
-
html = await options.beforeRender(html, config)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
html = await posthtml(html, config)
|
|
44
|
-
|
|
45
|
-
while (Object.keys(fm(html).attributes).length > 0) {
|
|
46
|
-
html = fm(html).body
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (options && typeof options.afterRender === 'function') {
|
|
50
|
-
html = await options.afterRender(html, config)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (config.applyTransformers) {
|
|
54
|
-
html = await Transformers.process(html, config)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (options && typeof options.afterTransformers === 'function') {
|
|
58
|
-
html = await options.afterTransformers(html, config)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
html = await posthtmlMSO(html, config)
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
html,
|
|
65
|
-
config
|
|
66
|
-
}
|
|
67
|
-
}
|
|
1
|
+
const fm = require('front-matter')
|
|
2
|
+
const {get, merge} = require('lodash')
|
|
3
|
+
const posthtml = require('../posthtml')
|
|
4
|
+
const Tailwind = require('../tailwindcss')
|
|
5
|
+
const Transformers = require('../../transformers')
|
|
6
|
+
const posthtmlMSO = require('../../transformers/posthtmlMso')
|
|
7
|
+
const Config = require('../config')
|
|
8
|
+
|
|
9
|
+
module.exports = async (html, options) => {
|
|
10
|
+
process.env.NODE_ENV = get(options, 'maizzle.env', 'local')
|
|
11
|
+
|
|
12
|
+
if (typeof html !== 'string') {
|
|
13
|
+
throw new TypeError(`first argument must be an HTML string, received ${html}`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (html.length === 0) {
|
|
17
|
+
throw new RangeError('received empty string')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const fileConfig = await Config.getMerged(process.env.NODE_ENV)
|
|
21
|
+
|
|
22
|
+
let config = merge(fileConfig, get(options, 'maizzle', {}))
|
|
23
|
+
|
|
24
|
+
const tailwindConfig = get(options, 'tailwind.config', {})
|
|
25
|
+
const cssString = get(options, 'tailwind.css', '')
|
|
26
|
+
|
|
27
|
+
const {frontmatter} = fm(html)
|
|
28
|
+
|
|
29
|
+
html = `---\n${frontmatter}\n---\n\n${fm(html).body}`
|
|
30
|
+
|
|
31
|
+
config = merge({applyTransformers: true}, config, fm(html).attributes)
|
|
32
|
+
|
|
33
|
+
if (typeof get(options, 'tailwind.compiled') === 'string') {
|
|
34
|
+
config.css = options.tailwind.compiled
|
|
35
|
+
} else {
|
|
36
|
+
config.css = await Tailwind.compile(cssString, html, tailwindConfig, config)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (options && typeof options.beforeRender === 'function') {
|
|
40
|
+
html = await options.beforeRender(html, config)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
html = await posthtml(html, config)
|
|
44
|
+
|
|
45
|
+
while (Object.keys(fm(html).attributes).length > 0) {
|
|
46
|
+
html = fm(html).body
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (options && typeof options.afterRender === 'function') {
|
|
50
|
+
html = await options.afterRender(html, config)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (config.applyTransformers) {
|
|
54
|
+
html = await Transformers.process(html, config)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (options && typeof options.afterTransformers === 'function') {
|
|
58
|
+
html = await options.afterTransformers(html, config)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
html = await posthtmlMSO(html, config)
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
html,
|
|
65
|
+
config
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
const {get} = require('lodash')
|
|
3
|
-
const postcss = require('postcss')
|
|
4
|
-
const postcssImport = require('postcss-import')
|
|
5
|
-
const postcssNested = require('tailwindcss/nesting')
|
|
6
|
-
const mergeLonghand = require('postcss-merge-longhand')
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
process: async (css = '', maizzleConfig = {}, spinner = null) => {
|
|
10
|
-
const userFilePath = get(maizzleConfig, 'build.tailwind.css', path.join(process.cwd(), 'src/css/tailwind.css'))
|
|
11
|
-
|
|
12
|
-
return postcss([
|
|
13
|
-
postcssImport({path: path.dirname(userFilePath)}),
|
|
14
|
-
postcssNested(),
|
|
15
|
-
maizzleConfig.env === 'local' ? () => {} : mergeLonghand(),
|
|
16
|
-
...get(maizzleConfig, 'build.postcss.plugins', [])
|
|
17
|
-
])
|
|
18
|
-
.process(css, {from: undefined})
|
|
19
|
-
.then(result => result.css)
|
|
20
|
-
.catch(error => {
|
|
21
|
-
console.error(error)
|
|
22
|
-
if (spinner) {
|
|
23
|
-
spinner.stop()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
throw new Error(`PostCSS processing failed`)
|
|
27
|
-
})
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const {get} = require('lodash')
|
|
3
|
+
const postcss = require('postcss')
|
|
4
|
+
const postcssImport = require('postcss-import')
|
|
5
|
+
const postcssNested = require('tailwindcss/nesting')
|
|
6
|
+
const mergeLonghand = require('postcss-merge-longhand')
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
process: async (css = '', maizzleConfig = {}, spinner = null) => {
|
|
10
|
+
const userFilePath = get(maizzleConfig, 'build.tailwind.css', path.join(process.cwd(), 'src/css/tailwind.css'))
|
|
11
|
+
|
|
12
|
+
return postcss([
|
|
13
|
+
postcssImport({path: path.dirname(userFilePath)}),
|
|
14
|
+
postcssNested(),
|
|
15
|
+
maizzleConfig.env === 'local' ? () => {} : mergeLonghand(),
|
|
16
|
+
...get(maizzleConfig, 'build.postcss.plugins', [])
|
|
17
|
+
])
|
|
18
|
+
.process(css, {from: undefined})
|
|
19
|
+
.then(result => result.css)
|
|
20
|
+
.catch(error => {
|
|
21
|
+
console.error(error)
|
|
22
|
+
if (spinner) {
|
|
23
|
+
spinner.stop()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
throw new Error(`PostCSS processing failed`)
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
const fm = require('front-matter')
|
|
2
|
-
const posthtml = require('posthtml')
|
|
3
|
-
const {get, merge} = require('lodash')
|
|
4
|
-
const fetch = require('posthtml-fetch')
|
|
5
|
-
const layouts = require('posthtml-extend')
|
|
6
|
-
const modules = require('posthtml-modules')
|
|
7
|
-
const expressions = require('posthtml-expressions')
|
|
8
|
-
|
|
9
|
-
module.exports = async (html, config) => {
|
|
10
|
-
const layoutsOptions = get(config, 'build.layouts', {})
|
|
11
|
-
|
|
12
|
-
const modulesOptions = get(config, 'build.components', {})
|
|
13
|
-
// Fake `from` option so we can reference modules relatively
|
|
14
|
-
const modulesRoot = modulesOptions.root || './'
|
|
15
|
-
const modulesFrom = modulesOptions.from || `${modulesRoot}/fake`
|
|
16
|
-
|
|
17
|
-
const posthtmlOptions = get(config, 'build.posthtml.options', {})
|
|
18
|
-
const posthtmlPlugins = get(config, 'build.posthtml.plugins', [])
|
|
19
|
-
|
|
20
|
-
const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {}))
|
|
21
|
-
|
|
22
|
-
const locals = merge(
|
|
23
|
-
get(expressionsOptions, 'locals', {}),
|
|
24
|
-
get(config, 'locals', {}),
|
|
25
|
-
{page: config}
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
const fetchPlugin = fetch(
|
|
29
|
-
merge(
|
|
30
|
-
{
|
|
31
|
-
expressions: merge({...expressionsOptions, locals})
|
|
32
|
-
},
|
|
33
|
-
get(config, 'build.posthtml.fetch', {})
|
|
34
|
-
)
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
return posthtml([
|
|
38
|
-
fetchPlugin,
|
|
39
|
-
expressions({...expressionsOptions, locals}),
|
|
40
|
-
layouts(
|
|
41
|
-
merge(
|
|
42
|
-
{
|
|
43
|
-
strict: false,
|
|
44
|
-
expressions: merge({...expressionsOptions, locals})
|
|
45
|
-
},
|
|
46
|
-
layoutsOptions
|
|
47
|
-
)
|
|
48
|
-
),
|
|
49
|
-
modules({
|
|
50
|
-
parser: posthtmlOptions,
|
|
51
|
-
attributeAsLocals: true,
|
|
52
|
-
from: modulesFrom,
|
|
53
|
-
root: modulesRoot,
|
|
54
|
-
tag: 'component',
|
|
55
|
-
attribute: 'src',
|
|
56
|
-
plugins: [
|
|
57
|
-
fetchPlugin
|
|
58
|
-
],
|
|
59
|
-
locals,
|
|
60
|
-
...modulesOptions
|
|
61
|
-
}),
|
|
62
|
-
...posthtmlPlugins
|
|
63
|
-
])
|
|
64
|
-
.process(html, {...posthtmlOptions})
|
|
65
|
-
.then(result => fm(result.html).body)
|
|
66
|
-
}
|
|
1
|
+
const fm = require('front-matter')
|
|
2
|
+
const posthtml = require('posthtml')
|
|
3
|
+
const {get, merge} = require('lodash')
|
|
4
|
+
const fetch = require('posthtml-fetch')
|
|
5
|
+
const layouts = require('posthtml-extend')
|
|
6
|
+
const modules = require('posthtml-modules')
|
|
7
|
+
const expressions = require('posthtml-expressions')
|
|
8
|
+
|
|
9
|
+
module.exports = async (html, config) => {
|
|
10
|
+
const layoutsOptions = get(config, 'build.layouts', {})
|
|
11
|
+
|
|
12
|
+
const modulesOptions = get(config, 'build.components', {})
|
|
13
|
+
// Fake `from` option so we can reference modules relatively
|
|
14
|
+
const modulesRoot = modulesOptions.root || './'
|
|
15
|
+
const modulesFrom = modulesOptions.from || `${modulesRoot}/fake`
|
|
16
|
+
|
|
17
|
+
const posthtmlOptions = get(config, 'build.posthtml.options', {})
|
|
18
|
+
const posthtmlPlugins = get(config, 'build.posthtml.plugins', [])
|
|
19
|
+
|
|
20
|
+
const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {}))
|
|
21
|
+
|
|
22
|
+
const locals = merge(
|
|
23
|
+
get(expressionsOptions, 'locals', {}),
|
|
24
|
+
get(config, 'locals', {}),
|
|
25
|
+
{page: config}
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const fetchPlugin = fetch(
|
|
29
|
+
merge(
|
|
30
|
+
{
|
|
31
|
+
expressions: merge({...expressionsOptions, locals})
|
|
32
|
+
},
|
|
33
|
+
get(config, 'build.posthtml.fetch', {})
|
|
34
|
+
)
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
return posthtml([
|
|
38
|
+
fetchPlugin,
|
|
39
|
+
expressions({...expressionsOptions, locals}),
|
|
40
|
+
layouts(
|
|
41
|
+
merge(
|
|
42
|
+
{
|
|
43
|
+
strict: false,
|
|
44
|
+
expressions: merge({...expressionsOptions, locals})
|
|
45
|
+
},
|
|
46
|
+
layoutsOptions
|
|
47
|
+
)
|
|
48
|
+
),
|
|
49
|
+
modules({
|
|
50
|
+
parser: posthtmlOptions,
|
|
51
|
+
attributeAsLocals: true,
|
|
52
|
+
from: modulesFrom,
|
|
53
|
+
root: modulesRoot,
|
|
54
|
+
tag: 'component',
|
|
55
|
+
attribute: 'src',
|
|
56
|
+
plugins: [
|
|
57
|
+
fetchPlugin
|
|
58
|
+
],
|
|
59
|
+
locals,
|
|
60
|
+
...modulesOptions
|
|
61
|
+
}),
|
|
62
|
+
...posthtmlPlugins
|
|
63
|
+
])
|
|
64
|
+
.process(html, {...posthtmlOptions})
|
|
65
|
+
.then(result => fm(result.html).body)
|
|
66
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
const serve = require('./commands/serve')
|
|
2
|
-
const toFile = require('./commands/build')
|
|
3
|
-
const transformers = require('./transformers')
|
|
4
|
-
const toString = require('./functions/render')
|
|
5
|
-
const PostCSS = require('./generators/postcss')
|
|
6
|
-
const toPlaintext = require('./functions/plaintext')
|
|
7
|
-
const TailwindCSS = require('./generators/tailwindcss')
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
serve,
|
|
11
|
-
build: toFile,
|
|
12
|
-
...transformers,
|
|
13
|
-
render: toString,
|
|
14
|
-
postcss: PostCSS,
|
|
15
|
-
plaintext: toPlaintext,
|
|
16
|
-
tailwindcss: TailwindCSS
|
|
17
|
-
}
|
|
1
|
+
const serve = require('./commands/serve')
|
|
2
|
+
const toFile = require('./commands/build')
|
|
3
|
+
const transformers = require('./transformers')
|
|
4
|
+
const toString = require('./functions/render')
|
|
5
|
+
const PostCSS = require('./generators/postcss')
|
|
6
|
+
const toPlaintext = require('./functions/plaintext')
|
|
7
|
+
const TailwindCSS = require('./generators/tailwindcss')
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
serve,
|
|
11
|
+
build: toFile,
|
|
12
|
+
...transformers,
|
|
13
|
+
render: toString,
|
|
14
|
+
postcss: PostCSS,
|
|
15
|
+
plaintext: toPlaintext,
|
|
16
|
+
tailwindcss: TailwindCSS
|
|
17
|
+
}
|
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
const posthtml = require('posthtml')
|
|
2
|
-
const parseAttrs = require('posthtml-attrs-parser')
|
|
3
|
-
const {get, forEach, intersection, keys, isEmpty} = require('lodash')
|
|
4
|
-
|
|
5
|
-
module.exports = async (html, config = {}, direct = false) => {
|
|
6
|
-
const posthtmlOptions = get(config, 'build.posthtml.options', {})
|
|
7
|
-
const attributes = get(config, 'inlineCSS.attributeToStyle', false)
|
|
8
|
-
|
|
9
|
-
if (typeof attributes === 'boolean' && attributes) {
|
|
10
|
-
return posthtml([attributesToStyle()]).process(html, posthtmlOptions).then(result => result.html)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (Array.isArray(attributes) && !isEmpty(attributes)) {
|
|
14
|
-
return posthtml([attributesToStyle({attributes})]).process(html, posthtmlOptions).then(result => result.html)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (direct) {
|
|
18
|
-
return posthtml([
|
|
19
|
-
attributesToStyle({
|
|
20
|
-
attributes: Array.isArray(config) ? config : []
|
|
21
|
-
})
|
|
22
|
-
]).process(html).then(result => result.html)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return html
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const attributesToStyle = (options = {}) => tree => {
|
|
29
|
-
options.attributes = options.attributes || ['width', 'height', 'bgcolor', 'background', 'align', 'valign']
|
|
30
|
-
|
|
31
|
-
const process = node => {
|
|
32
|
-
const nodeAttributes = parseAttrs(node.attrs)
|
|
33
|
-
const matches = intersection(keys(nodeAttributes), options.attributes)
|
|
34
|
-
const nodeStyle = get(node.attrs, 'style')
|
|
35
|
-
const csstoInline = []
|
|
36
|
-
|
|
37
|
-
forEach(matches, attribute => {
|
|
38
|
-
let value = get(node.attrs, attribute)
|
|
39
|
-
|
|
40
|
-
switch (attribute) {
|
|
41
|
-
case 'bgcolor':
|
|
42
|
-
csstoInline.push(`background-color: ${value}`)
|
|
43
|
-
break
|
|
44
|
-
|
|
45
|
-
case 'background':
|
|
46
|
-
csstoInline.push(`background-image: url('${value}')`)
|
|
47
|
-
break
|
|
48
|
-
|
|
49
|
-
case 'width':
|
|
50
|
-
value = Number.parseInt(value, 10) + (value.match(/px|%/) || 'px')
|
|
51
|
-
csstoInline.push(`width: ${value}`)
|
|
52
|
-
break
|
|
53
|
-
|
|
54
|
-
case 'height':
|
|
55
|
-
value = Number.parseInt(value, 10) + (value.match(/px|%/) || 'px')
|
|
56
|
-
csstoInline.push(`height: ${value}`)
|
|
57
|
-
break
|
|
58
|
-
|
|
59
|
-
case 'align':
|
|
60
|
-
if (node.tag !== 'table') {
|
|
61
|
-
return csstoInline.push(`text-align: ${value}`)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (['left', 'right'].includes(value)) {
|
|
65
|
-
csstoInline.push(`float: ${value}`)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (value === 'center') {
|
|
69
|
-
csstoInline.push('margin-left: auto', 'margin-right: auto')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
break
|
|
73
|
-
|
|
74
|
-
case 'valign':
|
|
75
|
-
csstoInline.push(`vertical-align: ${value}`)
|
|
76
|
-
break
|
|
77
|
-
|
|
78
|
-
// No default
|
|
79
|
-
}
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
nodeAttributes.style = nodeStyle ? `${nodeStyle} ${csstoInline.join('; ')}` : `${csstoInline.join('; ')}`
|
|
83
|
-
|
|
84
|
-
node.attrs = nodeAttributes.compose()
|
|
85
|
-
|
|
86
|
-
return node
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return tree.walk(process)
|
|
90
|
-
}
|
|
1
|
+
const posthtml = require('posthtml')
|
|
2
|
+
const parseAttrs = require('posthtml-attrs-parser')
|
|
3
|
+
const {get, forEach, intersection, keys, isEmpty} = require('lodash')
|
|
4
|
+
|
|
5
|
+
module.exports = async (html, config = {}, direct = false) => {
|
|
6
|
+
const posthtmlOptions = get(config, 'build.posthtml.options', {})
|
|
7
|
+
const attributes = get(config, 'inlineCSS.attributeToStyle', false)
|
|
8
|
+
|
|
9
|
+
if (typeof attributes === 'boolean' && attributes) {
|
|
10
|
+
return posthtml([attributesToStyle()]).process(html, posthtmlOptions).then(result => result.html)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(attributes) && !isEmpty(attributes)) {
|
|
14
|
+
return posthtml([attributesToStyle({attributes})]).process(html, posthtmlOptions).then(result => result.html)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (direct) {
|
|
18
|
+
return posthtml([
|
|
19
|
+
attributesToStyle({
|
|
20
|
+
attributes: Array.isArray(config) ? config : []
|
|
21
|
+
})
|
|
22
|
+
]).process(html).then(result => result.html)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return html
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const attributesToStyle = (options = {}) => tree => {
|
|
29
|
+
options.attributes = options.attributes || ['width', 'height', 'bgcolor', 'background', 'align', 'valign']
|
|
30
|
+
|
|
31
|
+
const process = node => {
|
|
32
|
+
const nodeAttributes = parseAttrs(node.attrs)
|
|
33
|
+
const matches = intersection(keys(nodeAttributes), options.attributes)
|
|
34
|
+
const nodeStyle = get(node.attrs, 'style')
|
|
35
|
+
const csstoInline = []
|
|
36
|
+
|
|
37
|
+
forEach(matches, attribute => {
|
|
38
|
+
let value = get(node.attrs, attribute)
|
|
39
|
+
|
|
40
|
+
switch (attribute) {
|
|
41
|
+
case 'bgcolor':
|
|
42
|
+
csstoInline.push(`background-color: ${value}`)
|
|
43
|
+
break
|
|
44
|
+
|
|
45
|
+
case 'background':
|
|
46
|
+
csstoInline.push(`background-image: url('${value}')`)
|
|
47
|
+
break
|
|
48
|
+
|
|
49
|
+
case 'width':
|
|
50
|
+
value = Number.parseInt(value, 10) + (value.match(/px|%/) || 'px')
|
|
51
|
+
csstoInline.push(`width: ${value}`)
|
|
52
|
+
break
|
|
53
|
+
|
|
54
|
+
case 'height':
|
|
55
|
+
value = Number.parseInt(value, 10) + (value.match(/px|%/) || 'px')
|
|
56
|
+
csstoInline.push(`height: ${value}`)
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
case 'align':
|
|
60
|
+
if (node.tag !== 'table') {
|
|
61
|
+
return csstoInline.push(`text-align: ${value}`)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (['left', 'right'].includes(value)) {
|
|
65
|
+
csstoInline.push(`float: ${value}`)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (value === 'center') {
|
|
69
|
+
csstoInline.push('margin-left: auto', 'margin-right: auto')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
break
|
|
73
|
+
|
|
74
|
+
case 'valign':
|
|
75
|
+
csstoInline.push(`vertical-align: ${value}`)
|
|
76
|
+
break
|
|
77
|
+
|
|
78
|
+
// No default
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
nodeAttributes.style = nodeStyle ? `${nodeStyle} ${csstoInline.join('; ')}` : `${csstoInline.join('; ')}`
|
|
83
|
+
|
|
84
|
+
node.attrs = nodeAttributes.compose()
|
|
85
|
+
|
|
86
|
+
return node
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return tree.walk(process)
|
|
90
|
+
}
|