Code style for #194

This commit is contained in:
SillyLossy
2023-04-28 23:34:15 +03:00
parent e06b8b959b
commit 2817a5c7a9
3 changed files with 62 additions and 43 deletions

View File

@ -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 = $('<div class="mes"></div>').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("<", "&lt;").replaceAll(">", "&gt;"); //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") {
}