diff --git a/tools/server/public_simplechat/simplechat.js b/tools/server/public_simplechat/simplechat.js index 5a16d62d1f..31f78b201e 100644 --- a/tools/server/public_simplechat/simplechat.js +++ b/tools/server/public_simplechat/simplechat.js @@ -1499,7 +1499,7 @@ class MultiChatUI { if (content.length > 0) { if ((name == "content") && (this.simpleChats[chatId].cfg.chatProps.bMarkdown)) { entry = document.createElement('div') - let md = new mMD.MarkDown() + let md = new mMD.MarkDown(this.simpleChats[chatId].cfg.chatProps.bMarkdownHtmlSanitize) md.process(content) entry.innerHTML = md.html secContents.appendChild(entry) @@ -2106,6 +2106,7 @@ export class Config { */ iRecentUserMsgCnt: 5, bMarkdown: true, + bMarkdownHtmlSanitize: false, bCompletionFreshChatAlways: true, bCompletionInsertStandardRolePrefix: false, bTrimGarbage: true, diff --git a/tools/server/public_simplechat/typemd.mjs b/tools/server/public_simplechat/typemd.mjs index d4c41382b6..4ba6f029d9 100644 --- a/tools/server/public_simplechat/typemd.mjs +++ b/tools/server/public_simplechat/typemd.mjs @@ -15,7 +15,12 @@ */ export class MarkDown { - constructor() { + /** + * Markdown parse and convert to html. + * @param {boolean} bHtmlSanitize + */ + constructor(bHtmlSanitize) { + this.bHtmlSanitize = bHtmlSanitize this.in = { preFenced: "", table: { @@ -30,7 +35,9 @@ export class MarkDown { }, /** @type {Object} */ empty: { - } + }, + /** @type {string} */ + blockQuote: "", } /** * @type {Array<*>} @@ -235,14 +242,50 @@ export class MarkDown { } } + unwind_blockquote() { + for(let i=0; i\n` + } + this.in.blockQuote = "" + } + + /** + * Handle blockquote block one line at a time. + * This expects all lines in the block quote to have the marker at the begining. + * + * @param {string} line + * @param {string} startTok + */ + process_blockquote(line, startTok) { + if (!line.startsWith(">")) { + this.unwind_blockquote() + return false + } + if (startTok.match(/^>+$/) == null) { + this.unwind_blockquote() + return false + } + this.unwind_list() + if (startTok.length > this.in.blockQuote.length) { + this.html += `
\n` + } else if (startTok.length < this.in.blockQuote.length) { + this.html += `
\n` + } + this.in.blockQuote = startTok + this.html += `

${line}

` + return true + } + /** * Process a line from markdown content * @param {string} line */ process_line(line) { - let elSanitize = document.createElement('div') - elSanitize.textContent = line - line = elSanitize.innerHTML + if (this.bHtmlSanitize) { + let elSanitize = document.createElement('div') + elSanitize.textContent = line + line = elSanitize.innerHTML + } let lineA = line.split(' ') if (this.in.preFenced.length > 0) { if (line == this.in.preFenced) { @@ -278,6 +321,9 @@ export class MarkDown { this.html += `
\n`
             return
         }
+        if (this.process_blockquote(line, lineA[0])) {
+            return
+        }
         if (this.process_list(line)) {
             return
         }