From 12076f91464cf2122967163598c4935ff4e2cff6 Mon Sep 17 00:00:00 2001
From: Saifeddine ALOUI
Date: Thu, 5 Sep 2024 03:18:39 +0200
Subject: [PATCH] Fixed paragraphs problems
---
.../libraries/lollms_markdown_renderer.js | 70 +++++++++++++++++--
1 file changed, 65 insertions(+), 5 deletions(-)
diff --git a/endpoints/libraries/lollms_markdown_renderer.js b/endpoints/libraries/lollms_markdown_renderer.js
index b7731759..768608ab 100644
--- a/endpoints/libraries/lollms_markdown_renderer.js
+++ b/endpoints/libraries/lollms_markdown_renderer.js
@@ -324,11 +324,71 @@ class MarkdownRenderer {
}
handleParagraphs(text) {
- //return text.replace(/^(?!<[uo]l|$1
');
- // No need to handle paragraphs separately, they will be handled as the remaining content
- return text;
- }
-
+ // Split the text into lines
+ let lines = text.split('\n');
+ let inList = false;
+ let inCodeBlock = false;
+ let result = [];
+
+ for (let i = 0; i < lines.length; i++) {
+ let line = lines[i].trim();
+
+ // Check for code blocks
+ if (line.startsWith('```')) {
+ inCodeBlock = !inCodeBlock;
+ result.push(line);
+ continue;
+ }
+
+ // If we're in a code block, don't process the line
+ if (inCodeBlock) {
+ result.push(line);
+ continue;
+ }
+
+ // Check for list items
+ if (line.match(/^[-*+]\s/) || line.match(/^\d+\.\s/)) {
+ if (!inList) {
+ result.push(inList ? '' : '');
+ inList = true;
+ }
+ result.push('- ' + line.replace(/^[-*+]\s/, '').replace(/^\d+\.\s/, '') + '
');
+ }
+ // Check for headers
+ else if (line.startsWith('#')) {
+ let level = line.match(/^#+/)[0].length;
+ result.push(`${line.replace(/^#+\s/, '')}`);
+ }
+ // Check for horizontal rules
+ else if (line.match(/^(-{3,}|\*{3,}|_{3,})$/)) {
+ result.push('
');
+ }
+ // Handle empty lines
+ else if (line === '') {
+ if (inList) {
+ result.push('
');
+ inList = false;
+ }
+ result.push('
');
+ }
+ // Regular paragraph
+ else {
+ if (inList) {
+ result.push('');
+ inList = false;
+ }
+ result.push('' + line + '
');
+ }
+ }
+
+ // Close any open list
+ if (inList) {
+ result.push('');
+ }
+
+ return result.join('\n');
+ }
+
initMathJax() {
// Configure MathJax
window.MathJax = {