on-codemerge
Version:
A WYSIWYG editor for on-codemerge is a user-friendly interface that allows users to edit and view their code in real time, exactly as it will appear in the final product
124 lines (112 loc) • 2.85 kB
JavaScript
/*! on-codemerge v1.3.1 @author Pavel Kuzmin @license MIT @homepage https://s00d.github.io/on-codemerge/ @repository git+https://github.com/s00d/on-codemerge.git Copyright (c) 2026 Pavel Kuzmin - Built on 2026-07-02T13:39:17.947Z */
import { createContainer as m } from "../../../utils/helpers.mjs";
function f(o) {
const e = m();
return e.innerHTML = o, s(e);
}
function s(o) {
var u, a;
if (o.nodeType === Node.TEXT_NODE)
return o.textContent || "";
if (o.nodeType !== Node.ELEMENT_NODE)
return "";
const e = o, r = Array.from(e.childNodes).map((n) => s(n)).join("");
switch (e.tagName.toLowerCase()) {
case "h1":
return `# ${r}
`;
case "h2":
return `## ${r}
`;
case "h3":
return `### ${r}
`;
case "h4":
return `#### ${r}
`;
case "h5":
return `##### ${r}
`;
case "h6":
return `###### ${r}
`;
case "p":
return `${r}
`;
case "strong":
case "b":
return `**${r}**`;
case "em":
case "i":
return `*${r}*`;
case "a":
const n = e.getAttribute("href");
return `[${r}](${n})`;
case "img":
const c = e.getAttribute("src");
return ``;
case "ul":
return e.childNodes.length ? Array.from(e.childNodes).map((t) => s(t)).join("") + `
` : "";
case "ol":
let i = 1;
return e.childNodes.length ? Array.from(e.childNodes).map((t) => t.nodeType === Node.ELEMENT_NODE && t.nodeName.toLowerCase() === "li" ? `${i++}. ${s(t)}
` : s(t)).join("") + `
` : "";
case "li":
return `- ${r}
`;
case "pre":
if (e.querySelector("code")) {
const t = e.querySelector("code");
return `\`\`\`${((u = t == null ? void 0 : t.className) == null ? void 0 : u.replace("language-", "")) || ""}
${(t == null ? void 0 : t.textContent) || ""}
\`\`\`
`;
}
return `\`\`\`
${r}
\`\`\`
`;
case "code":
return ((a = e.parentElement) == null ? void 0 : a.tagName.toLowerCase()) === "pre" ? r : `\`${r}\``;
case "blockquote":
return `> ${r}
`;
case "hr":
return `---
`;
case "br":
return `
`;
case "table":
return d(e);
default:
return r;
}
}
function d(o) {
const e = Array.from(o.rows);
if (!e.length) return "";
const r = Math.max(...e.map((n) => n.cells.length));
if (!r) return "";
const u = e[0];
let a = "| " + Array.from(u.cells).map((n) => {
var c;
return ((c = n.textContent) == null ? void 0 : c.trim()) || "";
}).join(" | ") + ` |
`;
a += "|" + " --- |".repeat(r) + `
`;
for (let n = 1; n < e.length; n++)
a += "| " + Array.from(e[n].cells).map((c) => {
var l;
return ((l = c.textContent) == null ? void 0 : l.trim()) || "";
}).join(" | ") + ` |
`;
return a + `
`;
}
export {
f as htmlToMarkdown
};