From 2fead33a965bf72a53b4e302feb0efbef13f7bc5 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Wed, 29 Apr 2026 16:48:18 -0500 Subject: [PATCH] html to markdown --- .../plugins/markdown/bidirectional.test.ts | 110 ++++++++ .../plugins/markdown/htmlToMarkdown.test.ts | 88 +++++++ src/app/plugins/markdown/htmlToMarkdown.ts | 237 ++++++++++++++++++ src/app/plugins/markdown/injectDataMd.test.ts | 81 ++++++ src/app/plugins/markdown/injectDataMd.ts | 108 ++++++++ 5 files changed, 624 insertions(+) create mode 100644 src/app/plugins/markdown/bidirectional.test.ts create mode 100644 src/app/plugins/markdown/htmlToMarkdown.test.ts create mode 100644 src/app/plugins/markdown/htmlToMarkdown.ts create mode 100644 src/app/plugins/markdown/injectDataMd.test.ts create mode 100644 src/app/plugins/markdown/injectDataMd.ts diff --git a/src/app/plugins/markdown/bidirectional.test.ts b/src/app/plugins/markdown/bidirectional.test.ts new file mode 100644 index 000000000..ab1b09ff1 --- /dev/null +++ b/src/app/plugins/markdown/bidirectional.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from "vitest"; +import { markdownToHtml } from "./markdownToHtml"; +import { htmlToMarkdown } from "./htmlToMarkdown"; +import { injectDataMd } from "./injectDataMd"; + +describe("bidirectional round-trip", () => { + it("round-trips headings", () => { + const markdown = "## Hello World"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("## Hello World"); + }); + + it("round-trips bold text", () => { + const markdown = "**bold text**"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("**bold text**"); + }); + + it("round-trips italic text", () => { + const markdown = "*italic text*"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("*italic text*"); + }); + + it("round-trips inline code", () => { + const markdown = "`inline code`"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("`inline code`"); + }); + + it("round-trips code blocks", () => { + const markdown = "```rust\nfn main() {}\n```"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("```rust"); + expect(result).toContain("fn main()"); + }); + + it("round-trips blockquotes", () => { + const markdown = "> Quote text"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("> Quote text"); + }); + + it("round-trips unordered lists", () => { + const markdown = "- Item 1\n- Item 2"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("- Item 1"); + expect(result).toContain("- Item 2"); + }); + + it("round-trips ordered lists", () => { + const markdown = "1. First\n2. Second"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + // Note: marked normalizes ordered lists to start at 1 + expect(result).toContain("1. First"); + expect(result).toContain("1. Second"); + }); + + it("round-trips spoiler syntax", () => { + const markdown = "||hidden message||"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("||hidden message||"); + }); + + it("round-trips inline math", () => { + const markdown = "$E = mc^2$"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("$E = mc^2$"); + }); + + it("round-trips block math", () => { + const markdown = "$$x + y$$"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + expect(result).toContain("$$x + y$$"); + }); + + it("does NOT parse k. as a list", () => { + const markdown = "k.Hello world"; + const html = markdownToHtml(markdown); + const injected = injectDataMd(html); + const result = htmlToMarkdown(injected); + // Should NOT contain any list markers + expect(result).not.toContain("
  • "); + expect(result).not.toContain("
      "); + expect(result).not.toContain("
        "); + expect(result).toContain("k."); + }); +}); diff --git a/src/app/plugins/markdown/htmlToMarkdown.test.ts b/src/app/plugins/markdown/htmlToMarkdown.test.ts new file mode 100644 index 000000000..3ffcc869c --- /dev/null +++ b/src/app/plugins/markdown/htmlToMarkdown.test.ts @@ -0,0 +1,88 @@ +import { describe, expect, it } from "vitest"; +import { htmlToMarkdown } from "./htmlToMarkdown"; + +describe("htmlToMarkdown", () => { + it("converts headings", () => { + expect(htmlToMarkdown("

        Hello

        ")).toContain("# Hello"); + expect(htmlToMarkdown("

        World

        ")).toContain("## World"); + }); + + it("converts bold text", () => { + expect(htmlToMarkdown("bold")).toContain("**bold**"); + expect(htmlToMarkdown("bold")).toContain("**bold**"); + }); + + it("converts italic text", () => { + expect(htmlToMarkdown("italic")).toContain("*italic*"); + expect(htmlToMarkdown("italic")).toContain("*italic*"); + }); + + it("converts strikethrough", () => { + expect(htmlToMarkdown("deleted")).toContain("~~deleted~~"); + expect(htmlToMarkdown("deleted")).toContain("~~deleted~~"); + }); + + it("converts inline code", () => { + expect(htmlToMarkdown("code")).toContain("`code`"); + }); + + it("converts code blocks", () => { + expect( + htmlToMarkdown( + '
        fn main() {}
        ', + ), + ).toContain("```rust"); + }); + + it("converts links", () => { + expect(htmlToMarkdown('link')).toContain( + "[link](https://example.com)", + ); + }); + + it("converts spoiler spans", () => { + expect(htmlToMarkdown("hidden")).toContain( + "||hidden||", + ); + }); + + it("converts inline math spans", () => { + expect( + htmlToMarkdown('E = mc^2'), + ).toContain("$E = mc^2$"); + }); + + it("converts block math divs", () => { + expect( + htmlToMarkdown('
        frac
        '), + ).toContain("$$\\frac{a}{b}$$"); + }); + + it("converts blockquotes", () => { + const result = htmlToMarkdown("
        Quote text
        "); + expect(result).toContain(">"); + expect(result).toContain("Quote text"); + }); + + it("converts unordered lists", () => { + const result = htmlToMarkdown("
        • Item 1
        • Item 2
        "); + expect(result).toContain("-"); + expect(result).toContain("Item 1"); + }); + + it("converts ordered lists", () => { + const result = htmlToMarkdown("
        1. Item 1
        2. Item 2
        "); + expect(result).toContain("1."); + expect(result).toContain("Item 1"); + }); + + it("preserves data-md attributes for round-trip", () => { + const result = htmlToMarkdown('bold'); + expect(result).toContain("**bold**"); + }); + + it("escapes markdown special characters in text", () => { + const result = htmlToMarkdown("

        Hello *world*

        "); + expect(result).toContain("\\*"); + }); +}); diff --git a/src/app/plugins/markdown/htmlToMarkdown.ts b/src/app/plugins/markdown/htmlToMarkdown.ts new file mode 100644 index 000000000..2e251ea7d --- /dev/null +++ b/src/app/plugins/markdown/htmlToMarkdown.ts @@ -0,0 +1,237 @@ +import parse from "html-dom-parser"; +import type { ChildNode, Element, Text as DomText } from "domhandler"; +import { isText, isTag } from "domhandler"; +import { + escapeMarkdownInlineSequences, + escapeMarkdownBlockSequences, +} from "./utils"; + +/** + * Converts Matrix-compatible HTML back to markdown for round-trip editing. + * Preserves original markdown syntax via data-md attributes and converts + * Matrix-specific elements (spoilers, math) back to their markdown equivalents. + * + * @param html - Input HTML string (should be pre-sanitized) + * @returns Markdown string for editor editing + */ +export function htmlToMarkdown(html: string): string { + const domNodes = parse(html); + return processNodes(domNodes); +} + +function processNodes(nodes: ChildNode[]): string { + return nodes.map(processNode).join(""); +} + +function processNode(node: ChildNode): string { + if (isText(node)) { + return escapeMarkdownInlineSequences(node.data); + } + + if (!isTag(node)) { + return ""; + } + + const tag = node.name.toLowerCase(); + + // Handle Matrix-specific attributes + if (tag === "span") { + if (node.attribs["data-mx-spoiler"] !== undefined) { + return processSpoiler(node); + } + if (node.attribs["data-mx-maths"] !== undefined) { + return processMath(node, "inline"); + } + if (node.attribs["data-md"] !== undefined) { + return processInlineMarkdown(node); + } + } + + if (tag === "div") { + if (node.attribs["data-mx-maths"] !== undefined) { + return processMath(node, "block"); + } + } + + // Handle block elements + switch (tag) { + case "h1": + case "h2": + case "h3": + case "h4": + case "h5": + case "h6": + return processHeading(node, tag); + + case "p": + return processParagraph(node); + + case "strong": + case "b": + return processInlineWrapper(node, "**"); + + case "em": + case "i": + return processInlineWrapper(node, "*"); + + case "u": + return processInlineWrapper(node, "_"); + + case "s": + case "del": + return processInlineWrapper(node, "~~"); + + case "code": + return processCode(node); + + case "pre": + return processPre(node); + + case "blockquote": + return processBlockquote(node); + + case "ul": + return processUnorderedList(node); + + case "ol": + return processOrderedList(node); + + case "li": + return processListItem(node); + + case "a": + return processLink(node); + + case "br": + return "\n"; + + case "hr": + return "\n---\n"; + + case "sub": + return processSubscript(node); + + default: + return processInlineElements(node); + } +} + +function processInlineElements(node: Element): string { + return node.children.map(processNode).join(""); +} + +function processInlineWrapper(node: Element, marker: string): string { + const content = node.children.map(processNode).join(""); + return `${marker}${content}${marker}`; +} + +function processCode(node: Element): string { + const codeContent = node.children.map(processNode).join(""); + + // Check if this is inside a pre (code block) + if (node.parent && isTag(node.parent) && node.parent.name === "pre") { + return codeContent; + } + + // Single backtick for inline code + return `\`${codeContent}\``; +} + +function processPre(node: Element): string { + // Get language from class="language-xxx" + const codeChild = node.children.find( + (c): c is Element => isTag(c) && c.name === "code", + ); + const className = codeChild?.attribs.class ?? ""; + const langMatch = className.match(/language-(\S+)/); + const lang = langMatch ? langMatch[1] : ""; + + const codeContent = codeChild + ? codeChild.children.map(processNode).join("") + : node.children.map(processNode).join(""); + + return `\`\`\`${lang}\n${codeContent}\`\`\``; +} + +function processHeading(node: Element, tag: string): string { + const level = tag.charAt(1); + const content = node.children.map(processNode).join(""); + return `\n${"#".repeat(parseInt(level, 10))} ${content}\n`; +} + +function processParagraph(node: Element): string { + const content = node.children.map(processNode).join(""); + return `${content}\n`; +} + +function processBlockquote(node: Element): string { + const content = node.children + .map((child) => { + if (isTag(child) && child.name === "br") return "\n"; + const text = processNode(child); + return text.replace(/\n/g, "\n> "); + }) + .join(""); + return `> ${content}\n`; +} + +function processUnorderedList(node: Element): string { + const items = node.children + .filter((c): c is Element => isTag(c) && c.name === "li") + .map(processListItem) + .join(""); + return `\n${items}\n`; +} + +function processOrderedList(node: Element): string { + const items = node.children + .filter((c): c is Element => isTag(c) && c.name === "li") + .map((li) => { + const content = li.children.map(processNode).join(""); + return `1. ${content}\n`; + }) + .join(""); + return `\n${items}\n`; +} + +function processListItem(node: Element): string { + const content = node.children + .map((child) => { + if (isTag(child) && child.name === "p") { + return child.children.map(processNode).join(""); + } + return processNode(child); + }) + .join(""); + return `- ${content}\n`; +} + +function processSubscript(node: Element): string { + const content = node.children.map(processNode).join(""); + return `-# ${content}\n`; +} + +function processLink(node: Element): string { + const href = node.attribs.href ?? ""; + const content = node.children.map(processNode).join(""); + return `[${content}](${href})`; +} + +function processSpoiler(node: Element): string { + const content = node.children.map(processNode).join(""); + return `||${content}||`; +} + +function processMath(node: Element, mode: "inline" | "block"): string { + const latex = node.attribs["data-mx-maths"] ?? ""; + if (mode === "block") { + return `$$${latex}$$`; + } + return `$${latex}$`; +} + +function processInlineMarkdown(node: Element): string { + const mdSequence = node.attribs["data-md"] ?? ""; + const content = node.children.map(processNode).join(""); + return `${mdSequence}${content}${mdSequence}`; +} diff --git a/src/app/plugins/markdown/injectDataMd.test.ts b/src/app/plugins/markdown/injectDataMd.test.ts new file mode 100644 index 000000000..4fc8a9c5d --- /dev/null +++ b/src/app/plugins/markdown/injectDataMd.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it } from "vitest"; +import { injectDataMd } from "./injectDataMd"; + +describe("injectDataMd", () => { + it("injects data-md into headings", () => { + const result = injectDataMd("

        Hello

        "); + expect(result).toContain('data-md="#'); + }); + + it("does not inject data-md if already present", () => { + const result = injectDataMd('

        Hello

        '); + expect(result).toContain('data-md="#'); + // Should not have duplicate data-md + expect(result.match(/data-md="#/g) ?? []).toHaveLength(1); + }); + + it("injects data-md into blockquotes", () => { + const result = injectDataMd("
        Quote
        "); + expect(result).toContain('data-md=">"'); + }); + + it("injects data-md into code blocks", () => { + const result = injectDataMd("
        code
        "); + expect(result).toContain('data-md="```"'); + }); + + it("injects data-md into horizontal rules", () => { + const result = injectDataMd("
        "); + expect(result).toContain('data-md="---"'); + }); + + it("injects data-md into subscript", () => { + const result = injectDataMd("text"); + expect(result).toContain('data-md="-#"'); + }); + + it("injects data-md into unordered lists", () => { + const result = injectDataMd("
        • Item
        "); + expect(result).toContain('data-md="-"'); + }); + + it("injects data-md into ordered lists", () => { + const result = injectDataMd("
        1. Item
        "); + expect(result).toContain('data-md="1."'); + }); + + it("injects data-md into strong tags", () => { + const result = injectDataMd("bold"); + expect(result).toContain('data-md="**"'); + }); + + it("injects data-md into em tags", () => { + const result = injectDataMd("italic"); + expect(result).toContain('data-md="*"'); + }); + + it("injects data-md into u tags", () => { + const result = injectDataMd("underline"); + expect(result).toContain('data-md="_"'); + }); + + it("injects data-md into s tags", () => { + const result = injectDataMd("strike"); + expect(result).toContain('data-md="~~"'); + }); + + it("injects data-md into del tags", () => { + const result = injectDataMd("deleted"); + expect(result).toContain('data-md="~~"'); + }); + + it("injects data-md into code tags", () => { + const result = injectDataMd("inline"); + expect(result).toContain('data-md="`"'); + }); + + it("handles multiline code blocks without injecting data-md", () => { + const result = injectDataMd("line1\nline2"); + expect(result).not.toContain("data-md"); + }); +}); diff --git a/src/app/plugins/markdown/injectDataMd.ts b/src/app/plugins/markdown/injectDataMd.ts new file mode 100644 index 000000000..8ab78538d --- /dev/null +++ b/src/app/plugins/markdown/injectDataMd.ts @@ -0,0 +1,108 @@ +/** + * Injects data-md attributes into HTML to preserve markdown syntax for round-trip editing. + * This is used when receiving HTML from a sender that may not have preserved data-md. + * + * The function identifies common HTML patterns and adds the appropriate data-md attribute + * so that when converting back to markdown, the original syntax is preserved. + * + * @param html - Input HTML string + * @returns HTML string with data-md attributes injected + */ +export function injectDataMd(html: string): string { + // Inject heading data-md (e.g.,

        ) + html = html.replace(/]*)>/g, (_, level, attrs) => { + if (attrs.includes("data-md")) return ``; + const hashes = "#".repeat(parseInt(level, 10)); + return ``; + }); + + // Inject blockquote data-md + html = html.replace(/]*)>/g, (_, attrs) => { + if (attrs.includes("data-md")) return ``; + return `
        `; + }); + + // Inject code block data-md + html = html.replace( + /]*)>]*)>/g, + (_, preAttrs, codeAttrs) => { + if (preAttrs.includes("data-md")) + return ``; + return `
        `;
        +    },
        +  );
        +
        +  // Inject horizontal rule data-md
        +  html = html.replace(/]*)>/g, (_, attrs) => {
        +    if (attrs.includes("data-md")) return ``;
        +    return `
        `; + }); + + // Inject subscript data-md + html = html.replace(/]*)>/g, (_, attrs) => { + if (attrs.includes("data-md")) return ``; + return ``; + }); + + // Inject ordered list data-md + html = html.replace(/]*)>/g, (_, attrs) => { + if (attrs.includes("data-md")) return ``; + return `
          `; + }); + + // Inject unordered list data-md + html = html.replace(/]*)>/g, (_, attrs) => { + if (attrs.includes("data-md")) return ``; + return `
            `; + }); + + // Inject inline markdown markers for strong/bold + html = html.replace( + /]*)>([^<]*)<\/strong>/g, + (_, attrs, content) => { + if (attrs.includes("data-md")) + return `${content}`; + return `${content}`; + }, + ); + html = html.replace(/]*)>([^<]*)<\/b>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + + // Inject inline markdown markers for emphasis/italic + html = html.replace(/]*)>([^<]*)<\/em>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + html = html.replace(/]*)>([^<]*)<\/i>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + + // Inject inline markdown markers for underline + html = html.replace(/]*)>([^<]*)<\/u>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + + // Inject inline markdown markers for strikethrough + html = html.replace(/]*)>([^<]*)<\/s>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + html = html.replace(/]*)>([^<]*)<\/del>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + return `${content}`; + }); + + // Inject inline code marker + html = html.replace(/]*)>([^<]*)<\/code>/g, (_, attrs, content) => { + if (attrs.includes("data-md")) return `${content}`; + // Don't add data-md for inline code inside pre (code blocks handled separately) + if (content.includes("\n")) return `${content}`; + return `${content}`; + }); + + return html; +}