Skip to content

Commit 6a5249b

Browse files
committed
fix: default posthtml options
get parser directives working again
1 parent 7d20789 commit 6a5249b

25 files changed

+79
-86
lines changed

src/generators/plaintext.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import posthtml from 'posthtml'
33
import get from 'lodash-es/get.js'
44
import { defu as merge } from 'defu'
55
import { stripHtml } from 'string-strip-html'
6-
import defaultConfig from '../posthtml/defaultConfig.js'
76
import { writeFile, lstat, mkdir } from 'node:fs/promises'
7+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
88

99
/**
1010
* Removes HTML tags from a given HTML string based on
@@ -46,7 +46,7 @@ const removeTags = ({ tag = 'not-plaintext', html = '', config = {} }) => {
4646
return tree.walk(process)
4747
}
4848

49-
const posthtmlOptions = merge(defaultConfig, config)
49+
const posthtmlOptions = merge(config, getPosthtmlOptions())
5050

5151
return posthtml([posthtmlPlugin()]).process(html, { ...posthtmlOptions }).then(res => res.html)
5252
}
@@ -95,7 +95,7 @@ export async function handlePlaintextTags(html = '', config = {}) {
9595
return tree.walk(process)
9696
}
9797

98-
const posthtmlOptions = merge(defaultConfig, config)
98+
const posthtmlOptions = merge(config, getPosthtmlOptions())
9999

100100
return posthtml([posthtmlPlugin()]).process(html, { ...posthtmlOptions }).then(res => res.html)
101101
}

src/generators/render.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { cwd } from 'node:process'
44
import { defu as merge } from 'defu'
55
import expressions from 'posthtml-expressions'
66
import { parseFrontMatter } from '../utils/node.js'
7-
import defaultConfig from '../posthtml/defaultConfig.js'
7+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
88
import { process as compilePostHTML } from '../posthtml/index.js'
99
import { run as useTransformers } from '../transformers/index.js'
1010

@@ -41,7 +41,7 @@ export async function render(html = '', config = {}) {
4141
})
4242
]
4343
)
44-
.process(matter, defaultConfig)
44+
.process(matter, getPosthtmlOptions())
4545
.then(({ html }) => parseFrontMatter(`---${html}\n---`))
4646

4747
const templateConfig = merge(matterData, config)

src/posthtml/defaultComponentsConfig.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@ export default {
88
missingLocal: '{local}',
99
strictMode: false,
1010
},
11-
parserOptions: {
12-
directives: [
13-
{ name: '?php', start: '<', end: '>' },
14-
]
15-
},
1611
}

src/posthtml/defaultConfig.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
export default {
2-
recognizeNoValueAttribute: true,
3-
recognizeSelfClosing: true,
4-
directives: [
5-
{ name: '?php', start: '<', end: '>' },
6-
],
1+
import { defu as merge } from 'defu'
2+
3+
export function getPosthtmlOptions(userConfigOptions = {}) {
4+
return merge(
5+
userConfigOptions,
6+
{
7+
recognizeNoValueAttribute: true,
8+
recognizeSelfClosing: true,
9+
directives: [
10+
{ name: '?php', start: '<', end: '>' },
11+
],
12+
}
13+
)
714
}

src/posthtml/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import posthtmlFetch from 'posthtml-fetch'
77
import envTags from './plugins/envTags.js'
88
import components from 'posthtml-component'
99
import posthtmlPostcss from 'posthtml-postcss'
10-
import defaultPosthtmlConfig from './defaultConfig.js'
1110
import expandLinkTag from './plugins/expandLinkTag.js'
1211
import envAttributes from './plugins/envAttributes.js'
12+
import { getPosthtmlOptions } from './defaultConfig.js'
1313

1414
// PostCSS
1515
import tailwindcss from 'tailwindcss'
@@ -36,7 +36,7 @@ export async function process(html = '', config = {}) {
3636
)
3737
)
3838

39-
const posthtmlOptions = merge(get(config, 'posthtml.options', {}), defaultPosthtmlConfig)
39+
const posthtmlOptions = getPosthtmlOptions(get(config, 'posthtml.options', {}))
4040

4141
const componentsUserOptions = get(config, 'components', {})
4242

src/transformers/addAttributes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import posthtml from 'posthtml'
22
import { defu as merge } from 'defu'
3-
import posthtmlConfig from '../posthtml/defaultConfig.js'
43
import addAttributesPlugin from 'posthtml-extra-attributes'
54

65
export default function posthtmlPlugin(attributes = {}) {
@@ -25,6 +24,6 @@ export async function addAttributes(html = '', attributes = {}, posthtmlOptions
2524
return posthtml([
2625
posthtmlPlugin(attributes)
2726
])
28-
.process(html, merge(posthtmlOptions, posthtmlConfig))
27+
.process(html, posthtmlOptions)
2928
.then(result => result.html)
3029
}

src/transformers/attributeToStyle.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import posthtml from 'posthtml'
22
import get from 'lodash-es/get.js'
3-
import { defu as merge } from 'defu'
43
import keys from 'lodash-es/keys.js'
54
import forEach from 'lodash-es/forEach.js'
65
import parseAttrs from 'posthtml-attrs-parser'
76
import intersection from 'lodash-es/intersection.js'
8-
import posthtmlConfig from '../posthtml/defaultConfig.js'
97

108
const posthtmlPlugin = (attributes = []) => tree => {
119
if (!Array.isArray(attributes)) {
@@ -87,6 +85,6 @@ export async function attributeToStyle(html = '', attributes = [], posthtmlOptio
8785
return posthtml([
8886
posthtmlPlugin(attributes)
8987
])
90-
.process(html, merge(posthtmlOptions, posthtmlConfig))
88+
.process(html, posthtmlOptions)
9189
.then(result => result.html)
9290
}

src/transformers/baseUrl.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import posthtml from 'posthtml'
22
import isUrl from 'is-url-superb'
33
import get from 'lodash-es/get.js'
4-
import { defu as merge } from 'defu'
54
import baseUrl from 'posthtml-base-url'
65
import { render } from 'posthtml-render'
76
import isEmpty from 'lodash-es/isEmpty.js'
87
import isObject from 'lodash-es/isObject.js'
98
import { parser as parse } from 'posthtml-parser'
10-
import posthtmlConfig from '../posthtml/defaultConfig.js'
9+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
10+
11+
const posthtmlOptions = getPosthtmlOptions()
1112

1213
const posthtmlPlugin = url => tree => {
1314
// Handle `baseURL` as a string
@@ -19,7 +20,7 @@ const posthtmlPlugin = url => tree => {
1920
allTags: true,
2021
styleTag: true,
2122
inlineCss: true
22-
})(parse(html, posthtmlConfig))
23+
})(parse(html, posthtmlOptions))
2324
}
2425

2526
// Handle `baseURL` as an object
@@ -31,7 +32,6 @@ const posthtmlPlugin = url => tree => {
3132
allTags,
3233
tags,
3334
url: baseURL,
34-
...posthtmlOptions
3535
} = url
3636

3737
return baseUrl({
@@ -40,7 +40,7 @@ const posthtmlPlugin = url => tree => {
4040
allTags,
4141
tags,
4242
url: baseURL,
43-
})(parse(html, merge(posthtmlConfig, posthtmlOptions)))
43+
})(parse(html, posthtmlOptions))
4444
}
4545

4646
return tree
@@ -52,7 +52,7 @@ export async function addBaseUrl(html = '', options = {}, posthtmlOptions = {})
5252
return posthtml([
5353
posthtmlPlugin(options)
5454
])
55-
.process(html, merge(posthtmlOptions, posthtmlConfig))
55+
.process(html, getPosthtmlOptions())
5656
.then(result => result.html)
5757
}
5858

src/transformers/comb.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defu as merge } from 'defu'
44
import { render } from 'posthtml-render'
55
import { comb as emailComb } from 'email-comb'
66
import { parser as parse } from 'posthtml-parser'
7-
import posthtmlConfig from '../posthtml/defaultConfig.js'
7+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
88

99
const posthtmlPlugin = options => tree => {
1010
const defaultSafelist = [
@@ -35,17 +35,18 @@ const posthtmlPlugin = options => tree => {
3535

3636
options = merge(options, defaultOptions)
3737

38+
const posthtmlConfig = getPosthtmlOptions()
3839
const { result: html } = emailComb(render(tree), options)
3940

4041
return parse(html, posthtmlConfig)
4142
}
4243

4344
export default posthtmlPlugin
4445

45-
export async function comb(html = '', options = {}, posthtmlOptions = {}) {
46+
export async function comb(html = '', pluginOptions = {}, posthtmlOptions = {}) {
4647
return posthtml([
47-
posthtmlPlugin(options)
48+
posthtmlPlugin(pluginOptions)
4849
])
49-
.process(html, merge(posthtmlOptions, posthtmlConfig))
50+
.process(html, posthtmlOptions)
5051
.then(result => result.html)
5152
}

src/transformers/filters/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import posthtml from 'posthtml'
22
import { defu as merge } from 'defu'
33
import posthtmlContent from 'posthtml-content'
4-
import posthtmlConfig from '../../posthtml/defaultConfig.js'
54
import { filters as defaultFilters } from './defaultFilters.js'
65

76
export default function posthtmlPlugin(filters = {}) {
@@ -14,6 +13,6 @@ export async function filters(html = '', filters = {}, posthtmlOptions = {}) {
1413
return posthtml([
1514
posthtmlPlugin(filters)
1615
])
17-
.process(html, merge(posthtmlOptions, posthtmlConfig))
16+
.process(html, posthtmlOptions)
1817
.then(result => result.html)
1918
}

src/transformers/index.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import replaceStrings from './replaceStrings.js'
2222
import attributeToStyle from './attributeToStyle.js'
2323
import removeAttributes from './removeAttributes.js'
2424

25-
import defaultPosthtmlConfig from '../posthtml/defaultConfig.js'
25+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
2626

2727
/**
2828
* Use Maizzle Transformers on an HTML string.
@@ -37,10 +37,7 @@ import defaultPosthtmlConfig from '../posthtml/defaultConfig.js'
3737
export async function run(html = '', config = {}) {
3838
const posthtmlPlugins = []
3939

40-
const posthtmlConfig = merge(
41-
get(config, 'posthtml.options', {}),
42-
defaultPosthtmlConfig
43-
)
40+
const posthtmlConfig = getPosthtmlOptions(get(config, 'posthtml.options', {}))
4441

4542
/**
4643
* 1. Core transformers

src/transformers/inline.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import isObject from 'lodash-es/isObject.js'
1212
import { parser as parse } from 'posthtml-parser'
1313
import { parseCSSRule } from '../utils/string.js'
1414
import { useAttributeSizes } from './useAttributeSizes.js'
15-
import defaultPostHTMLConfig from '../posthtml/defaultConfig.js'
15+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
1616

1717
const posthtmlPlugin = (options = {}) => tree => {
18-
return inline(render(tree), options).then(html => parse(html, defaultPostHTMLConfig))
18+
return inline(render(tree), options).then(html => parse(html, getPosthtmlOptions()))
1919
}
2020

2121
export default posthtmlPlugin
@@ -150,7 +150,7 @@ export async function inline(html = '', options = {}) {
150150
rule.walkDecls(decl => {
151151
// Resolve calc() values to static values
152152
if (options.resolveCalc) {
153-
decl.value = decl.value.includes('calc(') ? calc(decl.value, {precision: 2}) : decl.value
153+
decl.value = decl.value.includes('calc(') ? calc(decl.value, { precision: 2 }) : decl.value
154154
}
155155

156156
declarations.add(decl)
@@ -164,10 +164,10 @@ export async function inline(html = '', options = {}) {
164164
*/
165165
if (options.resolveCSSVariables) {
166166
Array.from(declarations)
167-
/**
168-
* Consider only declarations with a value that includes any of the other declarations' property
169-
* So a decl like color(var(--text-color)) will be removed if there's a decl with a property of --text-color
170-
* */
167+
/**
168+
* Consider only declarations with a value that includes any of the other declarations' property
169+
* So a decl like color(var(--text-color)) will be removed if there's a decl with a property of --text-color
170+
* */
171171
.filter(decl =>
172172
Array.from(declarations).some(otherDecl => decl.value.includes(otherDecl.prop))
173173
|| decl.prop.startsWith('--')
@@ -216,7 +216,7 @@ export async function inline(html = '', options = {}) {
216216
let { property, value } = parseCSSRule(i)
217217

218218
if (value && options.resolveCalc) {
219-
value = value.includes('calc') ? calc(value, {precision: 2}) : value
219+
value = value.includes('calc') ? calc(value, { precision: 2 }) : value
220220
}
221221

222222
if (value && options.preferUnitlessValues) {

src/transformers/markdown.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import posthtml from 'posthtml'
2-
import { defu as merge } from 'defu'
32
import md from 'posthtml-markdownit'
4-
import posthtmlConfig from '../posthtml/defaultConfig.js'
53

64
export async function markdown(input = '', options = {}, posthtmlOptions = {}) {
75
/**
@@ -23,6 +21,6 @@ export async function markdown(input = '', options = {}, posthtmlOptions = {}) {
2321
return posthtml([
2422
md(options)
2523
])
26-
.process(input, merge(posthtmlOptions, posthtmlConfig))
24+
.process(input, posthtmlOptions)
2725
.then(result => result.html)
2826
}

src/transformers/minify.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { crush } from 'html-crush'
33
import { defu as merge } from 'defu'
44
import { render } from 'posthtml-render'
55
import { parser as parse } from 'posthtml-parser'
6-
import posthtmlConfig from '../posthtml/defaultConfig.js'
7-
import defaultPostHTMLConfig from '../posthtml/defaultConfig.js'
6+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
87

98
const posthtmlPlugin = (options = {}) => tree => {
109
options = merge(options, {
1110
removeLineBreaks: true,
1211
})
1312

13+
const posthtmlConfig = getPosthtmlOptions()
1414
const { result: html } = crush(render(tree), options)
1515

16-
return parse(html, defaultPostHTMLConfig)
16+
return parse(html, posthtmlConfig)
1717
}
1818

1919
export default posthtmlPlugin
@@ -22,6 +22,6 @@ export async function minify(html = '', options = {}, posthtmlOptions = {}) {
2222
return posthtml([
2323
posthtmlPlugin(options)
2424
])
25-
.process(html, merge(posthtmlOptions, posthtmlConfig))
25+
.process(html, posthtmlOptions)
2626
.then(result => result.html)
2727
}

src/transformers/posthtmlMso.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import posthtml from 'posthtml'
2-
import { defu as merge } from 'defu'
32
import posthtmlMso from 'posthtml-mso'
4-
import posthtmlConfig from '../posthtml/defaultConfig.js'
53

64
export default function posthtmlPlugin(options = {}) {
75
return posthtmlMso(options)
@@ -11,6 +9,6 @@ export async function useMso(html = '', options = {}, posthtmlOptions = {}) {
119
return posthtml([
1210
posthtmlPlugin(options)
1311
])
14-
.process(html, merge(posthtmlOptions, posthtmlConfig))
12+
.process(html, posthtmlOptions)
1513
.then(result => result.html)
1614
}

src/transformers/prettify.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import posthtml from 'posthtml'
33
import { defu as merge } from 'defu'
44
import { render } from 'posthtml-render'
55
import { parser as parse } from 'posthtml-parser'
6-
import posthtmlConfig from '../posthtml/defaultConfig.js'
6+
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
77

88
const posthtmlPlugin = (options = {}) => tree => {
99
const defaultConfig = {
@@ -15,7 +15,7 @@ const posthtmlPlugin = (options = {}) => tree => {
1515

1616
const config = merge(options, defaultConfig)
1717

18-
return parse(pretty(render(tree), config), posthtmlConfig)
18+
return parse(pretty(render(tree), config), getPosthtmlOptions())
1919
}
2020

2121
export default posthtmlPlugin
@@ -24,6 +24,6 @@ export async function prettify(html = '', options = {}, posthtmlOptions = {}) {
2424
return posthtml([
2525
posthtmlPlugin(options)
2626
])
27-
.process(html, merge(posthtmlOptions, posthtmlConfig))
27+
.process(html, posthtmlOptions)
2828
.then(result => result.html)
2929
}

0 commit comments

Comments
 (0)