From 1d26453b52fddbebee12b05c74170c121c0f6a79 Mon Sep 17 00:00:00 2001 From: hanishkvc Date: Wed, 26 Nov 2025 19:02:23 +0530 Subject: [PATCH] SimpleChatTCRV:Markdown: CommonLogic listitem & para extend/append Similar to listitem before, now also allow a para to have its long lines split into adjacent lines. Inturn the logic will take care of merging them into single para. The common logic wrt both flows moved into its own helper function. --- tools/server/public_simplechat/typemd.mjs | 55 ++++++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/tools/server/public_simplechat/typemd.mjs b/tools/server/public_simplechat/typemd.mjs index 125f68343b..d4c41382b6 100644 --- a/tools/server/public_simplechat/typemd.mjs +++ b/tools/server/public_simplechat/typemd.mjs @@ -40,9 +40,14 @@ export class MarkDown { this.html = "" } + /** @typedef {{prev: number, cur: number}} EmptyTrackerResult */ + /** + * Track how many adjacent empty lines have been seen till now, in the immidate past. + * as well as whether the current line is empty or otherwise. * @param {string} key * @param {string} line + * @returns {EmptyTrackerResult} */ empty_tracker(key, line) { if (this.in.empty[key] == undefined) { @@ -57,6 +62,46 @@ export class MarkDown { return {prev: prev, cur: this.in.empty[key]} } + /** + * Append a new block to the end of html. + * @param {string} line + * @param {string} startMarker + * @param {string} endMarker + */ + appendnew(line, startMarker, endMarker) { + this.html += `${startMarker}${line}${endMarker}` + } + + /** + * Extend to the existing last block + * @param {string} line + * @param {string} endMarker + */ + extend(line, endMarker) { + let html = this.html + this.html = `${html.slice(0,html.length-endMarker.length)} ${line}${endMarker}` + } + + /** + * Extend the existing block, if + * * there was no immidiate empty lines AND + * * the existing block corresponds to what is specified. + * Else + * * append a new block + * + * @param {string} line + * @param {string} endMarker + * @param {string} startMarker + * @param {EmptyTrackerResult} emptyTracker + */ + extend_else_appendnew(line, endMarker, startMarker, emptyTracker) { + if ((emptyTracker.prev != 0) || (!this.html.endsWith(endMarker))) { + this.appendnew(line, startMarker, endMarker) + } else { + this.extend(line, endMarker) + } + } + unwind_list() { while (true) { let popped = this.in.list.endType.pop() @@ -126,12 +171,7 @@ export class MarkDown { if (matchOffset[1].length < lastOffset) { return false } - if ((emptyTracker.prev != 0) || (!this.html.endsWith("\n"))) { - this.html += `
  • ${matchOffset[2]}
  • \n` - } else { - let html = this.html - this.html = `${html.slice(0,html.length-"\n".length)} ${matchOffset[2]}\n` - } + this.extend_else_appendnew(matchOffset[2], "\n", '
  • ', emptyTracker) return true } } @@ -242,7 +282,8 @@ export class MarkDown { return } this.unwind_list() - this.html += `

    ${line}

    ` + let emptyTrackerPara = this.empty_tracker("para", line) + this.extend_else_appendnew(line, "

    \n", "

    ", emptyTrackerPara) } /**