Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Chat: only ~~double tilde~~ hides text as a spoiler (#4256)

wlals08748017d860d9b2
diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.ts
index 890f489b17..3d7e2e5443 100644
--- a/src/components/ChatMarkdown.test.ts
+++ b/src/components/ChatMarkdown.test.ts
@@ -68,6 +68,16 @@ describe("soft line breaks", () => {
});
});
+describe("spoilers", () => {
+ it("hides only double-tilde spans, not text between ranges written with a single tilde", () => {
+ const ranges = renderToStaticMarkup(createElement(ChatMarkdown, { text: "Episodes 77~82 first, then 77~80." }));
+ expect(ranges).not.toContain("Reveal spoiler");
+ expect(ranges).toContain("Episodes 77~82 first, then 77~80.");
+ const spoiler = renderToStaticMarkup(createElement(ChatMarkdown, { text: "The answer is ~~42~~." }));
+ expect(spoiler).toContain("Reveal spoiler");
+ });
+});
+
describe("mention highlighting", () => {
const mentionPeers = [{ name: "Atlas" }, { name: "調査担当" }];
it("carries bot colors into Markdown without coloring everyone as a bot", () => {
diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx
index 2c4a914be0..aa7f5610c7 100644
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@ -86,7 +86,9 @@ const gfmParseData: {
micromarkExtensions?: MarkdownParseOptions["extensions"];
fromMarkdownExtensions?: MarkdownParseOptions["mdastExtensions"];
} = {};
-remarkGfm.call({ data: () => gfmParseData });
+// A spoiler is "~~text~~" only: a lone "~" in a range such as "77~82" must not hide what follows it.
+const GFM_OPTIONS = { singleTilde: false };
+remarkGfm.call({ data: () => gfmParseData }, GFM_OPTIONS);
const normalizationParseOptions: MarkdownParseOptions = {
extensions: gfmParseData.micromarkExtensions,
mdastExtensions: [...(gfmParseData.fromMarkdownExtensions ?? []), windowsPathDestinations],
@@ -719,7 +721,7 @@ const NO_THREAD_REFS: ThreadRefsValue = { threads: [] };
// One plugin list for everything every message shares; a message adds mentions only when it has an "@" and thread
// links only when it may name one, so most messages render the same way whatever the bot list holds.
-const BASE_REMARK_PLUGINS: NonNullable<MarkdownOptions["remarkPlugins"]> = [remarkGfm, remarkMath, remarkWindowsPathDestinations, unwrapLinkedImages, remarkSoftBreaks, remarkGithubRefs];
+const BASE_REMARK_PLUGINS: NonNullable<MarkdownOptions["remarkPlugins"]> = [[remarkGfm, GFM_OPTIONS], remarkMath, remarkWindowsPathDestinations, unwrapLinkedImages, remarkSoftBreaks, remarkGithubRefs];
const NO_REHYPE_PLUGINS: NonNullable<MarkdownOptions["rehypePlugins"]> = [];
let katexPlugins: { plugin: KatexPlugin; list: NonNullable<MarkdownOptions["rehypePlugins"]> } | null = null;
const rehypePluginsFor = (math: KatexPlugin | null) => {