refactor(features): extract HTML cleaning regex setup to standalone module

Extract ensureHtmlCleaningRegex into src/systems/features/htmlCleaning.js.
This module automatically imports the HTML cleaning regex script that
strips HTML tags from outgoing prompts to prevent formatting issues.

Passes SillyTavern imports (st_extension_settings, saveSettingsDebounced)
as parameters to avoid deep module import path issues.

- Create htmlCleaning.js with regex import logic
- Update index.js to import and use the new module with parameters
- Maintain backward compatibility with existing functionality
This commit is contained in:
Lucas 'Paperboy' Rose-Winters
2025-10-17 13:48:49 +11:00
parent 3473f2ac44
commit 175ff9560c
2 changed files with 67 additions and 59 deletions
+2 -59
View File
@@ -97,6 +97,7 @@ import {
// Feature modules
import { setupPlotButtons } from './src/systems/features/plotProgression.js';
import { setupClassicStatsButtons } from './src/systems/features/classicStats.js';
import { ensureHtmlCleaningRegex } from './src/systems/features/htmlCleaning.js';
// Old state variable declarations removed - now imported from core modules
// (extensionSettings, lastGeneratedData, committedTrackerData, etc. are now in src/core/state.js)
@@ -711,64 +712,6 @@ function onMessageSwiped(messageIndex) {
updateChatThoughts();
}
/**
* Automatically imports the HTML cleaning regex script if it doesn't already exist.
* This regex removes HTML tags from outgoing prompts to prevent formatting issues.
*/
async function ensureHtmlCleaningRegex() {
try {
// Check if the HTML cleaning regex already exists
const scriptName = 'Clean HTML (From Outgoing Prompt)';
const existingScripts = st_extension_settings?.regex || [];
const alreadyExists = existingScripts.some(script => script.scriptName === scriptName);
if (alreadyExists) {
console.log('[RPG Companion] HTML cleaning regex already exists, skipping import');
return;
}
// Generate a UUID for the script
const uuidv4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
// Create the regex script object based on the attached file
const regexScript = {
id: uuidv4(),
scriptName: scriptName,
findRegex: '/\\s?<(?!\\!--)(?:\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>/g',
replaceString: '',
trimStrings: [],
placement: [2], // 2 = Input (affects outgoing prompt)
disabled: false,
markdownOnly: false,
promptOnly: true,
runOnEdit: true,
substituteRegex: 0,
minDepth: null,
maxDepth: null
};
// Add to global regex scripts
if (!Array.isArray(st_extension_settings.regex)) {
st_extension_settings.regex = [];
}
st_extension_settings.regex.push(regexScript);
// Save the changes using the already-imported function
saveSettingsDebounced();
console.log('[RPG Companion] ✅ HTML cleaning regex imported successfully');
} catch (error) {
console.error('[RPG Companion] Failed to import HTML cleaning regex:', error);
// Don't throw - this is a nice-to-have feature
}
}
/**
* Update the persona avatar image when user switches personas
@@ -817,7 +760,7 @@ jQuery(async () => {
loadChatData();
// Import the HTML cleaning regex if needed
await ensureHtmlCleaningRegex();
await ensureHtmlCleaningRegex(st_extension_settings, saveSettingsDebounced);
// Register event listeners
eventSource.on(event_types.MESSAGE_SENT, onMessageSent);
+65
View File
@@ -0,0 +1,65 @@
/**
* HTML Cleaning Module
* Automatically imports HTML cleaning regex to strip HTML tags from outgoing prompts
*/
/**
* Automatically imports the HTML cleaning regex script if it doesn't already exist.
* This regex removes HTML tags from outgoing prompts to prevent formatting issues.
* @param {Object} st_extension_settings - SillyTavern extension settings object
* @param {Function} saveSettingsDebounced - Function to save settings
*/
export async function ensureHtmlCleaningRegex(st_extension_settings, saveSettingsDebounced) {
try {
// Check if the HTML cleaning regex already exists
const scriptName = 'Clean HTML (From Outgoing Prompt)';
const existingScripts = st_extension_settings?.regex || [];
const alreadyExists = existingScripts.some(script => script.scriptName === scriptName);
if (alreadyExists) {
console.log('[RPG Companion] HTML cleaning regex already exists, skipping import');
return;
}
// Generate a UUID for the script
const uuidv4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
// Create the regex script object based on the attached file
const regexScript = {
id: uuidv4(),
scriptName: scriptName,
findRegex: '/\\s?<(?!\\!--)(?:\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>/g',
replaceString: '',
trimStrings: [],
placement: [2], // 2 = Input (affects outgoing prompt)
disabled: false,
markdownOnly: false,
promptOnly: true,
runOnEdit: true,
substituteRegex: 0,
minDepth: null,
maxDepth: null
};
// Add to global regex scripts
if (!Array.isArray(st_extension_settings.regex)) {
st_extension_settings.regex = [];
}
st_extension_settings.regex.push(regexScript);
// Save the changes using the already-imported function
saveSettingsDebounced();
console.log('[RPG Companion] ✅ HTML cleaning regex imported successfully');
} catch (error) {
console.error('[RPG Companion] Failed to import HTML cleaning regex:', error);
// Don't throw - this is a nice-to-have feature
}
}