From 27d54ff0821c27076f5990c2c1a0765ab773debc Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Mon, 10 Oct 2022 18:27:14 +0300 Subject: [PATCH] refactor: use default posthtml options --- src/generators/plaintext.js | 5 +- src/generators/posthtml/defaultConfig.js | 3 ++ .../{posthtml.js => posthtml/index.js} | 3 +- src/transformers/attributeToStyle.js | 5 +- src/transformers/baseUrl.js | 53 ++++++++++--------- src/transformers/extraAttributes.js | 5 +- src/transformers/filters/index.js | 3 +- src/transformers/markdown.js | 3 +- src/transformers/posthtmlMso.js | 5 +- src/transformers/preventWidows.js | 5 +- src/transformers/removeAttributes.js | 5 +- .../removeInlineBackgroundColor.js | 5 +- src/transformers/removeInlineSizes.js | 6 ++- src/transformers/removeInlinedSelectors.js | 6 ++- src/transformers/replaceStrings.js | 2 +- src/transformers/safeClassNames.js | 5 +- src/transformers/shorthandInlineCSS.js | 5 +- src/transformers/sixHex.js | 6 ++- src/transformers/urlParameters.js | 5 +- test/expected/transformers/base-url.html | 2 +- test/test-transformers.js | 4 +- 21 files changed, 81 insertions(+), 60 deletions(-) create mode 100644 src/generators/posthtml/defaultConfig.js rename src/generators/{posthtml.js => posthtml/index.js} (92%) diff --git a/src/generators/plaintext.js b/src/generators/plaintext.js index 8379082a..46b03db5 100644 --- a/src/generators/plaintext.js +++ b/src/generators/plaintext.js @@ -1,11 +1,12 @@ const path = require('path') -const {get} = require('lodash') const posthtml = require('posthtml') +const {get, merge} = require('lodash') const {stripHtml} = require('string-strip-html') +const defaultConfig = require('./posthtml/defaultConfig') const self = { handleCustomTags: (html, config = {}) => { - const posthtmlOptions = get(config, 'build.posthtml.options', {}) + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) const posthtmlPlugin = () => tree => { const process = node => { diff --git a/src/generators/posthtml/defaultConfig.js b/src/generators/posthtml/defaultConfig.js new file mode 100644 index 00000000..5ce80074 --- /dev/null +++ b/src/generators/posthtml/defaultConfig.js @@ -0,0 +1,3 @@ +module.exports = { + recognizeNoValueAttribute: true +} diff --git a/src/generators/posthtml.js b/src/generators/posthtml/index.js similarity index 92% rename from src/generators/posthtml.js rename to src/generators/posthtml/index.js index 2a3d6d32..98248316 100644 --- a/src/generators/posthtml.js +++ b/src/generators/posthtml/index.js @@ -4,6 +4,7 @@ const {get, merge} = require('lodash') const fetch = require('posthtml-fetch') const layouts = require('posthtml-extend') const modules = require('posthtml-modules') +const defaultConfig = require('./defaultConfig') const expressions = require('posthtml-expressions') module.exports = async (html, config) => { @@ -14,7 +15,7 @@ module.exports = async (html, config) => { const modulesRoot = modulesOptions.root || './' const modulesFrom = modulesOptions.from || `${modulesRoot}/fake` - const posthtmlOptions = merge({recognizeNoValueAttribute: true}, get(config, 'build.posthtml.options', {})) + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) const posthtmlPlugins = get(config, 'build.posthtml.plugins', []) const expressionsOptions = merge({strictMode: false}, get(config, 'build.posthtml.expressions', {})) diff --git a/src/transformers/attributeToStyle.js b/src/transformers/attributeToStyle.js index 16f643af..e39a734b 100644 --- a/src/transformers/attributeToStyle.js +++ b/src/transformers/attributeToStyle.js @@ -1,9 +1,10 @@ const posthtml = require('posthtml') const parseAttrs = require('posthtml-attrs-parser') -const {get, forEach, intersection, keys, isEmpty} = require('lodash') +const defaultConfig = require('../generators/posthtml/defaultConfig') +const {get, merge, forEach, intersection, keys, isEmpty} = require('lodash') module.exports = async (html, config = {}, direct = false) => { - const posthtmlOptions = get(config, 'build.posthtml.options', {}) + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) const attributes = get(config, 'inlineCSS.attributeToStyle', false) if (typeof attributes === 'boolean' && attributes) { diff --git a/src/transformers/baseUrl.js b/src/transformers/baseUrl.js index 59abac0e..023251c1 100644 --- a/src/transformers/baseUrl.js +++ b/src/transformers/baseUrl.js @@ -1,7 +1,33 @@ const posthtml = require('posthtml') const isUrl = require('is-url-superb') const baseUrl = require('posthtml-base-url') -const {get, isObject, isEmpty} = require('lodash') +const {get, merge, isObject, isEmpty} = require('lodash') +const defaultConfig = require('../generators/posthtml/defaultConfig') + +module.exports = async (html, config = {}, direct = false) => { + const url = direct ? config : get(config, 'baseURL') + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) + + // Handle `baseUrl` as a string + if (typeof url === 'string' && url.length > 0) { + html = rewriteVMLs(html, url) + + return posthtml([ + baseUrl({url, allTags: true, styleTag: true, inlineCss: true}) + ]) + .process(html, posthtmlOptions) + .then(result => result.html) + } + + // Handle `baseUrl` as an object + if (isObject(url) && !isEmpty(url)) { + html = rewriteVMLs(html, url.url) + + return posthtml([baseUrl(url)]).process(html, posthtmlOptions).then(result => result.html) + } + + return html +} /** * VML backgrounds must be handled with regex because @@ -42,28 +68,3 @@ const rewriteVMLs = (html, url) => { return html } - -module.exports = async (html, config = {}, direct = false) => { - const url = direct ? config : get(config, 'baseURL') - const posthtmlOptions = get(config, 'build.posthtml.options', {}) - - // Handle `baseUrl` as a string - if (typeof url === 'string' && url.length > 0) { - html = rewriteVMLs(html, url) - - return posthtml([ - baseUrl({url, allTags: true, styleTag: true, inlineCss: true}) - ]) - .process(html, posthtmlOptions) - .then(result => result.html) - } - - // Handle `baseUrl` as an object - if (isObject(url) && !isEmpty(url)) { - html = rewriteVMLs(html, url.url) - - return posthtml([baseUrl(url)]).process(html, posthtmlOptions).then(result => result.html) - } - - return html -} diff --git a/src/transformers/extraAttributes.js b/src/transformers/extraAttributes.js index 1a7f4238..c4725f13 100644 --- a/src/transformers/extraAttributes.js +++ b/src/transformers/extraAttributes.js @@ -1,13 +1,14 @@ const posthtml = require('posthtml') -const {get, isObject} = require('lodash') +const {get, merge, isObject} = require('lodash') const addAttributes = require('posthtml-extra-attributes') +const defaultConfig = require('../generators/posthtml/defaultConfig') module.exports = async (html, config = {}, direct = false) => { if (get(config, 'extraAttributes') === false) { return html } - const posthtmlOptions = get(config, 'build.posthtml.options', {}) + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) let attributes = { table: { diff --git a/src/transformers/filters/index.js b/src/transformers/filters/index.js index 495ccb16..83e7e94b 100644 --- a/src/transformers/filters/index.js +++ b/src/transformers/filters/index.js @@ -5,13 +5,14 @@ const PostCSS = require('../../generators/postcss') const posthtmlContent = require('posthtml-content') const Tailwind = require('../../generators/tailwindcss') const safeClassNames = require('posthtml-safe-class-names') +const defaultConfig = require('../../generators/posthtml/defaultConfig') module.exports = async (html, config = {}, direct = false) => { const filters = direct ? merge(defaultFilters, config) : merge(defaultFilters, get(config, 'filters', {})) - const posthtmlOptions = get(config, 'build.posthtml.options', {}) + const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {})) /** * Compile CSS in