From 143fc2871b05a105123b22ef8185deac345eb44d Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Sat, 28 Jun 2025 19:32:02 +0200 Subject: parse markdown --- index.html | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 368cce8..484353c 100644 --- a/index.html +++ b/index.html @@ -19,8 +19,7 @@

Ask below :-)

- +
@@ -59,7 +58,7 @@ }) }) .then(response => response.json()) - .then(data => appendChat(data.response)) + .then(data => appendChat(markdownParser(data.response))) .catch(error => console.error(error)); }); function appendChat(message) { @@ -67,6 +66,95 @@ messageDisplay.textContent = message; chat.appendChild(messageDisplay); } + + // simple markdown parser + function escapeHtml(html) { + var line = html.split('<').join('<'); + line = line.split('>').join('>'); + return line; + } + function markdownParser(text) { + // Regular expressions for different markdown elements + const headingRegex = /^#+ (.+)$/; + const boldRegex = /\*\*(.+?)\*\*/; + const italicRegex = /\_\_(.+?)\_\_/; + const listRegex = /^\* (.+)$/; + const blockPreRegex = /```.*/; + const inlinePreRegex = /`(.+?)`/; + + // Split the text into lines + const lines = text.split('\n'); + + // Initialize the parsed output + const output = []; + + var preMode = false; + var listMode = false; + for (var line of lines) { + // Check for pre-formatted block mode + const preMatch = line.match(blockPreRegex); + if (preMatch) { + if (preMode) + output.push(''); + else + output.push('
');
+          preMode = !preMode;
+          continue;
+        }
+        if (preMode) {
+          output.push(escapeHtml(line));
+          continue;
+        }
+
+        // Check for inline pre-formatted mode
+        var inlinePreMatch = line.match(inlinePreRegex);
+        while (inlinePreMatch) {
+          line = line.replace(inlinePreMatch[0], `${escapeHtml(inlinePreMatch[1])}`);
+          inlinePreMatch = line.match(inlinePreRegex);
+        }
+
+        // handle bold text
+        var boldMatch = line.match(boldRegex);
+        while (boldMatch) {
+          line = line.replace(boldMatch[0], `${boldMatch[1]}`);
+          boldMatch = line.match(boldRegex);
+        }
+
+        // handle italic text
+        var italicMatch = line.match(italicRegex);
+        while (italicMatch) {
+          line = line.replace(italicMatch[0], `${italicMatch[1]}`);
+          italicMatch = line.match(italicRegex);
+        }
+
+        // List
+        const listMatch = line.match(listRegex);
+        if (listMatch) {
+          if (!listMode)
+            output.push('');
+        }
+
+        // Check for headings
+        const headingMatch = line.match(headingRegex);
+        if (headingMatch) {
+          output.push(`

${headingMatch[1]}

`); + continue; + } + + // Check for paragraphs + output.push(`

${line}

`); + } + + // Join the parsed elements into HTML + return output.join('\n'); + } -- cgit v1.2.3