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.
This commit is contained in:
hanishkvc 2025-11-25 20:05:40 +05:30
parent c5eb783ec1
commit d3f1a398d8
2 changed files with 68 additions and 1 deletions

View File

@ -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}`)
}
}

View File

@ -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 += "</pre>\n"
} else {
this.html += line
}
return
}
if (line.startsWith ("#")) {
let hLevel = lineA[0].length
this.html += `<h${hLevel}>${lineRStripped}</h${hLevel}>\n`
return
}
if (lineA[0] == '```') {
this.in.pre = true
this.html += "<pre>\n"
return
}
this.html += `<p>${line}</p>`
}
/**
* 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)
}
}
}