From d3f1a398d805fdeb7974de44d856c1a0a5cc95de Mon Sep 17 00:00:00 2001 From: hanishkvc Date: Tue, 25 Nov 2025 20:05:40 +0530 Subject: [PATCH] SimpleChatTCRV:Markdown:Initial skeleton Try identify headings, and blocks in markdown and convert them into equivalent stuff in html Show the same in the chat message blocks. --- tools/server/public_simplechat/simplechat.js | 11 +++- tools/server/public_simplechat/typemd.mjs | 58 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tools/server/public_simplechat/typemd.mjs diff --git a/tools/server/public_simplechat/simplechat.js b/tools/server/public_simplechat/simplechat.js index 29924ad639..72008b936a 100644 --- a/tools/server/public_simplechat/simplechat.js +++ b/tools/server/public_simplechat/simplechat.js @@ -10,6 +10,7 @@ import * as du from "./datautils.mjs"; import * as ui from "./ui.mjs" import * as mTools from "./tools.mjs" import * as mIdb from "./idb.mjs" +import * as mMD from "./typemd.mjs" const TEMP_MARKER = "-TEMP" @@ -1496,7 +1497,15 @@ class MultiChatUI { } for (const [name, content] of showList) { if (content.length > 0) { - entry = ui.el_create_append_p(`${content}`, secContents); + if (name == "content") { + entry = document.createElement('div') + let md = new mMD.MarkDown() + md.process(content) + entry.innerHTML = md.html + secContents.appendChild(entry) + } else { + entry = ui.el_create_append_p(`${content}`, secContents); + } entry.classList.add(`chat-message-${name}`) } } diff --git a/tools/server/public_simplechat/typemd.mjs b/tools/server/public_simplechat/typemd.mjs new file mode 100644 index 0000000000..a1d3eab57e --- /dev/null +++ b/tools/server/public_simplechat/typemd.mjs @@ -0,0 +1,58 @@ +//@ts-check +// Helpers to handle markdown content +// by Humans for All +// + + +export class MarkDown { + + constructor() { + this.in = { + pre: false, + table: false, + } + this.md = "" + this.html = "" + } + + /** + * Process a line from markdown content + * @param {string} line + */ + process_line(line) { + let lineA = line.split(' ') + let lineRStripped = line.trimStart() + if (this.in.pre) { + if (lineA[0] == '```') { + this.in.pre = false + this.html += "\n" + } else { + this.html += line + } + return + } + if (line.startsWith ("#")) { + let hLevel = lineA[0].length + this.html += `${lineRStripped}\n` + return + } + if (lineA[0] == '```') { + this.in.pre = true + this.html += "
\n"
+            return
+        }
+        this.html += `

${line}

` + } + + /** + * Process a bunch of lines in markdown format. + * @param {string} lines + */ + process(lines) { + let linesA = lines.split('\n') + for(const line of linesA) { + this.process_line(line) + } + } + +}