mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
html to markdown
This commit is contained in:
5 files changed
+624
No files matched your search
@@ -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("<li>");
|
||||
expect(result).not.toContain("<ol>");
|
||||
expect(result).not.toContain("<ul>");
|
||||
expect(result).toContain("k.");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { htmlToMarkdown } from "./htmlToMarkdown";
|
||||
|
||||
describe("htmlToMarkdown", () => {
|
||||
it("converts headings", () => {
|
||||
expect(htmlToMarkdown("<h1>Hello</h1>")).toContain("# Hello");
|
||||
expect(htmlToMarkdown("<h2>World</h2>")).toContain("## World");
|
||||
});
|
||||
|
||||
it("converts bold text", () => {
|
||||
expect(htmlToMarkdown("<strong>bold</strong>")).toContain("**bold**");
|
||||
expect(htmlToMarkdown("<b>bold</b>")).toContain("**bold**");
|
||||
});
|
||||
|
||||
it("converts italic text", () => {
|
||||
expect(htmlToMarkdown("<em>italic</em>")).toContain("*italic*");
|
||||
expect(htmlToMarkdown("<i>italic</i>")).toContain("*italic*");
|
||||
});
|
||||
|
||||
it("converts strikethrough", () => {
|
||||
expect(htmlToMarkdown("<s>deleted</s>")).toContain("~~deleted~~");
|
||||
expect(htmlToMarkdown("<del>deleted</del>")).toContain("~~deleted~~");
|
||||
});
|
||||
|
||||
it("converts inline code", () => {
|
||||
expect(htmlToMarkdown("<code>code</code>")).toContain("`code`");
|
||||
});
|
||||
|
||||
it("converts code blocks", () => {
|
||||
expect(
|
||||
htmlToMarkdown(
|
||||
'<pre><code class="language-rust">fn main() {}</code></pre>',
|
||||
),
|
||||
).toContain("```rust");
|
||||
});
|
||||
|
||||
it("converts links", () => {
|
||||
expect(htmlToMarkdown('<a href="https://example.com">link</a>')).toContain(
|
||||
"[link](https://example.com)",
|
||||
);
|
||||
});
|
||||
|
||||
it("converts spoiler spans", () => {
|
||||
expect(htmlToMarkdown("<span data-mx-spoiler>hidden</span>")).toContain(
|
||||
"||hidden||",
|
||||
);
|
||||
});
|
||||
|
||||
it("converts inline math spans", () => {
|
||||
expect(
|
||||
htmlToMarkdown('<span data-mx-maths="E = mc^2">E = mc^2</span>'),
|
||||
).toContain("$E = mc^2$");
|
||||
});
|
||||
|
||||
it("converts block math divs", () => {
|
||||
expect(
|
||||
htmlToMarkdown('<div data-mx-maths="\\frac{a}{b}">frac</div>'),
|
||||
).toContain("$$\\frac{a}{b}$$");
|
||||
});
|
||||
|
||||
it("converts blockquotes", () => {
|
||||
const result = htmlToMarkdown("<blockquote>Quote text</blockquote>");
|
||||
expect(result).toContain(">");
|
||||
expect(result).toContain("Quote text");
|
||||
});
|
||||
|
||||
it("converts unordered lists", () => {
|
||||
const result = htmlToMarkdown("<ul><li>Item 1</li><li>Item 2</li></ul>");
|
||||
expect(result).toContain("-");
|
||||
expect(result).toContain("Item 1");
|
||||
});
|
||||
|
||||
it("converts ordered lists", () => {
|
||||
const result = htmlToMarkdown("<ol><li>Item 1</li><li>Item 2</li></ol>");
|
||||
expect(result).toContain("1.");
|
||||
expect(result).toContain("Item 1");
|
||||
});
|
||||
|
||||
it("preserves data-md attributes for round-trip", () => {
|
||||
const result = htmlToMarkdown('<strong data-md="**">bold</strong>');
|
||||
expect(result).toContain("**bold**");
|
||||
});
|
||||
|
||||
it("escapes markdown special characters in text", () => {
|
||||
const result = htmlToMarkdown("<p>Hello *world*</p>");
|
||||
expect(result).toContain("\\*");
|
||||
});
|
||||
});
|
||||
@@ -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}`;
|
||||
}
|
||||
@@ -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("<h1>Hello</h1>");
|
||||
expect(result).toContain('data-md="#');
|
||||
});
|
||||
|
||||
it("does not inject data-md if already present", () => {
|
||||
const result = injectDataMd('<h1 data-md="#">Hello</h1>');
|
||||
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("<blockquote>Quote</blockquote>");
|
||||
expect(result).toContain('data-md=">"');
|
||||
});
|
||||
|
||||
it("injects data-md into code blocks", () => {
|
||||
const result = injectDataMd("<pre><code>code</code></pre>");
|
||||
expect(result).toContain('data-md="```"');
|
||||
});
|
||||
|
||||
it("injects data-md into horizontal rules", () => {
|
||||
const result = injectDataMd("<hr/>");
|
||||
expect(result).toContain('data-md="---"');
|
||||
});
|
||||
|
||||
it("injects data-md into subscript", () => {
|
||||
const result = injectDataMd("<sub>text</sub>");
|
||||
expect(result).toContain('data-md="-#"');
|
||||
});
|
||||
|
||||
it("injects data-md into unordered lists", () => {
|
||||
const result = injectDataMd("<ul><li>Item</li></ul>");
|
||||
expect(result).toContain('data-md="-"');
|
||||
});
|
||||
|
||||
it("injects data-md into ordered lists", () => {
|
||||
const result = injectDataMd("<ol><li>Item</li></ol>");
|
||||
expect(result).toContain('data-md="1."');
|
||||
});
|
||||
|
||||
it("injects data-md into strong tags", () => {
|
||||
const result = injectDataMd("<strong>bold</strong>");
|
||||
expect(result).toContain('data-md="**"');
|
||||
});
|
||||
|
||||
it("injects data-md into em tags", () => {
|
||||
const result = injectDataMd("<em>italic</em>");
|
||||
expect(result).toContain('data-md="*"');
|
||||
});
|
||||
|
||||
it("injects data-md into u tags", () => {
|
||||
const result = injectDataMd("<u>underline</u>");
|
||||
expect(result).toContain('data-md="_"');
|
||||
});
|
||||
|
||||
it("injects data-md into s tags", () => {
|
||||
const result = injectDataMd("<s>strike</s>");
|
||||
expect(result).toContain('data-md="~~"');
|
||||
});
|
||||
|
||||
it("injects data-md into del tags", () => {
|
||||
const result = injectDataMd("<del>deleted</del>");
|
||||
expect(result).toContain('data-md="~~"');
|
||||
});
|
||||
|
||||
it("injects data-md into code tags", () => {
|
||||
const result = injectDataMd("<code>inline</code>");
|
||||
expect(result).toContain('data-md="`"');
|
||||
});
|
||||
|
||||
it("handles multiline code blocks without injecting data-md", () => {
|
||||
const result = injectDataMd("<code>line1\nline2</code>");
|
||||
expect(result).not.toContain("data-md");
|
||||
});
|
||||
});
|
||||
@@ -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., <h1 data-md="#">)
|
||||
html = html.replace(/<h([1-6])([^>]*)>/g, (_, level, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<h${level}${attrs}>`;
|
||||
const hashes = "#".repeat(parseInt(level, 10));
|
||||
return `<h${level} data-md="${hashes}"${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject blockquote data-md
|
||||
html = html.replace(/<blockquote([^>]*)>/g, (_, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<blockquote${attrs}>`;
|
||||
return `<blockquote data-md=">"${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject code block data-md
|
||||
html = html.replace(
|
||||
/<pre([^>]*)><code([^>]*)>/g,
|
||||
(_, preAttrs, codeAttrs) => {
|
||||
if (preAttrs.includes("data-md"))
|
||||
return `<pre${preAttrs}><code${codeAttrs}>`;
|
||||
return `<pre data-md="\`\`\`"${preAttrs}><code${codeAttrs}>`;
|
||||
},
|
||||
);
|
||||
|
||||
// Inject horizontal rule data-md
|
||||
html = html.replace(/<hr([^>]*)>/g, (_, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<hr${attrs}>`;
|
||||
return `<hr data-md="---"${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject subscript data-md
|
||||
html = html.replace(/<sub([^>]*)>/g, (_, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<sub${attrs}>`;
|
||||
return `<sub data-md="-#"${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject ordered list data-md
|
||||
html = html.replace(/<ol([^>]*)>/g, (_, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<ol${attrs}>`;
|
||||
return `<ol data-md="1."${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject unordered list data-md
|
||||
html = html.replace(/<ul([^>]*)>/g, (_, attrs) => {
|
||||
if (attrs.includes("data-md")) return `<ul${attrs}>`;
|
||||
return `<ul data-md="-"${attrs}>`;
|
||||
});
|
||||
|
||||
// Inject inline markdown markers for strong/bold
|
||||
html = html.replace(
|
||||
/<strong([^>]*)>([^<]*)<\/strong>/g,
|
||||
(_, attrs, content) => {
|
||||
if (attrs.includes("data-md"))
|
||||
return `<strong${attrs}>${content}</strong>`;
|
||||
return `<strong data-md="**"${attrs}>${content}</strong>`;
|
||||
},
|
||||
);
|
||||
html = html.replace(/<b([^>]*)>([^<]*)<\/b>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<b${attrs}>${content}</b>`;
|
||||
return `<b data-md="**"${attrs}>${content}</b>`;
|
||||
});
|
||||
|
||||
// Inject inline markdown markers for emphasis/italic
|
||||
html = html.replace(/<em([^>]*)>([^<]*)<\/em>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<em${attrs}>${content}</em>`;
|
||||
return `<em data-md="*"${attrs}>${content}</em>`;
|
||||
});
|
||||
html = html.replace(/<i([^>]*)>([^<]*)<\/i>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<i${attrs}>${content}</i>`;
|
||||
return `<i data-md="*"${attrs}>${content}</i>`;
|
||||
});
|
||||
|
||||
// Inject inline markdown markers for underline
|
||||
html = html.replace(/<u([^>]*)>([^<]*)<\/u>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<u${attrs}>${content}</u>`;
|
||||
return `<u data-md="_"${attrs}>${content}</u>`;
|
||||
});
|
||||
|
||||
// Inject inline markdown markers for strikethrough
|
||||
html = html.replace(/<s([^>]*)>([^<]*)<\/s>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<s${attrs}>${content}</s>`;
|
||||
return `<s data-md="~~"${attrs}>${content}</s>`;
|
||||
});
|
||||
html = html.replace(/<del([^>]*)>([^<]*)<\/del>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<del${attrs}>${content}</del>`;
|
||||
return `<del data-md="~~"${attrs}>${content}</del>`;
|
||||
});
|
||||
|
||||
// Inject inline code marker
|
||||
html = html.replace(/<code([^>]*)>([^<]*)<\/code>/g, (_, attrs, content) => {
|
||||
if (attrs.includes("data-md")) return `<code${attrs}>${content}</code>`;
|
||||
// Don't add data-md for inline code inside pre (code blocks handled separately)
|
||||
if (content.includes("\n")) return `<code${attrs}>${content}</code>`;
|
||||
return `<code data-md="\`"${attrs}>${content}</code>`;
|
||||
});
|
||||
|
||||
return html;
|
||||
}
|
||||
Reference in new issue
Block a user