From c84c300f44d43d4aea0bbbbd219b8bf98f92583d Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Sat, 23 Sep 2023 09:02:02 +0200 Subject: [PATCH] added: md syntax highlight --- index.html | 1 + main.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++------ style.css | 20 ++++++++++++++ 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 59d9963..d485775 100644 --- a/index.html +++ b/index.html @@ -49,6 +49,7 @@
+
diff --git a/main.js b/main.js index d736b6c..32fc338 100644 --- a/main.js +++ b/main.js @@ -14,7 +14,8 @@ var defaultsettings = titleinaccentcolor: false, enablenetwork: false, titlebydefault: false, - linksinnewtab: true + linksinnewtab: true, + colors: true }; //builtin @@ -1246,6 +1247,11 @@ function loadsettings() { toggletitle(); } + + colored.hidden = !settings.colors; + md.style.color = settings.colors ? "transparent" : "inherit"; + md.style.background = settings.colors ? "transparent" : "inherit"; + applycolors(); } function checksaved() @@ -1617,11 +1623,6 @@ function selectlines() md.selectionEnd = range.end; } -function seteditorcontent(content) -{ - md.value = content; -} - function ontopbarclick() { if (title.hidden) @@ -2025,8 +2026,66 @@ function save() } } +function applycolors() +{ + if (!settings.colors) + { + return; + } + + var lines = md.value.split("\n"); + header = false; + code = false; + var result = []; + lines.every( (line, i) => + { + if (line.startsWith("#")) + { + line = line.replace(/(#*)/g, "$1"); + line = "" + line + ""; + } + if (line.startsWith("* ")) + { + line = line.replace(/(\* )/g, "$1"); + } + + if (i == 0 && line == "---") + { + header = true; + } + if (header) + { + if (i > 0 && line == "---") + { + header = false; + } + line = "" + line + ""; + } + + if (line == "```" && !code) + { + code = true; + line = "
" + line; + } + if (line == "```" && code) + { + code = false; + line = line + "
"; + } + + line = line.replace(/(\[\[.*\]\])/g, "$1"); + line = line.replace(/(\*\*.*\*\*)/g, "$1"); + line = line.replace(/(\*.*\*)/g, "$1"); + result.push(line); + + return true; + }); + colored.innerHTML = result.join("
"); +} + function datachanged() { + applycolors(); resize(); saved = false; @@ -2193,7 +2252,7 @@ function restore() { if (confirm('restore "' + currentnote.title + '"?')) { - seteditorcontent(backup); + md.value = backup; datachanged(); } } @@ -2527,7 +2586,7 @@ function bindfile(note) title.value = note.title; setwindowtitle(); - seteditorcontent(note.content || ""); + md.value = note.content || ""; preview.innerHTML = md2html(md.value); if (changed) @@ -2543,6 +2602,7 @@ function bindfile(note) } setpos(note.pos || 0); + applycolors(); } function defaultheaders(title, tags = "") diff --git a/style.css b/style.css index 2720e1c..7e273bc 100644 --- a/style.css +++ b/style.css @@ -55,6 +55,26 @@ body::-webkit-scrollbar-thumb { font-size: inherit; line-height: inherit; background-color: inherit; + + /* coloring */ + position: absolute; + top: 0; + left: 0; + padding: 0; +} + +#notecontent { + position: relative; +} + +#colored { + position: absolute; + white-space: pre; + top: 0; + left: 0; + z-index: -1; + width: 100%; + text-wrap: inherit; } #network {