@maizzle/framework 4.8.8 → 5.0.0-beta.0

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.
Files changed (80) hide show
  1. package/bin/maizzle +3 -1
  2. package/package.json +64 -55
  3. package/src/commands/build.js +244 -19
  4. package/src/commands/serve.js +2 -197
  5. package/src/generators/plaintext.js +192 -91
  6. package/src/generators/render.js +128 -0
  7. package/src/index.js +45 -13
  8. package/src/{generators/posthtml → posthtml}/defaultComponentsConfig.js +6 -4
  9. package/src/{generators/posthtml → posthtml}/defaultConfig.js +1 -1
  10. package/src/posthtml/index.js +74 -0
  11. package/src/posthtml/plugins/expandLinkTag.js +36 -0
  12. package/src/server/client.js +181 -0
  13. package/src/server/index.js +383 -0
  14. package/src/server/routes/hmr.js +24 -0
  15. package/src/server/routes/index.js +38 -0
  16. package/src/server/views/error.html +83 -0
  17. package/src/server/views/index.html +24 -0
  18. package/src/server/websockets.js +27 -0
  19. package/src/transformers/addAttributes.js +30 -0
  20. package/src/transformers/attributeToStyle.js +30 -36
  21. package/src/transformers/baseUrl.js +56 -27
  22. package/src/transformers/comb.js +51 -0
  23. package/src/transformers/core.js +20 -0
  24. package/src/transformers/filters/defaultFilters.js +90 -71
  25. package/src/transformers/filters/index.js +14 -78
  26. package/src/transformers/index.js +268 -63
  27. package/src/transformers/inline.js +240 -0
  28. package/src/transformers/markdown.js +13 -14
  29. package/src/transformers/minify.js +21 -16
  30. package/src/transformers/posthtmlMso.js +13 -8
  31. package/src/transformers/prettify.js +16 -15
  32. package/src/transformers/preventWidows.js +32 -28
  33. package/src/transformers/removeAttributes.js +19 -19
  34. package/src/transformers/replaceStrings.js +30 -9
  35. package/src/transformers/safeClassNames.js +24 -24
  36. package/src/transformers/shorthandCss.js +22 -0
  37. package/src/transformers/sixHex.js +17 -17
  38. package/src/transformers/urlParameters.js +18 -16
  39. package/src/transformers/useAttributeSizes.js +65 -0
  40. package/src/utils/getConfigByFilePath.js +124 -0
  41. package/src/utils/node.js +68 -0
  42. package/src/utils/string.js +117 -0
  43. package/types/build.d.ts +117 -57
  44. package/types/components.d.ts +130 -112
  45. package/types/config.d.ts +454 -242
  46. package/types/css/inline.d.ts +234 -0
  47. package/types/css/purge.d.ts +125 -0
  48. package/types/events.d.ts +5 -105
  49. package/types/index.d.ts +148 -116
  50. package/types/markdown.d.ts +20 -18
  51. package/types/minify.d.ts +122 -120
  52. package/types/plaintext.d.ts +46 -52
  53. package/types/posthtml.d.ts +103 -136
  54. package/types/render.d.ts +0 -117
  55. package/types/urlParameters.d.ts +21 -20
  56. package/types/widowWords.d.ts +9 -7
  57. package/src/functions/plaintext.js +0 -5
  58. package/src/functions/render.js +0 -5
  59. package/src/generators/config.js +0 -52
  60. package/src/generators/output/index.js +0 -4
  61. package/src/generators/output/to-disk.js +0 -254
  62. package/src/generators/output/to-string.js +0 -73
  63. package/src/generators/postcss.js +0 -23
  64. package/src/generators/posthtml/index.js +0 -75
  65. package/src/generators/tailwindcss.js +0 -157
  66. package/src/transformers/extraAttributes.js +0 -33
  67. package/src/transformers/inlineCss.js +0 -42
  68. package/src/transformers/removeInlineBackgroundColor.js +0 -57
  69. package/src/transformers/removeInlineSizes.js +0 -45
  70. package/src/transformers/removeInlinedSelectors.js +0 -100
  71. package/src/transformers/removeUnusedCss.js +0 -48
  72. package/src/transformers/shorthandInlineCSS.js +0 -26
  73. package/src/utils/helpers.js +0 -13
  74. package/types/baseUrl.d.ts +0 -79
  75. package/types/fetch.d.ts +0 -143
  76. package/types/inlineCss.d.ts +0 -207
  77. package/types/layouts.d.ts +0 -39
  78. package/types/removeUnusedCss.d.ts +0 -115
  79. package/types/tailwind.d.ts +0 -22
  80. package/types/templates.d.ts +0 -181
@@ -1,20 +1,19 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge} = require('lodash')
3
- const markdown = require('posthtml-markdownit')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import md from 'posthtml-markdownit'
4
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
5
 
6
- module.exports = async (html, config = {}, direct = false) => {
7
- if (get(config, 'markdown') === false) {
8
- return html
9
- }
10
-
11
- const userMarkdownOptions = direct ? config : get(config, 'markdown', {})
12
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
13
- const markdownOptions = merge({markdownit: {html: true}}, userMarkdownOptions)
6
+ export async function markdown(html = '', options = {}, posthtmlOptions = {}) {
7
+ /**
8
+ * Automatically wrap in <md> tag, unless manual mode is enabled
9
+ * With manual mode, user must wrap the markdown content in a <md> tag
10
+ * https://github.com/posthtml/posthtml-markdownit#usage
11
+ */
12
+ html = options.manual ? html : `<md>${html}</md>`
14
13
 
15
14
  return posthtml([
16
- markdown({...markdownOptions})
15
+ md(options)
17
16
  ])
18
- .process(html, posthtmlOptions)
17
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
19
18
  .then(result => result.html)
20
19
  }
@@ -1,21 +1,26 @@
1
- const {crush} = require('html-crush')
2
- const {get, isEmpty} = require('lodash')
1
+ import posthtml from 'posthtml'
2
+ import { crush } from 'html-crush'
3
+ import { defu as merge } from 'defu'
4
+ import { render } from 'posthtml-render'
5
+ import { parser as parse } from 'posthtml-parser'
6
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
3
7
 
4
- module.exports = async (html, config = {}, direct = false) => {
5
- if (get(config, 'minify') === false) {
6
- return html
7
- }
8
-
9
- config = direct ? {
10
- lineLengthLimit: 500,
11
- removeIndentations: true,
8
+ const posthtmlPlugin = (options = {}) => tree => {
9
+ options = merge(options, {
12
10
  removeLineBreaks: true,
13
- ...config
14
- } : get(config, 'minify', {})
11
+ })
12
+
13
+ const { result: html } = crush(render(tree), options)
14
+
15
+ return parse(html)
16
+ }
15
17
 
16
- if (!isEmpty(config)) {
17
- html = crush(html, {removeLineBreaks: true, ...config}).result
18
- }
18
+ export default posthtmlPlugin
19
19
 
20
- return html
20
+ export async function minify(html = '', options = {}, posthtmlOptions = {}) {
21
+ return posthtml([
22
+ posthtmlPlugin(options)
23
+ ])
24
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
25
+ .then(result => result.html)
21
26
  }
@@ -1,11 +1,16 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge} = require('lodash')
3
- const outlook = require('posthtml-mso')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import posthtmlMso from 'posthtml-mso'
4
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
5
 
6
- module.exports = async (html, config) => {
7
- const outlookOptions = get(config, 'build.posthtml.outlook', {})
8
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
6
+ export default function posthtmlPlugin(options = {}) {
7
+ return posthtmlMso(options)
8
+ }
9
9
 
10
- return posthtml([outlook({...outlookOptions})]).process(html, posthtmlOptions).then(result => result.html)
10
+ export async function useMso(html = '', options = {}, posthtmlOptions = {}) {
11
+ return posthtml([
12
+ posthtmlPlugin(options)
13
+ ])
14
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
15
+ .then(result => result.html)
11
16
  }
@@ -1,8 +1,10 @@
1
- /* eslint-disable camelcase */
2
- const pretty = require('pretty')
3
- const {get, merge, isEmpty, isObject} = require('lodash')
1
+ import pretty from 'pretty'
2
+ import posthtml from 'posthtml'
3
+ import { defu as merge } from 'defu'
4
+ import { render } from 'posthtml-render'
5
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
4
6
 
5
- module.exports = async (html, config = {}, direct = false) => {
7
+ const posthtmlPlugin = (options = {}) => tree => {
6
8
  const defaultConfig = {
7
9
  space_around_combinator: true, // Preserve space around CSS selector combinators
8
10
  newline_between_rules: false, // Remove empty lines between CSS rules
@@ -10,18 +12,17 @@ module.exports = async (html, config = {}, direct = false) => {
10
12
  extra_liners: [] // Don't add extra new line before any tag
11
13
  }
12
14
 
13
- config = direct ? config : get(config, 'prettify')
15
+ const config = merge(options, defaultConfig)
14
16
 
15
- // Don't prettify if not explicitly enabled in config
16
- if (!config || (isObject(config) && isEmpty(config))) {
17
- return html
18
- }
19
-
20
- if (typeof config === 'boolean' && config) {
21
- return pretty(html, defaultConfig)
22
- }
17
+ return pretty(render(tree), config)
18
+ }
23
19
 
24
- config = merge(defaultConfig, config)
20
+ export default posthtmlPlugin
25
21
 
26
- return pretty(html, config)
22
+ export async function prettify(html = '', options = {}, posthtmlOptions = {}) {
23
+ return posthtml([
24
+ posthtmlPlugin(options)
25
+ ])
26
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
27
+ .then(result => result.html)
27
28
  }
@@ -1,22 +1,13 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge, isEmpty} = require('lodash')
3
- const {removeWidows} = require('string-remove-widows')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import { removeWidows } from 'string-remove-widows'
4
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
5
 
6
- module.exports = async (html, config = {}) => {
7
- if (isEmpty(config)) {
8
- return removeWidows(html).res
9
- }
10
-
11
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
12
-
13
- return posthtml([removeWidowsPlugin(config)]).process(html, posthtmlOptions).then(result => result.html)
14
- }
15
-
16
- const removeWidowsPlugin = options => tree => {
17
- const {attrName = 'prevent-widows', ...removeWidowsOptions} = get(options, 'widowWords', options)
18
-
19
- removeWidowsOptions.minWordCount = removeWidowsOptions.minWordCount || 3
6
+ const posthtmlPlugin = (options = {}) => tree => {
7
+ options = merge(options, {
8
+ minWordCount: 3,
9
+ attrName: 'prevent-widows'
10
+ })
20
11
 
21
12
  // Ignore defaults
22
13
  const mappings = [
@@ -58,22 +49,19 @@ const removeWidowsPlugin = options => tree => {
58
49
  }
59
50
  ]
60
51
 
61
- if (Array.isArray(removeWidowsOptions.ignore)) {
62
- for (const pair of removeWidowsOptions.ignore) {
63
- mappings.push(pair)
64
- }
52
+ if (Array.isArray(options.ignore)) {
53
+ options.ignore.forEach(pair => mappings.push(pair))
65
54
  }
66
55
 
67
- if (typeof removeWidowsOptions.ignore !== 'string') {
68
- removeWidowsOptions.ignore = mappings
56
+ if (typeof options.ignore !== 'string') {
57
+ options.ignore = mappings
69
58
  }
70
59
 
71
60
  const process = node => {
72
- if (node.attrs && Object.keys(node.attrs).includes(attrName)) {
73
- const widowsRemovedString = removeWidows(tree.render(node.content), removeWidowsOptions).res
74
-
61
+ if (node.attrs && Object.keys(node.attrs).includes(options.attrName)) {
62
+ const widowsRemovedString = removeWidows(tree.render(node.content), options).res
75
63
  node.content = tree.render(tree.parser(widowsRemovedString))
76
- node.attrs[attrName] = false
64
+ delete node.attrs[options.attrName]
77
65
  }
78
66
 
79
67
  return node
@@ -81,3 +69,19 @@ const removeWidowsPlugin = options => tree => {
81
69
 
82
70
  return tree.walk(process)
83
71
  }
72
+
73
+ export default posthtmlPlugin
74
+
75
+ export async function preventWidows(html = '', options = {}, posthtmlOptions = {}) {
76
+ // Apply only to elements that contain the `prevent-widows` attribute
77
+ if (options.withAttributes) {
78
+ return posthtml([
79
+ posthtmlPlugin(options)
80
+ ])
81
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
82
+ .then(result => result.html)
83
+ }
84
+
85
+ // Apply to all elements
86
+ return removeWidows(html, options).res
87
+ }
@@ -1,19 +1,7 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge} = require('lodash')
3
- const defaultConfig = require('../generators/posthtml/defaultConfig')
4
-
5
- module.exports = async (html, config = {}, direct = false) => {
6
- const attributes = direct ? (Array.isArray(config) ? [...config] : []) : get(config, 'removeAttributes', [])
7
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
8
-
9
- attributes.push('style', 'class')
10
-
11
- html = await posthtml([
12
- removeAttributes(attributes, posthtmlOptions)
13
- ]).process(html, posthtmlOptions).then(result => result.html)
14
-
15
- return html
16
- }
1
+ import posthtml from 'posthtml'
2
+ import get from 'lodash-es/get.js'
3
+ import { defu as merge } from 'defu'
4
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
17
5
 
18
6
  /**
19
7
  * Remove empty attributes with PostHTML
@@ -26,7 +14,9 @@ module.exports = async (html, config = {}, direct = false) => {
26
14
  *
27
15
  * Condition 3: same as 2, but for regular expressions
28
16
  */
29
- const removeAttributes = (attributes = {}, posthtmlOptions = {}) => tree => {
17
+ const posthtmlPlugin = (attributes = [], posthtmlOptions = {}) => tree => {
18
+ attributes.push('style', 'class')
19
+
30
20
  const process = node => {
31
21
  const normalizedAttrs = attributes.map(attribute => {
32
22
  return {
@@ -36,7 +26,7 @@ const removeAttributes = (attributes = {}, posthtmlOptions = {}) => tree => {
36
26
  })
37
27
 
38
28
  if (node.attrs) {
39
- for (const attr of normalizedAttrs) {
29
+ normalizedAttrs.forEach(attr => {
40
30
  const targetAttrValue = get(node.attrs, attr.name)
41
31
 
42
32
  if (
@@ -46,7 +36,7 @@ const removeAttributes = (attributes = {}, posthtmlOptions = {}) => tree => {
46
36
  ) {
47
37
  node.attrs[attr.name] = false
48
38
  }
49
- }
39
+ })
50
40
  }
51
41
 
52
42
  return node
@@ -54,3 +44,13 @@ const removeAttributes = (attributes = {}, posthtmlOptions = {}) => tree => {
54
44
 
55
45
  return tree.walk(process)
56
46
  }
47
+
48
+ export default posthtmlPlugin
49
+
50
+ export async function removeAttributes(html = '', attributes = [], posthtmlOptions = {}) {
51
+ return posthtml([
52
+ posthtmlPlugin(attributes, merge(posthtmlOptions, posthtmlConfig))
53
+ ])
54
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
55
+ .then(result => result.html)
56
+ }
@@ -1,14 +1,35 @@
1
- const {get, isEmpty} = require('lodash')
2
-
3
- module.exports = async (html, config = {}, direct = false) => {
4
- const replacements = direct ? config : get(config, 'replaceStrings', {})
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import { render } from 'posthtml-render'
4
+ import isEmpty from 'lodash-es/isEmpty.js'
5
+ import { parser as parse } from 'posthtml-parser'
6
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
7
 
8
+ const posthtmlPlugin = (replacements = {}) => tree => {
6
9
  if (!isEmpty(replacements)) {
7
- for (const [k, v] of Object.entries(replacements)) {
8
- const regex = new RegExp(k, 'gi')
9
- html = html.replace(regex, v)
10
- }
10
+ const regexes = Object.entries(replacements).map(([k, v]) => [new RegExp(k, 'gi'), v])
11
+ const patterns = new RegExp(Object.keys(replacements).join('|'), 'gi')
12
+
13
+ return parse(
14
+ render(tree).replace(patterns, matched => {
15
+ for (const [regex, replacement] of regexes) {
16
+ if (regex.test(matched)) {
17
+ return replacement
18
+ }
19
+ }
20
+ })
21
+ )
11
22
  }
12
23
 
13
- return html
24
+ return tree
25
+ }
26
+
27
+ export default posthtmlPlugin
28
+
29
+ export async function replaceStrings(html = '', replacements = {}, posthtmlOptions = {}) {
30
+ return posthtml([
31
+ posthtmlPlugin(replacements)
32
+ ])
33
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
34
+ .then(result => result.html)
14
35
  }
@@ -1,29 +1,29 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge, isEmpty} = require('lodash')
3
- const safeClassNames = require('posthtml-safe-class-names')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
4
+ import posthtmlSafeClassNames from 'posthtml-safe-class-names'
5
5
 
6
- module.exports = async (html, config = {}, direct = false) => {
7
- /*
8
- * Don't run when:
9
- * - `config` is falsy or empty
10
- * - developing locally and `safeClassNames` is not explicitly `true`
11
- * - `safeClassNames` is explicitly `false`
12
- */
13
- if (
14
- !config
15
- || isEmpty(config)
16
- || (get(config, 'env') === 'local' && get(config, 'safeClassNames') !== true)
17
- || get(config, 'safeClassNames') === false
18
- ) {
19
- return html
6
+ export default function posthtmlPlugin(options = {}) {
7
+ // If options is boolean, convert to object
8
+ if (typeof options === 'boolean') {
9
+ options = {}
20
10
  }
21
11
 
22
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
23
- const replacements = direct ? config : get(config, 'safeClassNames', {
24
- '{': '{',
25
- '}': '}'
26
- })
12
+ // Default options
13
+ options = merge({
14
+ replacements: {
15
+ '{': '{',
16
+ '}': '}'
17
+ }
18
+ }, options)
27
19
 
28
- return posthtml([safeClassNames({replacements})]).process(html, posthtmlOptions).then(result => result.html)
20
+ return posthtmlSafeClassNames(options)
21
+ }
22
+
23
+ export async function safeClassNames(html = '', options = {}, posthtmlOptions = {}) {
24
+ return posthtml([
25
+ posthtmlPlugin(options)
26
+ ])
27
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
28
+ .then(result => result.html)
29
29
  }
@@ -0,0 +1,22 @@
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
4
+ import posthtmlMergeLonghand from 'posthtml-postcss-merge-longhand'
5
+
6
+ export default function posthtmlPlugin(options = {}) {
7
+ if (Array.isArray(options.tags)) {
8
+ return posthtmlMergeLonghand({
9
+ tags: options.tags,
10
+ })
11
+ }
12
+
13
+ return posthtmlMergeLonghand()
14
+ }
15
+
16
+ export async function shorthandCSS(html = '', options = {}, posthtmlOptions = {}) {
17
+ return posthtml([
18
+ posthtmlPlugin(options)
19
+ ])
20
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
21
+ .then(result => result.html)
22
+ }
@@ -1,28 +1,18 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge} = require('lodash')
3
- const {conv} = require('color-shorthand-hex-to-six-digit')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import { defu as merge } from 'defu'
3
+ import { conv } from 'color-shorthand-hex-to-six-digit'
4
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
5
 
6
- module.exports = async (html, config = {}) => {
7
- if (get(config, 'sixHex') === false) {
8
- return html
9
- }
10
-
11
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
12
-
13
- return posthtml([sixHex()]).process(html, posthtmlOptions).then(result => result.html)
14
- }
15
-
16
- const sixHex = () => tree => {
6
+ const posthtmlPlugin = () => tree => {
17
7
  const targets = new Set(['bgcolor', 'color'])
18
8
 
19
9
  const process = node => {
20
10
  if (node.attrs) {
21
- for (const [name, value] of Object.entries(node.attrs)) {
11
+ Object.entries(node.attrs).forEach(([name, value]) => {
22
12
  if (targets.has(name) && node.attrs[name]) {
23
13
  node.attrs[name] = conv(value)
24
14
  }
25
- }
15
+ })
26
16
  }
27
17
 
28
18
  return node
@@ -30,3 +20,13 @@ const sixHex = () => tree => {
30
20
 
31
21
  return tree.walk(process)
32
22
  }
23
+
24
+ export default posthtmlPlugin
25
+
26
+ export async function sixHEX(html = '', posthtmlOptions = {}) {
27
+ return posthtml([
28
+ posthtmlPlugin()
29
+ ])
30
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
31
+ .then(result => result.html)
32
+ }
@@ -1,20 +1,22 @@
1
- const posthtml = require('posthtml')
2
- const {get, merge, isEmpty} = require('lodash')
3
- const urlParams = require('posthtml-url-parameters')
4
- const defaultConfig = require('../generators/posthtml/defaultConfig')
1
+ import posthtml from 'posthtml'
2
+ import get from 'lodash-es/get.js'
3
+ import { defu as merge } from 'defu'
4
+ import urlParameters from 'posthtml-url-parameters'
5
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
5
6
 
6
- module.exports = async (html, config = {}, direct = false) => {
7
- const urlParameters = direct ? config : get(config, 'urlParameters', {})
7
+ export default function posthtmlPlugin(options = {}) {
8
+ const { _options, ...parameters } = options
9
+ const tags = get(_options, 'tags', ['a'])
10
+ const strict = get(_options, 'strict', true)
11
+ const qs = get(_options, 'qs', { encode: false })
8
12
 
9
- if (!isEmpty(urlParameters)) {
10
- const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
11
- const {_options, ...parameters} = urlParameters
12
- const tags = get(_options, 'tags', ['a'])
13
- const strict = get(_options, 'strict', true)
14
- const qs = get(_options, 'qs', {encode: false})
15
-
16
- return posthtml([urlParams({parameters, tags, qs, strict})]).process(html, posthtmlOptions).then(result => result.html)
17
- }
13
+ return urlParameters({ parameters, tags, qs, strict })
14
+ }
18
15
 
19
- return html
16
+ export async function addURLParams(html = '', options = {}, posthtmlOptions = {}) {
17
+ return posthtml([
18
+ posthtmlPlugin(options)
19
+ ])
20
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
21
+ .then(result => result.html)
20
22
  }
@@ -0,0 +1,65 @@
1
+ import postcss from 'postcss'
2
+ import posthtml from 'posthtml'
3
+ import get from 'lodash-es/get.js'
4
+ import { defu as merge } from 'defu'
5
+ import posthtmlConfig from '../posthtml/defaultConfig.js'
6
+
7
+ const posthtmlPlugin = (mappings = {}) => tree => {
8
+ if (!Object.keys(mappings).length) {
9
+ return tree
10
+ }
11
+
12
+ // Normalize tags in mappings by lowercasing them
13
+ for (const key in mappings) {
14
+ if (Array.isArray(mappings[key])) {
15
+ mappings[key] = mappings[key].map(value => value.toLowerCase())
16
+ }
17
+ }
18
+
19
+ const process = node => {
20
+ // Check if the node is defined in mappings
21
+ if (
22
+ get(mappings, 'width', []).includes(node.tag)
23
+ || get(mappings, 'height', []).includes(node.tag)
24
+ ) {
25
+ // Check if the node has an inline CSS property equal to one of the keys in mappings
26
+ if (node.attrs.style) {
27
+ const { root } = postcss().process(`${node.attrs.style}`, { from: undefined })
28
+
29
+ root.walkDecls(decl => {
30
+ if (mappings.width.includes(node.tag) && decl.prop === 'width') {
31
+ // Set its value as an attribute on the node; the attribute name is the key in mappings
32
+ node.attrs.width = decl.value.replace('px', '')
33
+ // Remove the inline CSS property from the declaration
34
+ decl.remove()
35
+ }
36
+
37
+ if (mappings.height.includes(node.tag) && decl.prop === 'height') {
38
+ // Set its value as an attribute on the node; the attribute name is the key in mappings
39
+ node.attrs.height = decl.value.replace('px', '')
40
+ // Remove the inline CSS property from the declaration
41
+ decl.remove()
42
+ }
43
+ })
44
+
45
+ // Set the remaining inline CSS as the `style` attribute on the node
46
+ // If there are no remaining declarations, remove the `style` attribute
47
+ node.attrs.style = root.toString().trim() || false
48
+ }
49
+ }
50
+
51
+ return node
52
+ }
53
+
54
+ return tree.walk(process)
55
+ }
56
+
57
+ export default posthtmlPlugin
58
+
59
+ export async function useAttributeSizes(html = '', mappings = {}, posthtmlOptions = {}) {
60
+ return posthtml([
61
+ posthtmlPlugin(mappings)
62
+ ])
63
+ .process(html, merge(posthtmlOptions, posthtmlConfig))
64
+ .then(result => result.html)
65
+ }