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