@maizzle/framework
Version:
Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.
74 lines (73 loc) • 2.33 kB
JavaScript
import { parse } from "../utils/ast/parser.js";
import { walk } from "../utils/ast/walker.js";
import { serialize } from "../utils/ast/serializer.js";
import "../utils/ast/index.js";
import { defu as defu$1 } from "defu";
//#region src/transformers/entities.ts
const DEFAULT_ENTITIES = {
"": "‍",
"": "‌",
"\xA0": " ",
"": "­",
"": "​",
" ": " ",
"": "",
"͏": "͏",
" ": " ",
"\u2028": "&LineSeparator;",
"\u2029": "&ParagraphSeparator;",
"·": "·",
"–": "–",
"—": "—",
"‘": "‘",
"’": "’",
"“": "“",
"”": "”",
"«": "«",
"»": "»",
"•": "•",
"‹": "‹",
"›": "›"
};
/**
* Replace literal Unicode characters in text and comment nodes with their
* HTML entity equivalents (zero-width joiners, non-breaking spaces, smart
* quotes, dashes, etc.) for better email-client rendering.
*
* @param html HTML string to transform.
* @param custom Extra entries merged on top of the built-in entity map, or
* `false` to disable the transform. Defaults to `true`
* (built-ins only).
* @returns The transformed HTML string.
*
* @example
* import { entities } from '@maizzle/framework'
*
* // Defaults only
* entities('hello world') // → 'hello world'
*
* // Add a custom mapping
* entities('© Maizzle', { '©': '©' })
*
* // Disable the transform
* entities('hello world', false)
*/
function entities(html, custom = true, scope = {}) {
return serialize(entitiesDom(parse(html), custom, scope));
}
/**
* DOM-form of {@link entities} used by the internal transformer pipeline.
* Takes a parsed DOM, returns a parsed DOM — avoids redundant
* serialize/parse round-trips when chained with other transformers.
*/
function entitiesDom(dom, custom = true, scope = {}) {
if (!custom) return dom;
const map = typeof custom === "object" ? defu$1(custom, DEFAULT_ENTITIES) : DEFAULT_ENTITIES;
walk(dom, (node) => {
if (node.type === "text" && scope.text !== false || node.type === "comment" && scope.comments !== false) for (const [char, entity] of Object.entries(map)) node.data = node.data.split(char).join(entity);
});
return dom;
}
//#endregion
export { entities, entitiesDom };
//# sourceMappingURL=entities.js.map