From cbac831768ee53ad73cad73769c2819871a8d8c3 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Thu, 19 Oct 2023 12:17:01 +0200 Subject: [PATCH] ongoing refactor to update colors by line --- main.js | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index d65ff76..cec143d 100644 --- a/main.js +++ b/main.js @@ -1973,17 +1973,46 @@ function rawline(index) return md.value.split("\n")[index]; } -function applycolorsonline(line, index, options) +function applycolorsonline(line, linediv, index, options) { line = escapeHtml(line); + while (linediv.hasChildNodes()) + { + linediv.removeChild(linediv.lastChild); + } + // headings if (line.startsWith("#")) { - line = line.replace(/(#* )/, "$1"); // to check! - line = "" + line + ""; + var level = line.indexOf(" "); + if (level == -1) + { + level = line.length; + } + + var span = document.createElement("span"); + span.setAttribute("style", `font-weight: bold;color:${settings.accentcolor};`); + span.appendChild(document.createTextNode("#".repeat(level))); + linediv.appendChild(span); + + if (level < line.length) + { + var headingtext = line.substring(level); + span = document.createElement("span"); + span.setAttribute("style", "font-weight: bold;"); + span.appendChild(document.createTextNode(headingtext)); + linediv.appendChild(span); + } + return; } + // fallback + //linediv.appendChild(document.createTextNode(line || " ")); + //return; + + /* -----TODO------ */ + // bold and italics var temp = line; if (line.startsWith("* ")) @@ -2107,7 +2136,8 @@ function applycolorsonline(line, index, options) } } } - return line; + + linediv.innerHTML = line || " "; } function applycolors(currentonly) @@ -2130,9 +2160,7 @@ function applycolors(currentonly) var index = currentline(); var linediv = document.getElementById("line" + index); options = JSON.parse(linediv.getAttribute("tag")); - var line = rawline(index); - line = applycolorsonline(line, index, options); - linediv.innerHTML = line || " "; + applycolorsonline(rawline(index), linediv, index, options); } else { @@ -2149,7 +2177,7 @@ function applycolors(currentonly) } linediv.setAttribute("id", "line" + i); linediv.setAttribute("tag", JSON.stringify(options)); - linediv.innerHTML = applycolorsonline(lines[i], i, options) || " "; + applycolorsonline(lines[i], linediv, i, options); }; // remove remanining