@zaclinley/dsh-mailharbor 0.4.0 → 0.4.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/lib/index.js CHANGED
@@ -1753,6 +1753,18 @@ ${clip(sample.text, 800)}`).join("\n\n");
1753
1753
  return catalog;
1754
1754
  }
1755
1755
  };
1756
+ function normalizeTranslated(value) {
1757
+ const raw = value.trim();
1758
+ if (raw === "") return { text: "", html: "" };
1759
+ if (!/<[a-z][\s\S]*>/i.test(raw)) return { text: raw, html: textToHtml(raw) };
1760
+ let body = raw;
1761
+ const match = /<body[^>]*>([\s\S]*?)<\/body>/i.exec(raw);
1762
+ if (match !== null) body = match[1] ?? "";
1763
+ body = body.replace(/<!doctype[^>]*>/gi, "").replace(/<head[\s\S]*?<\/head>/gi, "").replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<script[\s\S]*?<\/script>/gi, "").trim();
1764
+ const text2 = htmlToPlain(body);
1765
+ if (body === "" || text2 === "") return { text: raw, html: textToHtml(raw) };
1766
+ return { text: text2, html: body };
1767
+ }
1756
1768
  function htmlToPlain(html) {
1757
1769
  return html.replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<script[\s\S]*?<\/script>/gi, "").replace(/<br\s*\/?>/gi, "\n").replace(/<\/(p|div|li|tr|h[1-6])>/gi, "\n").replace(/<[^>]+>/g, "").replace(/&nbsp;/g, " ").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/\n{3,}/g, "\n\n").trim();
1758
1770
  }
@@ -1783,7 +1795,7 @@ function textToHtml(text2) {
1783
1795
  }
1784
1796
 
1785
1797
  // src/host/version.ts
1786
- var VERSION = "0.4.0";
1798
+ var VERSION = "0.4.1";
1787
1799
 
1788
1800
  // src/host/api.ts
1789
1801
  init_smtp();
@@ -2139,8 +2151,9 @@ function buildRouter(runtime) {
2139
2151
  };
2140
2152
  } else if (kind === "translate") {
2141
2153
  const source = detail.html !== null && detail.html !== "" ? detail.html : detail.text;
2142
- output = await ai.translate(source, target);
2143
- html = /<[a-z][\s\S]*>/i.test(output) ? output : textToHtml(output);
2154
+ const translated = normalizeTranslated(await ai.translate(source, target));
2155
+ output = translated.text;
2156
+ html = translated.html;
2144
2157
  meta = { target };
2145
2158
  } else {
2146
2159
  const earlier = runtime.aiThreads(account).get(key)?.cards.filter((card2) => card2.kind === "answer").map((card2) => card2.output) ?? [];
@@ -2230,8 +2243,8 @@ function buildRouter(runtime) {
2230
2243
  const html = typeof payload.html === "string" ? payload.html : "";
2231
2244
  const target = payload.target === "en" ? "en" : "zh";
2232
2245
  try {
2233
- const text2 = await runtime.ai().translate(html, target);
2234
- return { text: text2, html: /<[a-z][\s\S]*>/i.test(text2) ? text2 : textToHtml(text2) };
2246
+ const translated = normalizeTranslated(await runtime.ai().translate(html, target));
2247
+ return { text: translated.text, html: translated.html };
2235
2248
  } catch (error) {
2236
2249
  throw aiHttpError(error);
2237
2250
  }