Skip to content

Commit

Permalink
revert: exposing tansformers in events
Browse files Browse the repository at this point in the history
they can be simply imported instead
  • Loading branch information
cossssmin committed Jul 24, 2024
1 parent d048b08 commit 9f0797c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 56 deletions.
5 changes: 1 addition & 4 deletions src/generators/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import expressions from 'posthtml-expressions'
import { parseFrontMatter } from '../utils/node.js'
import defaultConfig from '../posthtml/defaultConfig.js'
import { process as compilePostHTML } from '../posthtml/index.js'
import { run as useTransformers, transformers } from '../transformers/index.js'
import { run as useTransformers } from '../transformers/index.js'

export async function render(html = '', config = {}) {
if (typeof html !== 'string') {
Expand Down Expand Up @@ -70,7 +70,6 @@ export async function render(html = '', config = {}) {
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
transform: transformers,
})) ?? content
}

Expand All @@ -94,7 +93,6 @@ export async function render(html = '', config = {}) {
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
transform: transformers,
})) ?? compiled.html
}

Expand Down Expand Up @@ -130,7 +128,6 @@ export async function render(html = '', config = {}) {
matter: matterData,
config: templateConfig,
posthtml: compilePostHTML,
transform: transformers,
})) ?? compiled.html
}

Expand Down
67 changes: 23 additions & 44 deletions src/transformers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import posthtml from 'posthtml'
import get from 'lodash-es/get.js'
import { defu as merge } from 'defu'

import core from './core.js'
import comb from './comb.js'
import sixHex from './sixHex.js'
import minify from './minify.js'
Expand All @@ -21,19 +22,17 @@ import replaceStrings from './replaceStrings.js'
import attributeToStyle from './attributeToStyle.js'
import removeAttributes from './removeAttributes.js'

import coreTransformers from './core.js'

import defaultPosthtmlConfig from '../posthtml/defaultConfig.js'

/**
* Use Maizzle Transformers on an HTML string.
*
* Only Transformers that are enabled in the `config` will be used.
* Only Transformers that are enabled in the passed
* `config` parameter will be used.
*
* @param {string} html The HTML content
* @param {object} config The Maizzle config object
* @returns {Promise<{ original: string, config: object, html: string }>}
* A Promise resolving to an object containing the original HTML, modified HTML, and the config
* @returns {Promise<{ html: string }>} A Promise resolving to an object containing the modified HTML
*/
export async function run(html = '', config = {}) {
const posthtmlPlugins = []
Expand All @@ -46,16 +45,16 @@ export async function run(html = '', config = {}) {
/**
* 1. Core transformers
*
* Transformers that are always enabled
* Transformers that are always enabled.
*
*/
posthtmlPlugins.push(coreTransformers(config))
posthtmlPlugins.push(core(config))

/**
* 2. Safe class names
*
* Rewrite Tailwind CSS class names to email-safe alternatives,
* unless explicitly disabled
* unless explicitly disabled.
*/
if (get(config, 'css.safe') !== false) {
posthtmlPlugins.push(
Expand All @@ -66,7 +65,6 @@ export async function run(html = '', config = {}) {
/**
* 3. Filters
*
* Apply filters to HTML.
* Filters are always applied, unless explicitly disabled.
*/
if (get(config, 'filters') !== false) {
Expand All @@ -78,7 +76,7 @@ export async function run(html = '', config = {}) {
/**
* 4. Markdown
*
* Convert Markdown to HTML with Markdown-it, unless explicitly disabled
* Convert Markdown to HTML with markdown-it, unless explicitly disabled.
*/
if (get(config, 'markdown') !== false) {
posthtmlPlugins.push(
Expand All @@ -88,7 +86,9 @@ export async function run(html = '', config = {}) {

/**
* 5. Prevent widow words
* Always runs, unless explicitly disabled
*
* Enabled by default, will prevent widow words in elements
* wrapped with a `prevent-widows` attribute.
*/
if (get(config, 'widowWords') !== false) {
posthtmlPlugins.push(
Expand All @@ -110,7 +110,7 @@ export async function run(html = '', config = {}) {
/**
* 7. Inline CSS
*
* Inline CSS into HTML
* Inline CSS into HTML.
*/
if (get(config, 'css.inline')) {
posthtmlPlugins.push(inlineCSS(
Expand Down Expand Up @@ -150,7 +150,7 @@ export async function run(html = '', config = {}) {
/**
* 10. Shorthand CSS
*
* Convert longhand CSS properties to shorthand in `style` attributes
* Convert longhand CSS properties to shorthand in `style` attributes.
*/
if (get(config, 'css.shorthand')) {
posthtmlPlugins.push(
Expand All @@ -161,7 +161,7 @@ export async function run(html = '', config = {}) {
/**
* 11. Add attributes
*
* Add attributes to HTML tags
* Add attributes to HTML tags.
*/
if (get(config, 'attributes.add') !== false) {
posthtmlPlugins.push(
Expand All @@ -172,15 +172,15 @@ export async function run(html = '', config = {}) {
/**
* 12. Base URL
*
* Add a base URL to relative paths
* Add a base URL to relative paths.
*/
if (get(config, 'baseURL', get(config, 'baseUrl'))) {
posthtmlPlugins.push(
baseUrl(get(config, 'baseURL', get(config, 'baseUrl', {})))
)
} else {
/**
* Set baseURL to `build.static.destination` if it's not already set
* Set baseURL to `build.static.destination` if it's not already set.
*/
const destination = get(config, 'build.static.destination', '')
if (destination && !config._dev) {
Expand All @@ -198,7 +198,7 @@ export async function run(html = '', config = {}) {
/**
* 13. URL parameters
*
* Add parameters to URLs
* Add parameters to URLs.
*/
if (get(config, 'urlParameters')) {
posthtmlPlugins.push(
Expand All @@ -209,8 +209,7 @@ export async function run(html = '', config = {}) {
/**
* 14. Six-digit HEX
*
* Convert three-digit HEX colors to six-digit
* Always runs, unless explicitly disabled
* Enabled by default, converts three-digit HEX colors to six-digit.
*/
if (get(config, 'css.sixHex') !== false) {
posthtmlPlugins.push(
Expand All @@ -221,7 +220,7 @@ export async function run(html = '', config = {}) {
/**
* 15. PostHTML MSO
*
* Simplify writing MSO conditionals for Outlook
* Enabled by default, simplifies writing MSO conditionals for Outlook.
*/
if (get(config, 'outlook') !== false) {
posthtmlPlugins.push(
Expand All @@ -232,7 +231,7 @@ export async function run(html = '', config = {}) {
/**
* 16. Prettify
*
* Pretty-print HTML using js-beautify
* Pretty-print HTML using js-beautify.
*/
if (get(config, 'prettify')) {
posthtmlPlugins.push(
Expand All @@ -243,7 +242,7 @@ export async function run(html = '', config = {}) {
/**
* 17. Minify
*
* Minify HTML using html-crush
* Minify HTML using html-crush.
*/
if (get(config, 'minify')) {
posthtmlPlugins.push(
Expand All @@ -254,14 +253,14 @@ export async function run(html = '', config = {}) {
/**
* 18. <template> tags
*
* Replace <template> tags with their content
* Replace <template> tags with their content.
*/
posthtmlPlugins.push(templateTag())

/**
* 19. Replace strings
*
* Replace strings through regular expressions
* Replace strings through regular expressions.
*/
if (get(config, 'replaceStrings')) {
posthtmlPlugins.push(
Expand All @@ -275,23 +274,3 @@ export async function run(html = '', config = {}) {
html: result.html,
}))
}

export const transformers = {
comb,
sixHex,
minify,
baseUrl,
inlineCSS,
prettify,
filters,
markdown,
posthtmlMso,
shorthandCss,
preventWidows,
addAttributes,
urlParameters,
safeClassNames,
replaceStrings,
attributeToStyle,
removeAttributes,
}
3 changes: 1 addition & 2 deletions test/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ describe.concurrent('Build', () => {
content: ['test/fixtures/build/**/*.html']
}
},
async afterBuild({ files, config, transform }) {
async afterBuild({ files, config }) {
ctx.afterBuild = files
expect(config).toBeInstanceOf(Object)
expect(files).toBeInstanceOf(Array)
expect(transform.inlineCSS).toBeInstanceOf(Function)
}
}
)
Expand Down
9 changes: 3 additions & 6 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ describe.concurrent('Render', () => {

test('Runs the `beforeRender` event', async () => {
const { html } = await render('<div class="inline">{{ page.foo }}</div>', {
beforeRender({ config, posthtml, transform }) {
beforeRender({ config, posthtml }) {
config.foo = 'bar'

expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(transform.inlineCSS).toBeInstanceOf(Function)
}
})

Expand All @@ -42,14 +41,13 @@ describe.concurrent('Render', () => {

test('Runs the `afterRender` event', async () => {
const { html } = await render('<div class="inline">foo</div>', {
afterRender({ config, posthtml, transform }) {
afterRender({ config, posthtml }) {
config.replaceStrings = {
foo: 'bar'
}

expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(transform.inlineCSS).toBeInstanceOf(Function)
}
})

Expand All @@ -61,10 +59,9 @@ describe.concurrent('Render', () => {
replaceStrings: {
foo: 'bar'
},
afterTransformers({ html, config, posthtml, transform }) {
afterTransformers({ html, config, posthtml }) {
expect(config).toBeInstanceOf(Object)
expect(posthtml).toBeInstanceOf(Function)
expect(transform.inlineCSS).toBeInstanceOf(Function)

return html.replace('bar', 'baz')
}
Expand Down

0 comments on commit 9f0797c

Please sign in to comment.