discourse/plugins/spoiler-alert/assets/javascripts/lib/discourse-markdown/spoiler-alert.js
Blake Erickson 89580ee379
FEATURE: Bundle discourse-spoiler-alert plugin into core (#24030)
* FEATURE: Bundle discourse-spoiler-alert plugin into core

Formerly https://github.com/discourse/discourse-spoiler-alert

* DEV: Switch to new addComposerToolbarPopupMenuOption plugin API

`api.addToolbarPopupMenuOptionsCallback` has been deprecated in 913fd3a7b3

This commit was just added to the plugin, so adding it here.

49f86ba72e
2023-10-23 13:50:43 -06:00

47 lines
1.1 KiB
JavaScript

const CONTAINS_BLOCK_REGEX = /\n|<img|!\[[^\]]*\][(\[]/;
function insertSpoiler(_, spoiler) {
const element = CONTAINS_BLOCK_REGEX.test(spoiler) ? "div" : "span";
return `<${element} class='spoiler'>${spoiler}</${element}>`;
}
function replaceSpoilers(text) {
text = text || "";
while (
text !==
(text = text.replace(
/\[spoiler\]((?:(?!\[spoiler\]|\[\/spoiler\])[\S\s])*)\[\/spoiler\]/gi,
insertSpoiler
))
) {}
return text;
}
function setupMarkdownIt(helper) {
helper.registerOptions((opts, siteSettings) => {
opts.features["spoiler-alert"] = !!siteSettings.spoiler_enabled;
});
helper.registerPlugin((md) => {
md.inline.bbcode.ruler.push("spoiler", {
tag: "spoiler",
wrap: "span.spoiler",
});
md.block.bbcode.ruler.push("spoiler", {
tag: "spoiler",
wrap: "div.spoiler",
});
});
}
export function setup(helper) {
helper.allowList(["span.spoiler", "div.spoiler"]);
if (helper.markdownIt) {
setupMarkdownIt(helper);
} else {
helper.addPreProcessor(replaceSpoilers);
}
}