Skip to content

Commit

Permalink
revert: use prettier for prettify transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Jan 27, 2024
1 parent 4886a06 commit e8fa91d
Show file tree
Hide file tree
Showing 8 changed files with 857 additions and 214 deletions.
792 changes: 757 additions & 35 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -71,13 +71,14 @@
"posthtml-postcss-merge-longhand": "^2.0.1",
"posthtml-safe-class-names": "^3.0.0",
"posthtml-url-parameters": "^2.0.0",
"prettier": "^3.1.1",
"pretty": "^2.0.0",
"query-string": "^7.1.3",
"string-remove-widows": "^2.1.0",
"string-strip-html": "^8.2.0",
"tailwindcss": "^3.2.7"
},
"devDependencies": {
"@types/js-beautify": "^1.14.0",
"@types/markdown-it": "^13.0.0",
"ava": "^5.2.0",
"c8": "^9.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/index.d.ts
@@ -1,5 +1,5 @@
import type {StringifyOptions} from 'query-string';
import type {Config as PrettierOptions} from 'prettier';
import type {CoreBeautifyOptions} from 'js-beautify';
import type {Options as MarkdownItOptions} from 'markdown-it';
import type {Opts as PlaintextOptions} from 'string-strip-html';

Expand Down Expand Up @@ -1773,7 +1773,7 @@ declare namespace MaizzleFramework {
}
```
*/
prettify?: boolean | PrettierOptions;
prettify?: boolean | CoreBeautifyOptions;

/**
Minify the compiled HTML email code.
Expand Down Expand Up @@ -2041,9 +2041,9 @@ declare namespace MaizzleFramework {
/**
Pretty print HTML code so that it's nicely indented and more human-readable.
@param {string} html The HTML string to prettify.
@param {PrettierOptions} [options] Options to pass to the prettifier.
@param {CoreBeautifyOptions} [options] Options to pass to the prettifier.
*/
function prettify(html: string, options?: PrettierOptions): string;
function prettify(html: string, options?: CoreBeautifyOptions): string;

/**
Prepend a string to sources and hrefs in an HTML string.
Expand Down
62 changes: 10 additions & 52 deletions src/transformers/prettify.js
@@ -1,69 +1,27 @@
const {format} = require('prettier')
/* eslint-disable camelcase */
const pretty = require('pretty')
const {get, merge, isEmpty, isObject} = require('lodash')

module.exports = async (html, config = {}, direct = false) => {
config = direct ? config : get(config, 'prettify')

const defaultConfig = {
parser: 'html',
printWidth: 500,
htmlWhitespaceSensitivity: 'ignore',
xmlMode: get(config, 'posthtml.options.xmlMode', false),
rewriteSelfClosing: true,
selfClosingTags: [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr'
]
space_around_combinator: true, // Preserve space around CSS selector combinators
newline_between_rules: false, // Remove empty lines between CSS rules
indent_inner_html: false, // Helps reduce file size
extra_liners: [] // Don't add extra new line before any tag
}

config = direct ? config : get(config, 'prettify')

// Don't prettify if not explicitly enabled in config
if (!config || (isObject(config) && isEmpty(config))) {
return html
}

if (typeof config === 'boolean' && config) {
return format(html, defaultConfig).then(html => reFormat(html, defaultConfig))
return pretty(html, defaultConfig)
}

config = merge(defaultConfig, config)

return format(html, config).then(html => reFormat(html, config))
}

const reFormat = (html, config) => {
if (/<!doctype html>/i.test(html) && !config.xmlMode && config.rewriteSelfClosing) {
html = html.replace(new RegExp(`<(${config.selfClosingTags.join('|')})\s?([^>]*?)\s?\/>`, 'g'), (match, p1, p2) => {
return `<${p1}${p2.trimEnd()}>`
})
}

return html
// Fix style="" attributes broken down on multiple lines
.replace(/(\s+style="\s+)([\s\S]*?)(\s+")/g, (match, p1, p2, p3) => {
return p1.replace(/\n\s+?(style)/g, ' $1').trimEnd()
+ p2.replace(/\s+/g, ' ').trim()
+ p3.trim()
})
// Fix closing </pre> tags broken down on multiple lines (</pre>\n\s+>)
.replace(/(<\/pre)\s+>/g, '$1>')
// Undo escaping of quotes in attribute values
.replace(/="(.*?)"/g, (match, p1) => {
return `="${p1.replace(/&quot;/g, '\'')}"`
})
// Fix <tag \n\s+{attrs}\n\s+> => <tag {attrs}>
.replace(/<([^>]+)\n\s*([^>]+)\n\s*>/g, '<$1 $2>')
// Fix <tag {attrs}\n[\s\t]*> => <tag {attrs}>
.replace(/<([^>]+)\n\s*>/g, '<$1>')
return pretty(html, config)
}
158 changes: 70 additions & 88 deletions test/expected/components/kitchen-sink.html
@@ -1,91 +1,73 @@
{{ test }} The Website Weekly Newsletter bar {% capture email_title %}Hey {{customer.first_name}} {{ customer.last_name}}, {% endcapture %}

<!doctype html>
{{ test }}
The Website Weekly Newsletter
bar
{% capture email_title %}Hey {{customer.first_name}} {{ customer.last_name}}, {% endcapture %}
<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<style>
.text-sm {
font-size: 0.875rem;
line-height: 1.25rem;
}
</style>
</head>

<body class="bg-red-500">
<h1 class="text-sm">H1 in fill:template</h1>

<!-- Expressions -->
Compiled: maizzle-ci Ignored: {{ page.env }} Inside component: maizzle-ci Inside component (ignored): {{ page.env }} Inside nested component: maizzle-ci Inside nested component (ignored): {{ page.env }}

<!-- Component slots -->

Slot header

<!-- Markdown -->
<h3>Heading</h3>
<pre><code class="language-js">console.log('Hello world!')
<head>
<style>
</style>
</head>
<body class="bg-red-500">
<h1>H1 in fill:template</h1>

<!-- Expressions -->
Compiled: maizzle-ci
Ignored: {{ page.env }}
Inside component: maizzle-ci
Inside component (ignored): {{ page.env }}
Inside nested component: maizzle-ci
Inside nested component (ignored): {{ page.env }}

<!-- Component slots -->
Slot header

<!-- Markdown -->
<h3>Heading</h3>
<pre><code class="language-js">console.log('Hello world!')
</code></pre>

<h3>Markdown in component</h3>

<!-- Passing props to component -->

Environment: maizzle-ci Environment (ignore expression): {{ page.env }}

<!-- Nesting components, with props -->

Parent Childish

<!-- <fetch> tag -->

<ul>
<li>Leanne Graham</li>

<li>Ervin Howell</li>

<li>Clementine Bauch (last item)</li>
</ul>

<!-- <outlook> tag -->

<!--[if mso]>in outlook<![endif]-->

<!-- Conditionals -->

<p>Using Maizzle programmatically</p>

<!-- Loops -->

{{ i }} is
<i>1</i>

{{ i }} is
<i>2 (last)</i>

<!-- Switch -->

page.foo is bar

<!-- Scope -->

Author: John Editor: Jane

<!-- Raw -->

Expression should be ignored: {{ foo }}

<!-- MSO Comments -->

<div>
<a href="#">
<!--[if mso]>
<i>&nbsp;</i>
<![endif]-->
<span>Read more &rarr;</span>
<!--[if mso]>
<i>&nbsp;</i>
<![endif]-->
</a>
</div>
</body>
<h3>Markdown in component</h3>

<!-- Passing props to component -->
Environment: maizzle-ci
Environment (ignore expression): {{ page.env }}

<!-- Nesting components, with props -->
Parent
Childish

<!-- <fetch> tag -->
<ul>
<li>
Leanne Graham
</li>
<li>
Ervin Howell
</li>
<li>
Clementine Bauch (last item)
</li>
</ul>

<!-- <outlook> tag -->

<!--[if mso]>in outlook<![endif]-->

<!-- Conditionals -->
<p>Using Maizzle programmatically</p>

<!-- Loops -->
{{ i }} is <i>1</i>
{{ i }} is <i>2 (last)</i>

<!-- Switch -->
page.foo is bar

<!-- Scope -->
Author: John
Editor: Jane

<!-- Raw -->
Expression should be ignored: {{ foo }}
</body>
</html>
18 changes: 1 addition & 17 deletions test/fixtures/components/kitchen-sink.html
Expand Up @@ -19,7 +19,7 @@

<x-layout>
<fill:template>
<h1 class="text-sm">H1 in fill:template</h1>
<h1>H1 in fill:template</h1>

<!-- Expressions -->
Compiled: {{ page.env }}
Expand Down Expand Up @@ -120,21 +120,5 @@ <h1 class="text-sm">H1 in fill:template</h1>
Expression should be ignored: {{ foo }}
</raw>
</x-bare>

<!-- MSO Comments -->

<div>
<a href="#">
<!--[if mso]>
<i>&nbsp;</i>
<![endif]-->
<span>
Read more &rarr;
</span>
<!--[if mso]>
<i>&nbsp;</i>
<![endif]-->
</a>
</div>
</fill:template>
</x-layout>
5 changes: 2 additions & 3 deletions test/test-posthtml.js
Expand Up @@ -118,10 +118,9 @@ test.serial('components', async t => {
}

const html = await renderString(source, options)
const expectedHtml = await expected('components/kitchen-sink')

t.is(
await prettify(html, {printWidth: 500}),
expectedHtml + '\n'
await prettify(html, {ocd: true}),
await expected('components/kitchen-sink')
)
})
25 changes: 11 additions & 14 deletions test/test-transformers.js
Expand Up @@ -240,26 +240,23 @@ test('base URL (object)', async t => {
})

test('prettify', async t => {
const source = await fixture('prettify')

const html = await Maizzle.prettify(source, {
printWidth: 500,
rewriteSelfClosing: true
})

t.is(html.trim(), (await expected('prettify')))

// `prettify: true`
const html2 = await Maizzle.prettify('<!doctype html><meta name="test" /><custom /><div><p>prettify: true</p></div>', true)
t.is(html2, '<!doctype html>\n<meta name="test">\n<custom />\n<div><p>prettify: true</p></div>\n')
const html2 = await Maizzle.prettify('<div><p>test</p></div>', true)

// With custom object config
// eslint-disable-next-line
const html = await Maizzle.prettify('<div><p>test</p></div>', {indent_inner_result: true})

// No config
const html3 = await Maizzle.prettify('<div><p>test</p></div>')
t.is(html3, '<div><p>test</p></div>')

// Empty object config
const html4 = await Maizzle.prettify('<div><p>empty config</p></div>', {})
t.is(html4, '<div><p>empty config</p></div>')
const html4 = await Maizzle.prettify('<div><p>test</p></div>', {})

t.is(html, '<div>\n <p>test</p>\n</div>')
t.is(html2, '<div>\n <p>test</p>\n</div>')
t.is(html3, '<div><p>test</p></div>')
t.is(html4, '<div><p>test</p></div>')
})

test('minify', async t => {
Expand Down

0 comments on commit e8fa91d

Please sign in to comment.