SimpleChatTCRV:MarkDown: Better Fenced Pre

Allow fenced code block / pre to be demarkated using either ```
or ~~~

Ensure the termination line wrt fenced block doesnt contain anything
else.

Same starting marker needs to be present wrt ending also
This commit is contained in:
hanishkvc 2025-11-25 23:24:37 +05:30
parent 924bb6cb47
commit 95c8cd6eba
1 changed files with 8 additions and 8 deletions

View File

@ -8,7 +8,7 @@ export class MarkDown {
constructor() { constructor() {
this.in = { this.in = {
pre: false, preFenced: "",
table: false, table: false,
/** @type {Array<number>} */ /** @type {Array<number>} */
listUnordered: [] listUnordered: []
@ -34,9 +34,9 @@ export class MarkDown {
*/ */
process_line(line) { process_line(line) {
let lineA = line.split(' ') let lineA = line.split(' ')
if (this.in.pre) { if (this.in.preFenced.length > 0) {
if (lineA[0] == '```') { if (line == this.in.preFenced) {
this.in.pre = false this.in.preFenced = ""
this.html += "</pre>\n" this.html += "</pre>\n"
} else { } else {
this.html += `${line}\n` this.html += `${line}\n`
@ -54,11 +54,11 @@ export class MarkDown {
this.html += `<h${hLevel}>${line.slice(hLevel)}</h${hLevel}>\n` this.html += `<h${hLevel}>${line.slice(hLevel)}</h${hLevel}>\n`
return return
} }
let matchPre = line.match(/^```([a-zA-Z0-9]*)(.*)/); let matchPreFenced = line.match(/^(```|~~~)([a-zA-Z0-9]*)(.*)/);
if ( matchPre != null) { if ( matchPreFenced != null) {
this.unwind_list() this.unwind_list()
this.in.pre = true this.in.preFenced = matchPreFenced[1]
this.html += `<pre class="${matchPre[1]}">\n` this.html += `<pre class="${matchPreFenced[2]}">\n`
return return
} }
let matchUnOrdered = line.match(/^([ ]*)[-+*][ ](.*)$/); let matchUnOrdered = line.match(/^([ ]*)[-+*][ ](.*)$/);