Do no automatically apply non-markdown for context separators (#4003)

This commit is contained in:
Cohee
2025-05-16 19:57:56 +03:00
committed by GitHub
parent c218c1baea
commit 817474c60d

View File

@@ -1,40 +1,39 @@
import { power_user } from './power-user.js';
import { substituteParams } from '../script.js';
// Showdown extension to make chat separators (dinkuses) ignore markdown formatting
/**
* Showdown extension to make chat separators (dinkuses) ignore markdown formatting
* @returns {import('showdown').ShowdownExtension[]} An array of Showdown extensions
*/
export const markdownExclusionExt = () => {
if (!power_user) {
console.log('Showdown-dinkus extension: power_user wasn\'t found! Returning.');
return [];
}
let combinedExcludeString = '';
if (power_user.context.chat_start) {
combinedExcludeString += `${power_user.context.chat_start},`;
}
if (power_user.context.example_separator) {
combinedExcludeString += `${power_user.context.example_separator},`;
}
if (power_user.markdown_escape_strings) {
combinedExcludeString += power_user.markdown_escape_strings;
}
const escapedExclusions = combinedExcludeString
.split(',')
.filter((element) => element.length > 0)
.map((element) => `(${element.split('').map((char) => `\\${char}`).join('')})`);
// No exclusions? No extension!
if (!combinedExcludeString || combinedExcludeString.length === 0 || escapedExclusions.length === 0) {
// The extension will only be applied if the user has non-empty "Non-markdown strings"
// Changing the string in the UI reloads the processor, so we don't need to worry about it
if (!power_user.markdown_escape_strings) {
return [];
}
const replaceRegex = new RegExp(`^(${escapedExclusions.join('|')})\n`, 'gm');
// Escape the strings to be excluded from markdown parsing
// Function is evaluated every time, so we don't care about stale macros in the strings
return [{
type: 'lang',
regex: replaceRegex,
replace: ((match) => match.replace(replaceRegex, `\u0000${match} \n`)),
filter: (text) => {
const escapedExclusions = substituteParams(power_user.markdown_escape_strings)
.split(',')
.filter((element) => element.length > 0)
.map((element) => `(${element.split('').map((char) => `\\${char}`).join('')})`);
// No exclusions? No extension!
if (escapedExclusions.length === 0) {
return text;
}
const replaceRegex = new RegExp(`^(${escapedExclusions.join('|')})\n`, 'gm');
return text.replace(replaceRegex, ((match) => match.replace(replaceRegex, `\u0000${match} \n`)));
},
}];
};