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

@ -1338,6 +1338,14 @@
<label for="auto_save_msg_edits"><input id="auto_save_msg_edits" type="checkbox" /> <label for="auto_save_msg_edits"><input id="auto_save_msg_edits" type="checkbox" />
Auto-save Message Edits Auto-save Message Edits
</label> </label>
<label for="auto_fix_generated_markdown">
<input id="auto_fix_generated_markdown" type="checkbox" />
Auto-fix Markdown
</label>
<label for="auto_scroll_chat_to_bottom">
<input id="auto_scroll_chat_to_bottom" type="checkbox" />
Auto-scroll Chat
</label>
</div> </div>
</div> </div>

View File

@ -46,6 +46,7 @@ import {
loadPowerUserSettings, loadPowerUserSettings,
playMessageSound, playMessageSound,
sortCharactersList, sortCharactersList,
fixMarkdown,
power_user, power_user,
pygmalion_options, pygmalion_options,
tokenizers, tokenizers,
@ -453,8 +454,6 @@ let novel_tier;
let novelai_settings; let novelai_settings;
let novelai_setting_names; let novelai_setting_names;
let scrollChatToBottomAuto = true;
let autoFixGeneratedTextMarkdown = true;
//css //css
var bg1_toggle = true; // inits the BG as BG1 var bg1_toggle = true; // inits the BG as BG1
var css_mes_bg = $('<div class="mes"></div>').css("background"); var css_mes_bg = $('<div class="mes"></div>').css("background");
@ -902,6 +901,10 @@ function messageFormating(mes, ch_name, isSystem, forceAvatar) {
mes = ''; mes = '';
} }
if (power_user.auto_fix_generated_markdown) {
mes = fixMarkdown(mes);
}
if (this_chid != undefined && !isSystem) if (this_chid != undefined && !isSystem)
mes = mes.replaceAll("<", "&lt;").replaceAll(">", "&gt;"); //for welcome message mes = mes.replaceAll("<", "&lt;").replaceAll(">", "&gt;"); //for welcome message
if (this_chid === undefined && !selected_group) { if (this_chid === undefined && !selected_group) {
@ -1071,7 +1074,7 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true
} }
function scrollChatToBottom() { function scrollChatToBottom() {
if (scrollChatToBottomAuto) { if (power_user.auto_scroll_chat_to_bottom) {
var $textchat = $("#chat"); var $textchat = $("#chat");
$textchat.scrollTop(($textchat[0].scrollHeight)); $textchat.scrollTop(($textchat[0].scrollHeight));
} }
@ -2219,38 +2222,6 @@ function extractMessageFromData(data) {
return getMessage; 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) { function cleanUpMessage(getMessage, isImpersonate) {
const nameToTrim = isImpersonate ? name2 : name1; const nameToTrim = isImpersonate ? name2 : name1;
if (power_user.collapse_newlines) { 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); getMessage = fixMarkdown(getMessage);
} }
return getMessage; return getMessage;
@ -2937,12 +2908,6 @@ async function getSettings(type) {
// Load- character tags // Load- character tags
loadTagsSettings(settings); 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 //Enable GUI deference settings if GUI is selected for Kobold
if (main_api === "kobold") { if (main_api === "kobold") {
} }

View File

@ -12,6 +12,7 @@ export {
collapseNewlines, collapseNewlines,
playMessageSound, playMessageSound,
sortCharactersList, sortCharactersList,
fixMarkdown,
power_user, power_user,
pygmalion_options, pygmalion_options,
tokenizers, tokenizers,
@ -85,6 +86,9 @@ let power_user = {
movingUI: false, movingUI: false,
noShadows: false, noShadows: false,
theme: 'Default (Dark)', theme: 'Default (Dark)',
auto_scroll_chat_to_bottom: true,
auto_fix_generated_markdown: true,
}; };
let themes = []; let themes = [];
@ -133,6 +137,37 @@ function collapseNewlines(x) {
return x.replaceAll(/\n+/g, "\n"); 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() { function switchUiMode() {
const fastUi = localStorage.getItem(storage_keys.fast_ui_mode); const fastUi = localStorage.getItem(storage_keys.fast_ui_mode);
power_user.fast_ui_mode = fastUi === null ? true : fastUi == "true"; 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.font_scale = Number(localStorage.getItem(storage_keys.font_scale) ?? 1);
power_user.blur_strength = Number(localStorage.getItem(storage_keys.blur_strength) ?? 10); 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); $(`#tokenizer option[value="${power_user.tokenizer}"]`).attr('selected', true);
$(`#pygmalion_formatting option[value=${power_user.pygmalion_formatting}]`).attr("selected", true); $(`#pygmalion_formatting option[value=${power_user.pygmalion_formatting}]`).attr("selected", true);
$("#collapse-newlines-checkbox").prop("checked", power_user.collapse_newlines); $("#collapse-newlines-checkbox").prop("checked", power_user.collapse_newlines);
@ -647,12 +684,21 @@ $(document).ready(() => {
saveSettingsDebounced(); saveSettingsDebounced();
}); });
$("#multigen_next_chunks").on('input', function () { $("#multigen_next_chunks").on('input', function () {
power_user.multigen_next_chunks = Number($(this).val()); power_user.multigen_next_chunks = Number($(this).val());
saveSettingsDebounced(); 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 () { $("#tokenizer").on('change', function () {
const value = $(this).find(':selected').val(); const value = $(this).find(':selected').val();
power_user.tokenizer = Number(value); power_user.tokenizer = Number(value);