Merge pull request #671 from bdashore3/dev

Regex: Make MD formatting granular
This commit is contained in:
Cohee
2023-07-08 21:14:14 +03:00
committed by GitHub
4 changed files with 68 additions and 15 deletions

View File

@@ -56,13 +56,7 @@
<div class="flex-container">
<div class="wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
<small>Placement</small>
<div>
<label class="checkbox flex-container">
<input type="checkbox" name="replace_position" value="0">
<span data-i18n="Author's Note">Markdown Display</span>
</label>
</div>
<small>Affects</small>
<div>
<label class="checkbox flex-container">
<input type="checkbox" name="replace_position" value="1">
@@ -94,6 +88,10 @@
<input type="checkbox" name="disabled" />
<span data-i18n="Disabled">Disabled</span>
</label>
<label class="checkbox flex-container">
<input type="checkbox" name="only_format_display" />
<span data-i18n="Only Format Display">Only Format Display</span>
</label>
<label class="checkbox flex-container">
<input type="checkbox" name="run_on_edit" />
<span data-i18n="Run On Edit">Run On Edit</span>

View File

@@ -7,6 +7,7 @@ export {
}
const regex_placement = {
// MD Display is deprecated. Do not use.
MD_DISPLAY: 0,
USER_INPUT: 1,
AI_OUTPUT: 2,
@@ -38,13 +39,17 @@ function regexFromString(input) {
}
// Parent function to fetch a regexed version of a raw string
function getRegexedString(rawString, placement, { characterOverride } = {}) {
function getRegexedString(rawString, placement, { characterOverride, isMarkdown } = {}) {
let finalString = rawString;
if (extension_settings.disabledExtensions.includes("regex") || !rawString || placement === undefined) {
return finalString;
}
extension_settings.regex.forEach((script) => {
if (script.markdownOnly && !isMarkdown) {
return;
}
if (script.placement.includes(placement)) {
finalString = runRegexScript(script, finalString, { characterOverride });
}

View File

@@ -48,12 +48,10 @@ async function saveRegexScript(regexScript, existingScriptIndex) {
saveSettingsDebounced();
await loadRegexScripts();
// Markdown is global, so reload the chat.
if (regexScript.placement.includes(regex_placement.MD_DISPLAY)) {
const currentChatId = getCurrentChatId();
if (currentChatId !== undefined && currentChatId !== null) {
await reloadCurrentChat();
}
// Reload the current chat to undo previous markdown
const currentChatId = getCurrentChatId();
if (currentChatId !== undefined && currentChatId !== null) {
await reloadCurrentChat();
}
}
@@ -113,6 +111,9 @@ async function onRegexEditorOpenClick(existingId) {
editorHtml
.find(`input[name="disabled"]`)
.prop("checked", existingScript.disabled ?? false);
editorHtml
.find(`input[name="only_format_display"]`)
.prop("checked", existingScript.markdownOnly ?? false);
editorHtml
.find(`input[name="run_on_edit"]`)
.prop("checked", existingScript.runOnEdit ?? false);
@@ -130,6 +131,10 @@ async function onRegexEditorOpenClick(existingId) {
});
}
} else {
editorHtml
.find(`input[name="only_format_display"]`)
.prop("checked", true);
editorHtml
.find(`input[name="run_on_edit"]`)
.prop("checked", true);
@@ -157,6 +162,10 @@ async function onRegexEditorOpenClick(existingId) {
editorHtml
.find(`input[name="disabled"]`)
.prop("checked"),
markdownOnly:
editorHtml
.find(`input[name="only_format_display"]`)
.prop("checked"),
runOnEdit:
editorHtml
.find(`input[name="run_on_edit"]`)
@@ -176,9 +185,36 @@ async function onRegexEditorOpenClick(existingId) {
}
}
// Common settings migration function. Some parts will eventually be removed
// TODO: Maybe migrate placement to strings?
function migrateSettings() {
let performSave = false;
// Current: If MD Display is present in placement, remove it and add new placements/MD option
extension_settings.regex.forEach((script) => {
if (script.placement.includes(regex_placement.MD_DISPLAY)) {
script.placement = script.placement.length === 1 ?
Object.values(regex_placement).filter((e) => e !== regex_placement.MD_DISPLAY) :
script.placement = script.placement.filter((e) => e !== regex_placement.MD_DISPLAY);
script.markdownOnly = true
performSave = true;
}
});
if (performSave) {
saveSettingsDebounced();
}
}
// Workaround for loading in sequence with other extensions
// NOTE: Always puts extension at the top of the list, but this is fine since it's static
jQuery(async () => {
if (extension_settings.regex) {
migrateSettings();
}
// Manually disable the extension since static imports auto-import the JS file
if (extension_settings.disabledExtensions.includes("regex")) {
return;