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; }