discourse/plugins/spoiler-alert/assets/javascripts/initializers/spoiler-alert.js
Régis Hanol 94cf1c4786 FIX: quoting a spoiler
Was "removing" (rather not re-applying) the `[spoiler]` BBCode because we were testing the **whole** class of the `span`/`div` was `spoiled` but we added another class and thus broke this functionnality.

In order to fix this issue, the test to determine whether a `span`/`div` is a spoiler, now uses a regular expression to check whether the `class` **contains** the word `spoiled`.

Reference - https://meta.discourse.org/t/quoting-spoiler-text-doesnt-include-spoiler-tags-in-the-quote/170145
2024-05-28 19:24:52 +02:00

61 lines
1.5 KiB
JavaScript

import { withPluginApi } from "discourse/lib/plugin-api";
import {
addBlockDecorateCallback,
addTagDecorateCallback,
} from "discourse/lib/to-markdown";
import applySpoiler from "discourse/plugins/spoiler-alert/lib/apply-spoiler";
function spoil(element) {
element.querySelectorAll(".spoiler").forEach((spoiler) => {
spoiler.classList.remove("spoiler");
spoiler.classList.add("spoiled");
applySpoiler(spoiler);
});
}
export function initializeSpoiler(api) {
api.decorateCookedElement(spoil, { id: "spoiler-alert" });
api.addComposerToolbarPopupMenuOption({
icon: "magic",
label: "spoiler.title",
action: (toolbarEvent) => {
toolbarEvent.applySurround("[spoiler]", "[/spoiler]", "spoiler_text", {
multiline: false,
useBlockMode: true,
});
},
});
addTagDecorateCallback(function () {
const { attributes } = this.element;
if (/\bspoiled\b/.test(attributes.class)) {
this.prefix = "[spoiler]";
this.suffix = "[/spoiler]";
}
});
addBlockDecorateCallback(function (text) {
const { name, attributes } = this.element;
if (name === "div" && /\bspoiled\b/.test(attributes.class)) {
this.prefix = "[spoiler]\n";
this.suffix = "\n[/spoiler]";
return text.trim();
}
});
}
export default {
name: "spoiler-alert",
initialize(container) {
const siteSettings = container.lookup("service:site-settings");
if (siteSettings.spoiler_enabled) {
withPluginApi("1.15.0", initializeSpoiler);
}
},
};