/* «يقبل الرسالة متنسقة» (2026-11-15): a design copied out of Word, or out of ChatGPT or Claude, carries its structure in the clipboard's HTML - headings, lists inside lists, Word's numbered paragraphs - and a plain text box keeps only the words, so three levels arrive as one column of lines. This turns that HTML into Markdown, which keeps every level as `#` or as indentation, and is what the box receives instead. String-based on purpose: no DOM is needed, so it is tested as it is. */ const ENTITIES: Record = { " ": " ", "&": "&", "<": "<", ">": ">", """: '"', "'": "'", "‏": "", "‎": "", }; function decode(text: string): string { return text .replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n))) .replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16))) .replace(/&[a-z]+;/gi, (e) => ENTITIES[e.toLowerCase()] ?? e); } /** Whether the HTML carries structure worth keeping - else the plain paste is left alone. */ export function hasStructure(html: string): boolean { return /<(h[1-6]|li)\b/i.test(html) || /mso-list\s*:\s*l\d+\s+level\d/i.test(html); } /** The clipboard's HTML as Markdown: headings as `#`, list items indented two spaces a level with `-`, Word's list paragraphs by their `level`. */ export function htmlToMarkdown(html: string): string { const body = html .replace(//g, "") // Word's [if !supportLists] .replace(/<(style|script|head|title)\b[\s\S]*?<\/\1>/gi, ""); const tokens = body.match(/<[^>]+>|[^<]+/g) ?? []; const lines: string[] = []; let line = ""; let prefix = ""; const lists: ("ul" | "ol")[] = []; let ignore = 0; // inside Word's «mso-list:Ignore» span: the drawn bullet // A line is written when it has words. An element that opens with none - // the

ChatGPT and Claude put inside every

  • - keeps the marker the // list item set, or the bullets vanished (TAZA brief, 2026-11-16). const flush = () => { const text = decode(line).replace(/\s+/g, " ").replace(/\*\*\s*\*\*/g, "") .replace(/\*\*\s+/g, "**").replace(/\s+\*\*(?=\s|$)/g, "**").trim(); if (text && text !== "**") { lines.push(prefix + text); prefix = ""; } line = ""; }; for (const token of tokens) { if (!token.startsWith("<")) { if (!ignore) line += token; continue; } const closing = token.startsWith("