-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1162 from maizzle/fix/revert-prettier
- Loading branch information
Showing
8 changed files
with
857 additions
and
214 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(/"/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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> </i> | ||
<![endif]--> | ||
<span>Read more →</span> | ||
<!--[if mso]> | ||
<i> </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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters