@emailens/engine 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -10115,45 +10115,100 @@ function auditEmail(html, options) {
10115
10115
  var cheerio13 = __toESM(require("cheerio"), 1);
10116
10116
  function toPlainText(html) {
10117
10117
  const $ = cheerio13.load(html);
10118
- $("style, script").remove();
10118
+ $("style, script, head").remove();
10119
10119
  $("[data-skip-in-text='true']").remove();
10120
- $("hr").replaceWith("\n---\n");
10121
- $("br").replaceWith("\n");
10122
- $("img").each((_, el) => {
10123
- const alt = $(el).attr("alt");
10124
- $(el).replaceWith(alt ? alt : "");
10125
- });
10126
- $("a").each((_, el) => {
10127
- const $el = $(el);
10128
- const href = $el.attr("href") || "";
10129
- const text2 = $el.text().trim();
10130
- if (!href || href === text2) {
10131
- $el.replaceWith(text2 || href);
10132
- } else if (!text2) {
10133
- $el.replaceWith(href);
10134
- } else {
10135
- $el.replaceWith(`${text2} (${href})`);
10120
+ const lines = [];
10121
+ let currentLine = "";
10122
+ const BLOCK_TAGS = /* @__PURE__ */ new Set([
10123
+ "p",
10124
+ "div",
10125
+ "h1",
10126
+ "h2",
10127
+ "h3",
10128
+ "h4",
10129
+ "h5",
10130
+ "h6",
10131
+ "tr",
10132
+ "table",
10133
+ "blockquote",
10134
+ "ul",
10135
+ "ol",
10136
+ "li",
10137
+ "section",
10138
+ "article",
10139
+ "header",
10140
+ "footer",
10141
+ "main",
10142
+ "td",
10143
+ "th"
10144
+ // treat cells as blocks to avoid run-on text
10145
+ ]);
10146
+ const SKIP_TAGS = /* @__PURE__ */ new Set(["style", "script", "head"]);
10147
+ function flushLine() {
10148
+ const trimmed = currentLine.trim();
10149
+ if (trimmed) lines.push(trimmed);
10150
+ currentLine = "";
10151
+ }
10152
+ function walk5(node) {
10153
+ var _a;
10154
+ if (node.type === "text") {
10155
+ const text = node.data.replace(/\s+/g, " ");
10156
+ if (text.trim()) {
10157
+ currentLine += text;
10158
+ }
10159
+ return;
10160
+ }
10161
+ if (node.type !== "tag") return;
10162
+ const el = node;
10163
+ const tag = el.tagName.toLowerCase();
10164
+ if (SKIP_TAGS.has(tag)) return;
10165
+ if (tag === "br") {
10166
+ flushLine();
10167
+ return;
10168
+ }
10169
+ if (tag === "hr") {
10170
+ flushLine();
10171
+ lines.push("---");
10172
+ return;
10173
+ }
10174
+ if (tag === "img") {
10175
+ const alt = (_a = $(el).attr("alt")) == null ? void 0 : _a.trim();
10176
+ if (alt) currentLine += alt;
10177
+ return;
10178
+ }
10179
+ if (tag === "a") {
10180
+ const href = $(el).attr("href") || "";
10181
+ const text = $(el).text().trim();
10182
+ if (!href || href === "#" || href === text) {
10183
+ currentLine += text || href;
10184
+ } else if (!text) {
10185
+ currentLine += href;
10186
+ } else {
10187
+ currentLine += `${text} (${href})`;
10188
+ }
10189
+ return;
10190
+ }
10191
+ const isBlock = BLOCK_TAGS.has(tag);
10192
+ if (isBlock) flushLine();
10193
+ if (tag === "li") {
10194
+ currentLine = "- ";
10195
+ }
10196
+ for (const child of el.children) {
10197
+ walk5(child);
10198
+ }
10199
+ if (isBlock) flushLine();
10200
+ }
10201
+ const body = $("body");
10202
+ const root = body.length ? body[0] : $.root()[0];
10203
+ if (root && "children" in root) {
10204
+ for (const child of root.children) {
10205
+ walk5(child);
10136
10206
  }
10137
- });
10138
- $("li").each((_, el) => {
10139
- const $el = $(el);
10140
- const text2 = $el.text().trim();
10141
- $el.replaceWith(`
10142
- - ${text2}
10143
- `);
10144
- });
10145
- const blockTags = "p, div, h1, h2, h3, h4, h5, h6, tr, blockquote, ul, ol";
10146
- $(blockTags).each((_, el) => {
10147
- const $el = $(el);
10148
- $el.prepend("\n\n");
10149
- $el.append("\n\n");
10150
- });
10151
- let text = $("body").text();
10152
- if (!text.trim()) {
10153
- text = $.root().text();
10154
10207
  }
10155
- text = text.replace(/\n{3,}/g, "\n\n");
10156
- return text.trim();
10208
+ flushLine();
10209
+ let result = lines.join("\n");
10210
+ result = result.replace(/\n{3,}/g, "\n\n");
10211
+ return result.trim();
10157
10212
  }
10158
10213
 
10159
10214
  // src/session.ts