|
| 1 | +import findConditionalComments from '@ciolabs/html-find-conditional-comments'; |
| 2 | +import { preserve, restore } from '@ciolabs/html-preserve-comment-whitespace'; |
| 3 | +import jsBeautify from 'js-beautify'; |
| 4 | +import MagicString from 'magic-string'; |
| 5 | +import pretty from 'pretty'; |
| 6 | + |
| 7 | +export default function emailFormatter( |
| 8 | + html: string, |
| 9 | + options?: Parameters<typeof pretty>[1] & jsBeautify.HTMLBeautifyOptions |
| 10 | +): string { |
| 11 | + const id = Math.random(); |
| 12 | + const opener = `<!--${id}`; |
| 13 | + const closer = `${id}-->`; |
| 14 | + |
| 15 | + html = closeConditionalComments(html, { opener, closer }); |
| 16 | + |
| 17 | + const cache = preserve(html); |
| 18 | + html = pretty(html, options); |
| 19 | + html = restore(html, cache); |
| 20 | + html = openConditionalComments(html, { opener, closer }); |
| 21 | + html = alignOpenAndCloseOfConditionalComments(html); |
| 22 | + |
| 23 | + return html; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Close downlevel-hidden conditional comments |
| 28 | + * |
| 29 | + * Example: |
| 30 | + * <!--[if mso]> |
| 31 | + * <p>Content</p> |
| 32 | + * <![endif]--> |
| 33 | + * |
| 34 | + * Becomes: |
| 35 | + * <!--[if mso]>123456789--> |
| 36 | + * <p>Content</p> |
| 37 | + * <!--123456789<![endif]--> |
| 38 | + * |
| 39 | + * This opens the the MSO HTML to be formatted by the HTML formatter. |
| 40 | + */ |
| 41 | +function closeConditionalComments(html: string, { opener, closer }: { opener: string; closer: string }) { |
| 42 | + const comments = findConditionalComments(html); |
| 43 | + const s = new MagicString(html); |
| 44 | + |
| 45 | + for (const comment of comments) { |
| 46 | + const contentStartIndex = comment.range[0] + comment.open.length; |
| 47 | + const contentEndIndex = comment.range[1] - comment.close.length; |
| 48 | + |
| 49 | + s.appendLeft(contentStartIndex, closer); |
| 50 | + s.appendLeft(contentEndIndex, opener); |
| 51 | + } |
| 52 | + |
| 53 | + return s.toString(); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Open downlevel-hidden conditional comments |
| 58 | + * |
| 59 | + * Example: |
| 60 | + * <!--[if mso]>123456789--> |
| 61 | + * <p>Content</p> |
| 62 | + * <!--123456789<![endif]--> |
| 63 | + * |
| 64 | + * Becomes: |
| 65 | + * <!--[if mso]> |
| 66 | + * <p>Content</p> |
| 67 | + * <![endif]--> |
| 68 | + * |
| 69 | + * This reverses the effect of `closeConditionalComments`. After the HTML |
| 70 | + * formatter has formatted the MSO HTML, we restore the original state. |
| 71 | + */ |
| 72 | +function openConditionalComments(html: string, { opener, closer }: { opener: string; closer: string }) { |
| 73 | + return html.replaceAll(new RegExp(`(${opener}|${closer})`, 'g'), ''); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Align open and close tags of conditional comments |
| 78 | + */ |
| 79 | +function alignOpenAndCloseOfConditionalComments(html: string) { |
| 80 | + const comments = findConditionalComments(html); |
| 81 | + const s = new MagicString(html); |
| 82 | + |
| 83 | + for (const comment of comments) { |
| 84 | + const code = new Set(html.slice(comment.range[0], comment.range[1])); |
| 85 | + if (!code.has('\n') && !code.has('\r')) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + const { whitespace: openLeadingWhitespace } = parseWhitespaceAndTextOfLastLine( |
| 90 | + html.slice(0, Math.max(0, comment.range[0])) |
| 91 | + ); |
| 92 | + const { whitespace: closeLeadingWhitespace, text: closeLeadingText } = parseWhitespaceAndTextOfLastLine( |
| 93 | + html.slice(comment.range[0], comment.range[1] - comment.close.length) |
| 94 | + ); |
| 95 | + |
| 96 | + if (openLeadingWhitespace.length === closeLeadingWhitespace.length) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if (openLeadingWhitespace.length > closeLeadingWhitespace.length) { |
| 101 | + s.overwrite(comment.range[0] - openLeadingWhitespace.length, comment.range[0], closeLeadingWhitespace); |
| 102 | + } else { |
| 103 | + s.overwrite( |
| 104 | + comment.range[1] - comment.close.length - closeLeadingText.length - closeLeadingWhitespace.length, |
| 105 | + comment.range[1] - comment.close.length - closeLeadingText.length, |
| 106 | + openLeadingWhitespace |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return s.toString(); |
| 112 | +} |
| 113 | + |
| 114 | +function parseWhitespaceAndTextOfLastLine(string_: string): { |
| 115 | + whitespace: string; |
| 116 | + text: string; |
| 117 | +} { |
| 118 | + const lastLine = string_.split('\n').at(-1) ?? ''; |
| 119 | + const match = /^[^\S\n\r]*/.exec(lastLine); |
| 120 | + const whitespace = match ? match[0] : ''; |
| 121 | + const text = lastLine.slice(whitespace.length); |
| 122 | + |
| 123 | + return { whitespace, text }; |
| 124 | +} |
0 commit comments