Blame Raw
· · · 39 lines (1.5 KB)
0 contributors
1import { power_user } from './power-user.js';
2import { substituteParams } from '../script.js';
3
4/**
5 * Showdown extension to make chat separators (dinkuses) ignore markdown formatting
6 * @returns {import('showdown').ShowdownExtension[]} An array of Showdown extensions
7 */
8export const markdownExclusionExt = () => {
9 if (!power_user) {
10 console.log('Showdown-dinkus extension: power_user wasn\'t found! Returning.');
11 return [];
12 }
13
14 // The extension will only be applied if the user has non-empty "Non-markdown strings"
15 // Changing the string in the UI reloads the processor, so we don't need to worry about it
16 if (!power_user.markdown_escape_strings) {
17 return [];
18 }
19
20 // Escape the strings to be excluded from markdown parsing
21 // Function is evaluated every time, so we don't care about stale macros in the strings
22 return [{
23 type: 'lang',
24 filter: (text) => {
25 const escapedExclusions = substituteParams(power_user.markdown_escape_strings)
26 .split(',')
27 .filter((element) => element.length > 0)
28 .map((element) => `(${element.split('').map((char) => `\\${char}`).join('')})`);
29
30 // No exclusions? No extension!
31 if (escapedExclusions.length === 0) {
32 return text;
33 }
34
35 const replaceRegex = new RegExp(`^(${escapedExclusions.join('|')})\n`, 'gm');
36 return text.replace(replaceRegex, ((match) => match.replace(replaceRegex, `\u0000${match} \n`)));
37 },
38 }];
39};