SillyTavern/public/scripts/showdown-exclusion.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-07-20 20:32:15 +03:00
import { power_user } from './power-user.js';
// Showdown extension to make chat separators (dinkuses) ignore markdown formatting
export const markdownExclusionExt = () => {
if (!power_user) {
2023-12-02 13:04:51 -05:00
console.log('Showdown-dinkus extension: power_user wasn\'t found! Returning.');
2023-12-02 21:11:06 +02:00
return [];
2023-07-20 20:32:15 +03:00
}
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},`;
2023-07-20 20:32:15 +03:00
}
if (power_user.markdown_escape_strings) {
combinedExcludeString += power_user.markdown_escape_strings;
}
const escapedExclusions = combinedExcludeString
2023-12-02 13:04:51 -05:00
.split(',')
2023-07-20 20:32:15 +03:00
.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) {
return [];
}
2023-12-02 13:04:51 -05:00
const replaceRegex = new RegExp(`^(${escapedExclusions.join('|')})\n`, 'gm');
2023-07-20 20:32:15 +03:00
return [{
2023-12-02 13:04:51 -05:00
type: 'lang',
2023-07-20 20:32:15 +03:00
regex: replaceRegex,
2023-12-02 22:06:57 +02:00
replace: ((match) => match.replace(replaceRegex, `\u0000${match} \n`)),
2023-07-20 20:32:15 +03:00
}];
2023-12-02 21:11:06 +02:00
};