From c8a09f933318dbcbe32bf2c12b66921493c8d941 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 06:00:24 -0300 Subject: [PATCH 1/6] Fix markdown on replies & no scroll to bottom --- public/script.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index 40b9e67b7..fdc94e839 100644 --- a/public/script.js +++ b/public/script.js @@ -1068,8 +1068,8 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true } function scrollChatToBottom() { - var $textchat = $("#chat"); - $textchat.scrollTop(($textchat[0].scrollHeight)); + // var $textchat = $("#chat"); + // $textchat.scrollTop(($textchat[0].scrollHeight)); } function substituteParams(content, _name1, _name2) { @@ -2213,6 +2213,38 @@ function extractMessageFromData(data) { return getMessage; } + +function fixMarkdown(text) { + // fix formatting problems in markdown + // e.g.: + // "^example * text*\n" -> "^example *text*\n" + // "^*example * text\n" -> "^*example* text\n" + // "^example *text *\n" -> "^example *text*\n" + // "^* example * text\n" -> "^*example* text\n" + // take note that the side you move the asterisk depends on where its pairing is + // i.e. both of the following strings have the same broken asterisk ' * ', but you move the first to the left and the second to the right, to match the non-broken asterisk "^example * text*\n" "^*example * text\n" + // and you HAVE to handle the cases where multiple pairs of asterisks exist in the same line + // i.e. "^example * text* * harder problem *\n" -> "^example *text* *harder problem*\n" + + // Find pairs of asterisks and capture the text in between them + const asterisks = /\*{2}|(\*\s*\S.*?\S\s*\*)/g; + let matches = []; + let match; + while ((match = asterisks.exec(text)) !== null) { + matches.push(match); + } + + // Iterate through the matches and replace spaces immediately beside asterisks + let newText = text; + for (let i = matches.length - 1; i >= 0; i--) { + let matchText = matches[i][0]; + let replacementText = matchText.replace(/\s*(?<=\*)\s|\s(?=\*)\s*/g, ''); + newText = newText.slice(0, matches[i].index) + replacementText + newText.slice(matches[i].index + matchText.length); + } + + return newText; +} + function cleanUpMessage(getMessage, isImpersonate) { const nameToTrim = isImpersonate ? name2 : name1; if (power_user.collapse_newlines) { @@ -2220,6 +2252,7 @@ function cleanUpMessage(getMessage, isImpersonate) { } getMessage = $.trim(getMessage); + getMessage = getMessage.replace(/\s+$/gm, ""); if (is_pygmalion) { getMessage = getMessage.replace(//g, name1); getMessage = getMessage.replace(//g, name2); @@ -2253,7 +2286,7 @@ function cleanUpMessage(getMessage, isImpersonate) { } } } - + getMessage = fixMarkdown(getMessage); return getMessage; } From 2739ea0389213372324f7aabffddfec5e116b5ac Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 12:38:09 -0300 Subject: [PATCH 2/6] markdownfixer now supports: italic (_), strikethrough (~~), bold (**) --- public/script.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/script.js b/public/script.js index fdc94e839..883df8d81 100644 --- a/public/script.js +++ b/public/script.js @@ -2226,19 +2226,19 @@ function fixMarkdown(text) { // and you HAVE to handle the cases where multiple pairs of asterisks exist in the same line // i.e. "^example * text* * harder problem *\n" -> "^example *text* *harder problem*\n" - // Find pairs of asterisks and capture the text in between them - const asterisks = /\*{2}|(\*\s*\S.*?\S\s*\*)/g; + // Find pairs of formatting characters and capture the text in between them + const format = /(\*{2}|__|\*|_|~~)(\s*\S.*?\S\s*)(\1)/g; let matches = []; let match; - while ((match = asterisks.exec(text)) !== null) { + while ((match = format.exec(text)) !== null) { matches.push(match); } - // Iterate through the matches and replace spaces immediately beside asterisks + // Iterate through the matches and replace consecutive spaces immediately beside formatting characters let newText = text; for (let i = matches.length - 1; i >= 0; i--) { let matchText = matches[i][0]; - let replacementText = matchText.replace(/\s*(?<=\*)\s|\s(?=\*)\s*/g, ''); + let replacementText = matchText.replace(/\s*(?<=\S)(\s+)(?=\S)/g, ''); newText = newText.slice(0, matches[i].index) + replacementText + newText.slice(matches[i].index + matchText.length); } From cee92cddf34ca6d3127027cdf7d18ea53c27d8da Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 12:50:05 -0300 Subject: [PATCH 3/6] added changes are optional --- public/script.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/public/script.js b/public/script.js index 883df8d81..6775261f9 100644 --- a/public/script.js +++ b/public/script.js @@ -440,6 +440,8 @@ let novel_tier; let novelai_settings; let novelai_setting_names; +let scrollChatToBottomAuto = true; +let autoFixGeneratedTextMarkdown = true; //css var bg1_toggle = true; // inits the BG as BG1 var css_mes_bg = $('
').css("background"); @@ -1068,8 +1070,10 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true } function scrollChatToBottom() { - // var $textchat = $("#chat"); - // $textchat.scrollTop(($textchat[0].scrollHeight)); + if (scrollChatToBottomAuto) { + var $textchat = $("#chat"); + $textchat.scrollTop(($textchat[0].scrollHeight)); + } } function substituteParams(content, _name1, _name2) { @@ -2873,6 +2877,12 @@ async function getSettings(type) { // Load- character tags loadTagsSettings(settings); + // Others, TODO: move to power user settings + if (settings.scrollChatToBottomAuto !== undefined) + scrollChatToBottomAuto = !!settings.scrollChatToBottomAuto; + if (settings.autoFixGeneratedTextMarkdown !== undefined) + autoFixGeneratedTextMarkdown = !!settings.autoFixGeneratedTextMarkdown; + //Enable GUI deference settings if GUI is selected for Kobold if (main_api === "kobold") { } From 759c5d6d9661ac13012faa754128dc7c05a3139f Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 12:54:09 -0300 Subject: [PATCH 4/6] finished + 1 comment --- public/script.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index de431ccef..b7580c9aa 100644 --- a/public/script.js +++ b/public/script.js @@ -2256,6 +2256,7 @@ function cleanUpMessage(getMessage, isImpersonate) { } getMessage = $.trim(getMessage); + // remove trailing invisible whitespace from lines getMessage = getMessage.replace(/\s+$/gm, ""); if (is_pygmalion) { getMessage = getMessage.replace(//g, name1); @@ -2290,7 +2291,9 @@ function cleanUpMessage(getMessage, isImpersonate) { } } } - getMessage = fixMarkdown(getMessage); + if (autoFixGeneratedTextMarkdown) { + getMessage = fixMarkdown(getMessage); + } return getMessage; } From 6e4aba900fbd96a603628db6d062d4b37b9dcdc4 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 13:28:02 -0300 Subject: [PATCH 5/6] Fixed fixer --- public/script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index b7580c9aa..fb6052da4 100644 --- a/public/script.js +++ b/public/script.js @@ -2231,18 +2231,18 @@ function fixMarkdown(text) { // i.e. "^example * text* * harder problem *\n" -> "^example *text* *harder problem*\n" // Find pairs of formatting characters and capture the text in between them - const format = /(\*{2}|__|\*|_|~~)(\s*\S.*?\S\s*)(\1)/g; + const format = /(\*|_|~){1,2}([\s\S]*?)\1{1,2}/gm; let matches = []; let match; while ((match = format.exec(text)) !== null) { matches.push(match); } - // Iterate through the matches and replace consecutive spaces immediately beside formatting characters + // Iterate through the matches and replace adjacent spaces immediately beside formatting characters let newText = text; for (let i = matches.length - 1; i >= 0; i--) { let matchText = matches[i][0]; - let replacementText = matchText.replace(/\s*(?<=\S)(\s+)(?=\S)/g, ''); + let replacementText = matchText.replace(/(\*|_|~)(\s+)|(\s+)(\*|_|~)/g, '$1$4'); newText = newText.slice(0, matches[i].index) + replacementText + newText.slice(matches[i].index + matchText.length); } From 5a387e3e0b3a1fc048ef3428c7f21541ac759d08 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 28 Apr 2023 13:41:53 -0300 Subject: [PATCH 6/6] clarified a comment --- public/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index fb6052da4..ff2170ff4 100644 --- a/public/script.js +++ b/public/script.js @@ -2256,7 +2256,9 @@ function cleanUpMessage(getMessage, isImpersonate) { } getMessage = $.trim(getMessage); - // remove trailing invisible whitespace from lines + // trailing invisible whitespace before every newlines, on a multiline string + // "trailing whitespace on newlines \nevery line of the string \n?sample text" -> + // "trailing whitespace on newlines\nevery line of the string\nsample text" getMessage = getMessage.replace(/\s+$/gm, ""); if (is_pygmalion) { getMessage = getMessage.replace(//g, name1);