Skip to content

Commit

Permalink
fix: default posthtml options
Browse files Browse the repository at this point in the history
get parser directives working again
  • Loading branch information
cossssmin committed Nov 4, 2024
1 parent 7d20789 commit 6a5249b
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 86 deletions.
6 changes: 3 additions & 3 deletions src/generators/plaintext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import posthtml from 'posthtml'
import get from 'lodash-es/get.js'
import { defu as merge } from 'defu'
import { stripHtml } from 'string-strip-html'
import defaultConfig from '../posthtml/defaultConfig.js'
import { writeFile, lstat, mkdir } from 'node:fs/promises'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

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

const posthtmlOptions = merge(defaultConfig, config)
const posthtmlOptions = merge(config, getPosthtmlOptions())

return posthtml([posthtmlPlugin()]).process(html, { ...posthtmlOptions }).then(res => res.html)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function handlePlaintextTags(html = '', config = {}) {
return tree.walk(process)
}

const posthtmlOptions = merge(defaultConfig, config)
const posthtmlOptions = merge(config, getPosthtmlOptions())

return posthtml([posthtmlPlugin()]).process(html, { ...posthtmlOptions }).then(res => res.html)
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cwd } from 'node:process'
import { defu as merge } from 'defu'
import expressions from 'posthtml-expressions'
import { parseFrontMatter } from '../utils/node.js'
import defaultConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'
import { process as compilePostHTML } from '../posthtml/index.js'
import { run as useTransformers } from '../transformers/index.js'

Expand Down Expand Up @@ -41,7 +41,7 @@ export async function render(html = '', config = {}) {
})
]
)
.process(matter, defaultConfig)
.process(matter, getPosthtmlOptions())
.then(({ html }) => parseFrontMatter(`---${html}\n---`))

const templateConfig = merge(matterData, config)
Expand Down
5 changes: 0 additions & 5 deletions src/posthtml/defaultComponentsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ export default {
missingLocal: '{local}',
strictMode: false,
},
parserOptions: {
directives: [
{ name: '?php', start: '<', end: '>' },
]
},
}
19 changes: 13 additions & 6 deletions src/posthtml/defaultConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export default {
recognizeNoValueAttribute: true,
recognizeSelfClosing: true,
directives: [
{ name: '?php', start: '<', end: '>' },
],
import { defu as merge } from 'defu'

export function getPosthtmlOptions(userConfigOptions = {}) {
return merge(
userConfigOptions,
{
recognizeNoValueAttribute: true,
recognizeSelfClosing: true,
directives: [
{ name: '?php', start: '<', end: '>' },
],
}
)
}
4 changes: 2 additions & 2 deletions src/posthtml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import posthtmlFetch from 'posthtml-fetch'
import envTags from './plugins/envTags.js'
import components from 'posthtml-component'
import posthtmlPostcss from 'posthtml-postcss'
import defaultPosthtmlConfig from './defaultConfig.js'
import expandLinkTag from './plugins/expandLinkTag.js'
import envAttributes from './plugins/envAttributes.js'
import { getPosthtmlOptions } from './defaultConfig.js'

// PostCSS
import tailwindcss from 'tailwindcss'
Expand All @@ -36,7 +36,7 @@ export async function process(html = '', config = {}) {
)
)

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

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

Expand Down
3 changes: 1 addition & 2 deletions src/transformers/addAttributes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import posthtml from 'posthtml'
import { defu as merge } from 'defu'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import addAttributesPlugin from 'posthtml-extra-attributes'

export default function posthtmlPlugin(attributes = {}) {
Expand All @@ -25,6 +24,6 @@ export async function addAttributes(html = '', attributes = {}, posthtmlOptions
return posthtml([
posthtmlPlugin(attributes)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
4 changes: 1 addition & 3 deletions src/transformers/attributeToStyle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import posthtml from 'posthtml'
import get from 'lodash-es/get.js'
import { defu as merge } from 'defu'
import keys from 'lodash-es/keys.js'
import forEach from 'lodash-es/forEach.js'
import parseAttrs from 'posthtml-attrs-parser'
import intersection from 'lodash-es/intersection.js'
import posthtmlConfig from '../posthtml/defaultConfig.js'

const posthtmlPlugin = (attributes = []) => tree => {
if (!Array.isArray(attributes)) {
Expand Down Expand Up @@ -87,6 +85,6 @@ export async function attributeToStyle(html = '', attributes = [], posthtmlOptio
return posthtml([
posthtmlPlugin(attributes)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
12 changes: 6 additions & 6 deletions src/transformers/baseUrl.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import posthtml from 'posthtml'
import isUrl from 'is-url-superb'
import get from 'lodash-es/get.js'
import { defu as merge } from 'defu'
import baseUrl from 'posthtml-base-url'
import { render } from 'posthtml-render'
import isEmpty from 'lodash-es/isEmpty.js'
import isObject from 'lodash-es/isObject.js'
import { parser as parse } from 'posthtml-parser'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

const posthtmlOptions = getPosthtmlOptions()

const posthtmlPlugin = url => tree => {
// Handle `baseURL` as a string
Expand All @@ -19,7 +20,7 @@ const posthtmlPlugin = url => tree => {
allTags: true,
styleTag: true,
inlineCss: true
})(parse(html, posthtmlConfig))
})(parse(html, posthtmlOptions))
}

// Handle `baseURL` as an object
Expand All @@ -31,7 +32,6 @@ const posthtmlPlugin = url => tree => {
allTags,
tags,
url: baseURL,
...posthtmlOptions
} = url

return baseUrl({
Expand All @@ -40,7 +40,7 @@ const posthtmlPlugin = url => tree => {
allTags,
tags,
url: baseURL,
})(parse(html, merge(posthtmlConfig, posthtmlOptions)))
})(parse(html, posthtmlOptions))
}

return tree
Expand All @@ -52,7 +52,7 @@ export async function addBaseUrl(html = '', options = {}, posthtmlOptions = {})
return posthtml([
posthtmlPlugin(options)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, getPosthtmlOptions())
.then(result => result.html)
}

Expand Down
9 changes: 5 additions & 4 deletions src/transformers/comb.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defu as merge } from 'defu'
import { render } from 'posthtml-render'
import { comb as emailComb } from 'email-comb'
import { parser as parse } from 'posthtml-parser'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

const posthtmlPlugin = options => tree => {
const defaultSafelist = [
Expand Down Expand Up @@ -35,17 +35,18 @@ const posthtmlPlugin = options => tree => {

options = merge(options, defaultOptions)

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

return parse(html, posthtmlConfig)
}

export default posthtmlPlugin

export async function comb(html = '', options = {}, posthtmlOptions = {}) {
export async function comb(html = '', pluginOptions = {}, posthtmlOptions = {}) {
return posthtml([
posthtmlPlugin(options)
posthtmlPlugin(pluginOptions)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
3 changes: 1 addition & 2 deletions src/transformers/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import posthtml from 'posthtml'
import { defu as merge } from 'defu'
import posthtmlContent from 'posthtml-content'
import posthtmlConfig from '../../posthtml/defaultConfig.js'
import { filters as defaultFilters } from './defaultFilters.js'

export default function posthtmlPlugin(filters = {}) {
Expand All @@ -14,6 +13,6 @@ export async function filters(html = '', filters = {}, posthtmlOptions = {}) {
return posthtml([
posthtmlPlugin(filters)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
7 changes: 2 additions & 5 deletions src/transformers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import replaceStrings from './replaceStrings.js'
import attributeToStyle from './attributeToStyle.js'
import removeAttributes from './removeAttributes.js'

import defaultPosthtmlConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

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

const posthtmlConfig = merge(
get(config, 'posthtml.options', {}),
defaultPosthtmlConfig
)
const posthtmlConfig = getPosthtmlOptions(get(config, 'posthtml.options', {}))

/**
* 1. Core transformers
Expand Down
16 changes: 8 additions & 8 deletions src/transformers/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import isObject from 'lodash-es/isObject.js'
import { parser as parse } from 'posthtml-parser'
import { parseCSSRule } from '../utils/string.js'
import { useAttributeSizes } from './useAttributeSizes.js'
import defaultPostHTMLConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

const posthtmlPlugin = (options = {}) => tree => {
return inline(render(tree), options).then(html => parse(html, defaultPostHTMLConfig))
return inline(render(tree), options).then(html => parse(html, getPosthtmlOptions()))
}

export default posthtmlPlugin
Expand Down Expand Up @@ -150,7 +150,7 @@ export async function inline(html = '', options = {}) {
rule.walkDecls(decl => {
// Resolve calc() values to static values
if (options.resolveCalc) {
decl.value = decl.value.includes('calc(') ? calc(decl.value, {precision: 2}) : decl.value
decl.value = decl.value.includes('calc(') ? calc(decl.value, { precision: 2 }) : decl.value
}

declarations.add(decl)
Expand All @@ -164,10 +164,10 @@ export async function inline(html = '', options = {}) {
*/
if (options.resolveCSSVariables) {
Array.from(declarations)
/**
* Consider only declarations with a value that includes any of the other declarations' property
* So a decl like color(var(--text-color)) will be removed if there's a decl with a property of --text-color
* */
/**
* Consider only declarations with a value that includes any of the other declarations' property
* So a decl like color(var(--text-color)) will be removed if there's a decl with a property of --text-color
* */
.filter(decl =>
Array.from(declarations).some(otherDecl => decl.value.includes(otherDecl.prop))
|| decl.prop.startsWith('--')
Expand Down Expand Up @@ -216,7 +216,7 @@ export async function inline(html = '', options = {}) {
let { property, value } = parseCSSRule(i)

if (value && options.resolveCalc) {
value = value.includes('calc') ? calc(value, {precision: 2}) : value
value = value.includes('calc') ? calc(value, { precision: 2 }) : value
}

if (value && options.preferUnitlessValues) {
Expand Down
4 changes: 1 addition & 3 deletions src/transformers/markdown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import posthtml from 'posthtml'
import { defu as merge } from 'defu'
import md from 'posthtml-markdownit'
import posthtmlConfig from '../posthtml/defaultConfig.js'

export async function markdown(input = '', options = {}, posthtmlOptions = {}) {
/**
Expand All @@ -23,6 +21,6 @@ export async function markdown(input = '', options = {}, posthtmlOptions = {}) {
return posthtml([
md(options)
])
.process(input, merge(posthtmlOptions, posthtmlConfig))
.process(input, posthtmlOptions)
.then(result => result.html)
}
8 changes: 4 additions & 4 deletions src/transformers/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { crush } from 'html-crush'
import { defu as merge } from 'defu'
import { render } from 'posthtml-render'
import { parser as parse } from 'posthtml-parser'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import defaultPostHTMLConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

const posthtmlPlugin = (options = {}) => tree => {
options = merge(options, {
removeLineBreaks: true,
})

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

return parse(html, defaultPostHTMLConfig)
return parse(html, posthtmlConfig)
}

export default posthtmlPlugin
Expand All @@ -22,6 +22,6 @@ export async function minify(html = '', options = {}, posthtmlOptions = {}) {
return posthtml([
posthtmlPlugin(options)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
4 changes: 1 addition & 3 deletions src/transformers/posthtmlMso.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import posthtml from 'posthtml'
import { defu as merge } from 'defu'
import posthtmlMso from 'posthtml-mso'
import posthtmlConfig from '../posthtml/defaultConfig.js'

export default function posthtmlPlugin(options = {}) {
return posthtmlMso(options)
Expand All @@ -11,6 +9,6 @@ export async function useMso(html = '', options = {}, posthtmlOptions = {}) {
return posthtml([
posthtmlPlugin(options)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
6 changes: 3 additions & 3 deletions src/transformers/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import posthtml from 'posthtml'
import { defu as merge } from 'defu'
import { render } from 'posthtml-render'
import { parser as parse } from 'posthtml-parser'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import { getPosthtmlOptions } from '../posthtml/defaultConfig.js'

const posthtmlPlugin = (options = {}) => tree => {
const defaultConfig = {
Expand All @@ -15,7 +15,7 @@ const posthtmlPlugin = (options = {}) => tree => {

const config = merge(options, defaultConfig)

return parse(pretty(render(tree), config), posthtmlConfig)
return parse(pretty(render(tree), config), getPosthtmlOptions())
}

export default posthtmlPlugin
Expand All @@ -24,6 +24,6 @@ export async function prettify(html = '', options = {}, posthtmlOptions = {}) {
return posthtml([
posthtmlPlugin(options)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(html, posthtmlOptions)
.then(result => result.html)
}
Loading

0 comments on commit 6a5249b

Please sign in to comment.