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
43 lines (42 loc) • 1.5 kB
JavaScript
var o = Object.defineProperty;
var c = (i, e, t) => e in i ? o(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var r = (i, e, t) => c(i, typeof e != "symbol" ? e + "" : e, t);
class g {
constructor(e = {}) {
r(this, "defaultOptions", {
indentSize: 2,
maxLineLength: 80
});
this.options = e, this.options = { ...this.defaultOptions, ...e };
}
format(e) {
let t = "", a = 0;
const { indentSize: n } = { ...this.defaultOptions, ...this.options };
return e = e.replace(/<[^>]*class="[^"]*\bresize-handle\b[^"]*"[^>]*>/g, ""), e = e.replace(/\s*draggable="true"\s*/g, " "), e = e.replace(/\s*contenteditable="true"\s*/g, " "), e = e.replace(/\s*class="selected"\s*/g, " "), e = e.replace(/\bdragging\b/g, ""), e = e.replace(/(<(th|td)[^>]*)\s*style="position:\s*relative;"\s*/g, "$1"), e.split(/(<[^>]+>)/g).filter(Boolean).forEach((s) => {
s.trim() && (s.match(/^<\//) && (a = Math.max(0, a - 1)), s.startsWith("<") ? t += `
` + " ".repeat(a * n) + s : t += s, s.match(/^<[^/][^>]*[^/]>$/) && !this.isVoidElement(s) && (a = Math.max(0, a + 1)));
}), t.trim();
}
isVoidElement(e) {
const t = e.match(/<([a-zA-Z0-9-]+)/);
return t ? [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr"
].includes(t[1].toLowerCase()) : !1;
}
}
export {
g as HTMLFormatter
};