From 39c588f30e4d642738ce0d0aeffd116cd859e09a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 3 Mar 2024 15:26:25 +0200 Subject: [PATCH] Showdown: parse single underscores as italics --- public/script.js | 2 ++ public/scripts/showdown-underscore.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 public/scripts/showdown-underscore.js diff --git a/public/script.js b/public/script.js index 29b848769..a664c12d2 100644 --- a/public/script.js +++ b/public/script.js @@ -172,6 +172,7 @@ import { } from './scripts/secrets.js'; import { EventEmitter } from './lib/eventemitter.js'; import { markdownExclusionExt } from './scripts/showdown-exclusion.js'; +import { markdownUnderscoreExt } from './scripts/showdown-underscore.js'; import { NOTE_MODULE_NAME, initAuthorsNote, metadata_keys, setFloatingPrompt, shouldWIAddPrompt } from './scripts/authors-note.js'; import { registerPromptManagerMigration } from './scripts/PromptManager.js'; import { getRegexedString, regex_placement } from './scripts/extensions/regex/engine.js'; @@ -690,6 +691,7 @@ function reloadMarkdownProcessor(render_formulas = false) { parseImgDimensions: true, tables: true, underline: true, + extensions: [markdownUnderscoreExt()], }); } diff --git a/public/scripts/showdown-underscore.js b/public/scripts/showdown-underscore.js new file mode 100644 index 000000000..1ff3bdabd --- /dev/null +++ b/public/scripts/showdown-underscore.js @@ -0,0 +1,22 @@ +// Showdown extension that replaces words surrounded by singular underscores with tags +export const markdownUnderscoreExt = () => { + if (!canUseNegativeLookbehind()) { + console.log('Showdown-underscore extension: Negative lookbehind not supported. Skipping.'); + return []; + } + + return [{ + type: 'lang', + regex: /\b(?$1', + }]; +}; + +function canUseNegativeLookbehind() { + try { + new RegExp('(?