diff --git a/public/index.html b/public/index.html index 235d727be..47dff8f28 100644 --- a/public/index.html +++ b/public/index.html @@ -1338,6 +1338,14 @@ + + diff --git a/public/script.js b/public/script.js index e690eae8a..502023e4c 100644 --- a/public/script.js +++ b/public/script.js @@ -46,6 +46,7 @@ import { loadPowerUserSettings, playMessageSound, sortCharactersList, + fixMarkdown, power_user, pygmalion_options, tokenizers, @@ -453,8 +454,6 @@ 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"); @@ -902,6 +901,10 @@ function messageFormating(mes, ch_name, isSystem, forceAvatar) { mes = ''; } + if (power_user.auto_fix_generated_markdown) { + mes = fixMarkdown(mes); + } + if (this_chid != undefined && !isSystem) mes = mes.replaceAll("<", "<").replaceAll(">", ">"); //for welcome message if (this_chid === undefined && !selected_group) { @@ -1071,7 +1074,7 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true } function scrollChatToBottom() { - if (scrollChatToBottomAuto) { + if (power_user.auto_scroll_chat_to_bottom) { var $textchat = $("#chat"); $textchat.scrollTop(($textchat[0].scrollHeight)); } @@ -2219,38 +2222,6 @@ 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 formatting characters and capture the text in between them - 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 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+)(\*|_|~)/g, '$1$4'); - 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) { @@ -2295,7 +2266,7 @@ function cleanUpMessage(getMessage, isImpersonate) { } } } - if (autoFixGeneratedTextMarkdown) { + if (power_user.auto_fix_generated_markdown) { getMessage = fixMarkdown(getMessage); } return getMessage; @@ -2937,12 +2908,6 @@ 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") { } diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index 2abf23b7c..a75305cc4 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -12,6 +12,7 @@ export { collapseNewlines, playMessageSound, sortCharactersList, + fixMarkdown, power_user, pygmalion_options, tokenizers, @@ -85,6 +86,9 @@ let power_user = { movingUI: false, noShadows: false, theme: 'Default (Dark)', + + auto_scroll_chat_to_bottom: true, + auto_fix_generated_markdown: true, }; let themes = []; @@ -133,6 +137,37 @@ function collapseNewlines(x) { return x.replaceAll(/\n+/g, "\n"); } +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 formatting characters and capture the text in between them + 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 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+)(\*|_|~)/g, '$1$4'); + newText = newText.slice(0, matches[i].index) + replacementText + newText.slice(matches[i].index + matchText.length); + } + + return newText; +} + function switchUiMode() { const fastUi = localStorage.getItem(storage_keys.fast_ui_mode); power_user.fast_ui_mode = fastUi === null ? true : fastUi == "true"; @@ -301,6 +336,8 @@ function loadPowerUserSettings(settings, data) { power_user.font_scale = Number(localStorage.getItem(storage_keys.font_scale) ?? 1); power_user.blur_strength = Number(localStorage.getItem(storage_keys.blur_strength) ?? 10); + $('#auto_fix_generated_markdown').prop("checked", power_user.auto_fix_generated_markdown); + $('#auto_scroll_chat_to_bottom').prop("checked", power_user.auto_scroll_chat_to_bottom); $(`#tokenizer option[value="${power_user.tokenizer}"]`).attr('selected', true); $(`#pygmalion_formatting option[value=${power_user.pygmalion_formatting}]`).attr("selected", true); $("#collapse-newlines-checkbox").prop("checked", power_user.collapse_newlines); @@ -647,12 +684,21 @@ $(document).ready(() => { saveSettingsDebounced(); }); - $("#multigen_next_chunks").on('input', function () { power_user.multigen_next_chunks = Number($(this).val()); saveSettingsDebounced(); }); + $('#auto_fix_generated_markdown').on('input', function () { + power_user.auto_fix_generated_markdown = !!$(this).prop('checked'); + saveSettingsDebounced(); + }); + + $('#auto_scroll_chat_to_bottom').on("input", function () { + power_user.auto_scroll_chat_to_bottom = !!$(this).prop('checked'); + saveSettingsDebounced(); + }); + $("#tokenizer").on('change', function () { const value = $(this).find(':selected').val(); power_user.tokenizer = Number(value);