Email Snippet Decoder (email/snippetText.ts)

warpdrive · src/features/email/snippetText.ts · typescript · MIT
email, dev-tools, security, tag:text

Decodes HTML entities and character references within email snippets. Use this to sanitize and format raw email text for display.

// Source: https://github.com/sneg55/warpdrive | License: MIT | Copyright 2026 Nikita Sawinyh
// Provided via justclaw.it.com; keep this notice with the code.
const NAMED_ENTITIES = new Map<string, string>([
  ["amp", "&"],
  ["lt", "<"],
  ["gt", ">"],
  ["quot", '"'],
  ["apos", "'"],
  ["nbsp", " "],
]);

const ENTITY_PATTERN = /&(#\d+|#[xX][0-9a-fA-F]+|[a-zA-Z]+);/g;

function fromCodePoint(raw: string): string | null {
  const code =
    raw[1] === "x" || raw[1] === "X"
      ? Number.parseInt(raw.slice(2), 16)
      : Number.parseInt(raw.slice(1), 10);
  if (!Number.isFinite(code) || code < 1 || code > 0x10_ff_ff) return null;
  return String.fromCodePoint(code);
}

export function decodeEmailSnippet(snippet: string | null): string | null {
  if (snippet === null) return null;
  retu

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key