Skip to content

Commit

Permalink
refactor: running safeClassNames transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Jun 7, 2023
1 parent 8912018 commit e06df04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/transformers/filters/index.js
Expand Up @@ -41,7 +41,11 @@ module.exports = async (html, config = {}, direct = false) => {
posthtmlContent(filters)
]

if (get(config, 'safeClassNames') !== false) {
/**
* Run `safeClassNames` in filters only when not when developing locally and
* `safeClassNames` is not explicitly disabled (set to `false`).
*/
if (get(config, 'env') !== 'local' && get(config, 'safeClassNames') !== false) {
posthtmlPlugins.push(safeClassNames({
replacements: {
'{': '{',
Expand Down
20 changes: 10 additions & 10 deletions src/transformers/safeClassNames.js
@@ -1,20 +1,20 @@
const posthtml = require('posthtml')
const {get, merge} = require('lodash')
const {get, merge, isEmpty} = require('lodash')
const safeClassNames = require('posthtml-safe-class-names')
const defaultConfig = require('../generators/posthtml/defaultConfig')

module.exports = async (html, config = {}, direct = false) => {
const option = get(config, 'safeClassNames')

if (option === false) {
return html
}

/*
* Setting it to `true` in the config will run `safeClassNames`
* no matter the environment.
* Don't run when:
* - `config` is falsy or empty
* - developing locally and `safeClassNames` is not explicitly
* set to `true`
*/
if (config.env === 'local' && !option) {
if (
!config
|| isEmpty(config)
|| (get(config, 'env') === 'local' && get(config, 'safeClassNames') !== true)
) {
return html
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-transformers.js
Expand Up @@ -284,7 +284,7 @@ test('safe class names', async t => {
})

test('safe class names (disabled)', async t => {
const html = await Maizzle.safeClassNames('<div class="sm:text-left">foo</div>', {safeClassNames: false})
const html = await Maizzle.safeClassNames('<div class="sm:text-left">foo</div>')

t.is(html, '<div class="sm:text-left">foo</div>')
})
Expand Down

0 comments on commit e06df04

Please sign in to comment.