Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/shared/src/utils/html-to-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ describe("htmlToPlainText", () => {
expect(htmlToPlainText("©")).toBe("\u00A9"); // ©
});

it("decodes common currency and symbol named entities", () => {
expect(htmlToPlainText("Price £5, €9, ¥100, ¢50")).toBe(
"Price £5, €9, ¥100, ¢50"
); // pound euro yen cent
expect(htmlToPlainText("5° × ½ ÷ ±")).toBe(
"5° × ½ ÷ ±"
); // deg times frac12 divide plusmn
expect(htmlToPlainText("§2 ¶3 µg")).toBe(
"§2 ¶3 µg"
); // sect para micro
});

it("decodes typographic quote named entities", () => {
expect(htmlToPlainText("“Hi” ‘yo’")).toBe(
"“Hi” ‘yo’"
);
});

it("decodes Latin-1 accented named entities", () => {
// Cafe resume naive Zoe Francois Munoz Strasse (with accents)
expect(
htmlToPlainText(
"Café résumé naïve Zoë François Muñoz Straße"
)
).toBe(
"Café résumé naïve Zoë François Muñoz Straße"
);
});

it("leaves unknown named entities untouched", () => {
expect(htmlToPlainText("&notanentity; x")).toBe("&notanentity; x");
});

// Skip elements
it("skips elements with data-skip-in-text attribute", () => {
const html = '<div data-skip-in-text="true">hidden preview</div><p>Visible content</p>';
Expand Down
62 changes: 61 additions & 1 deletion packages/shared/src/utils/html-to-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,86 @@
*/

/**
* Common HTML entity map for decoding.
* Common HTML named entity map for decoding.
*
* Covers the entities that actually appear in email/marketing content —
* currency, typographic punctuation, common symbols, and the Latin-1 accented
* letters used in European names and words. Kept as a curated map (rather than
* the full HTML5 named-reference set, which is ~2000 entries) so the bundle
* stays within its size budget. Numeric entities (`&#123;`, `&#x1a;`) are
* handled separately below, so anything missing here still decodes if written
* numerically.
*/
const HTML_ENTITIES: Record<string, string> = {
// Core markup + whitespace
"&amp;": "&",
"&lt;": "<",
"&gt;": ">",
"&quot;": '"',
"&#39;": "'",
"&apos;": "'",
"&nbsp;": " ",
// Dashes, quotes, and typographic punctuation
"&mdash;": "—",
"&ndash;": "–",
"&laquo;": "«",
"&raquo;": "»",
"&lsquo;": "‘",
"&rsquo;": "’",
"&ldquo;": "“",
"&rdquo;": "”",
"&bull;": "•",
"&middot;": "·",
"&hellip;": "…",
// Currency
"&cent;": "¢",
"&pound;": "£",
"&yen;": "¥",
"&euro;": "€",
// Legal / trademark
"&copy;": "©",
"&reg;": "®",
"&trade;": "™",
"&sect;": "§",
"&para;": "¶",
// Math and misc symbols
"&deg;": "°",
"&plusmn;": "±",
"&times;": "×",
"&divide;": "÷",
"&micro;": "µ",
"&frac14;": "¼",
"&frac12;": "½",
"&frac34;": "¾",
// Latin-1 accented letters (common in European names/words)
"&szlig;": "ß",
"&agrave;": "à",
"&aacute;": "á",
"&acirc;": "â",
"&atilde;": "ã",
"&auml;": "ä",
"&aring;": "å",
"&aelig;": "æ",
"&ccedil;": "ç",
"&egrave;": "è",
"&eacute;": "é",
"&ecirc;": "ê",
"&euml;": "ë",
"&igrave;": "ì",
"&iacute;": "í",
"&icirc;": "î",
"&iuml;": "ï",
"&ntilde;": "ñ",
"&ograve;": "ò",
"&oacute;": "ó",
"&ocirc;": "ô",
"&otilde;": "õ",
"&ouml;": "ö",
"&oslash;": "ø",
"&ugrave;": "ù",
"&uacute;": "ú",
"&ucirc;": "û",
"&uuml;": "ü",
};

/**
Expand Down
Loading