mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Markdown: Add ability to exclude specific strings
A comma-separated list of markdown strings provided by the user can be excluded as needed. This is combined with the set chat separator to provide a seamless experience when chatting. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@ -1628,7 +1628,6 @@
|
||||
<input id="custom_chat_separator" class="text_pole textarea_compact" type="text" placeholder="<START>" maxlength="100" />
|
||||
</div>
|
||||
</div>
|
||||
<!--
|
||||
<div>
|
||||
<h4 data-i18n="Non-markdown strings">
|
||||
Non-markdown strings
|
||||
@ -1637,7 +1636,6 @@
|
||||
<input id="markdown_escape_strings" class="text_pole textarea_compact" type="text" placeholder="separate with commas w/o space between" maxlength="100" />
|
||||
</div>
|
||||
</div>
|
||||
-->
|
||||
<div>
|
||||
|
||||
<h4 data-i18n="Instruct mode">Instruct mode
|
||||
|
@ -150,7 +150,7 @@ import {
|
||||
} from "./scripts/secrets.js";
|
||||
import { EventEmitter } from './scripts/eventemitter.js';
|
||||
import { context_settings, loadContextTemplatesFromSettings } from "./scripts/context-template.js";
|
||||
import { dinkusExtension } from "./scripts/showdown-dinkus.js";
|
||||
import { markdownExclusionExt } from "./scripts/showdown-exclusion.js";
|
||||
import { setFloatingPrompt } from "./scripts/extensions/floating-prompt/index.js";
|
||||
|
||||
//exporting functions and vars for mods
|
||||
@ -545,7 +545,7 @@ function reloadMarkdownProcessor(render_formulas = false) {
|
||||
// Maybe move this into power_user init?
|
||||
setTimeout(() => {
|
||||
if (power_user) {
|
||||
converter.addExtension(dinkusExtension(), 'dinkus');
|
||||
converter.addExtension(markdownExclusionExt(), 'exclusion');
|
||||
}
|
||||
}, 1)
|
||||
|
||||
|
@ -93,7 +93,7 @@ let power_user = {
|
||||
multigen_first_chunk: 50,
|
||||
multigen_next_chunks: 30,
|
||||
custom_chat_separator: '',
|
||||
// markdown_escape_strings: '',
|
||||
markdown_escape_strings: '',
|
||||
fast_ui_mode: true,
|
||||
avatar_style: avatar_styles.ROUND,
|
||||
chat_display: chat_styles.DEFAULT,
|
||||
@ -562,7 +562,7 @@ function loadPowerUserSettings(settings, data) {
|
||||
$("#include_newline_checkbox").prop("checked", power_user.include_newline);
|
||||
$('#render_formulas').prop("checked", power_user.render_formulas);
|
||||
$("#custom_chat_separator").val(power_user.custom_chat_separator);
|
||||
//$("#markdown_escape_strings").val(power_user.markdown_escape_strings);
|
||||
$("#markdown_escape_strings").val(power_user.markdown_escape_strings);
|
||||
$("#fast_ui_mode").prop("checked", power_user.fast_ui_mode);
|
||||
$("#waifuMode").prop("checked", power_user.waifuMode);
|
||||
$("#movingUImode").prop("checked", power_user.movingUI);
|
||||
@ -1009,13 +1009,13 @@ $(document).ready(() => {
|
||||
saveSettingsDebounced();
|
||||
reloadMarkdownProcessor(power_user.render_formulas);
|
||||
});
|
||||
/*
|
||||
|
||||
$("#markdown_escape_strings").on('input', function () {
|
||||
power_user.markdown_escape_strings = $(this).val();
|
||||
saveSettingsDebounced();
|
||||
reloadMarkdownProcessor(power_user.render_formulas);
|
||||
});
|
||||
*/
|
||||
|
||||
$("#multigen").change(function () {
|
||||
power_user.multigen = $(this).prop("checked");
|
||||
saveSettingsDebounced();
|
||||
|
@ -1,25 +0,0 @@
|
||||
import { power_user } from './power-user.js';
|
||||
|
||||
// Showdown extension to make chat separators (dinkuses) ignore markdown formatting
|
||||
export const dinkusExtension = () => {
|
||||
if (!power_user) {
|
||||
console.log("Showdown-dinkus extension: power_user wasn't found! Returning.");
|
||||
return []
|
||||
}
|
||||
|
||||
// Create an escaped sequence so the regex can work with any character
|
||||
const savedDinkus = power_user.custom_chat_separator
|
||||
|
||||
// No dinkus? No extension!
|
||||
if (!savedDinkus || savedDinkus.trim().length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const escapedDinkus = savedDinkus.split('').map((e) => `\\${e}`).join('');
|
||||
const replaceRegex = new RegExp(`^(${escapedDinkus})\n`, "gm")
|
||||
return [{
|
||||
type: "lang",
|
||||
regex: replaceRegex,
|
||||
replace: (match) => match.replace(replaceRegex, `\u0000${savedDinkus} \n`)
|
||||
}];
|
||||
}
|
35
public/scripts/showdown-exclusion.js
Normal file
35
public/scripts/showdown-exclusion.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { power_user } from './power-user.js';
|
||||
|
||||
// Showdown extension to make chat separators (dinkuses) ignore markdown formatting
|
||||
export const markdownExclusionExt = () => {
|
||||
if (!power_user) {
|
||||
console.log("Showdown-dinkus extension: power_user wasn't found! Returning.");
|
||||
return []
|
||||
}
|
||||
|
||||
let combinedExcludeString = '';
|
||||
if (power_user.custom_chat_separator) {
|
||||
combinedExcludeString += `${power_user.custom_chat_separator},`;
|
||||
}
|
||||
|
||||
if (power_user.markdown_escape_strings) {
|
||||
combinedExcludeString += power_user.markdown_escape_strings;
|
||||
}
|
||||
|
||||
const escapedExclusions = combinedExcludeString
|
||||
.split(",")
|
||||
.map((element) => `(${element.split('').map((char) => `\\${char}`).join('')})`);
|
||||
|
||||
|
||||
// No exclusions? No extension!
|
||||
if (!combinedExcludeString || combinedExcludeString.length === 0 || escapedExclusions.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const replaceRegex = new RegExp(`^(${escapedExclusions.join("|")})\n`, "gm");
|
||||
return [{
|
||||
type: "lang",
|
||||
regex: replaceRegex,
|
||||
replace: ((match) => match.replace(replaceRegex, `\u0000${match} \n`))
|
||||
}];
|
||||
}
|
Reference in New Issue
Block a user