From 375a4e089f8250759e2442007973441818c70613 Mon Sep 17 00:00:00 2001 From: Sinclair Chen Date: Mon, 3 Oct 2022 18:25:44 -0700 Subject: [PATCH] Hide spoiler content in emails / notifs (#957) --- common/util/parse.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/common/util/parse.ts b/common/util/parse.ts index 72ceaf15..0bb26454 100644 --- a/common/util/parse.ts +++ b/common/util/parse.ts @@ -24,7 +24,7 @@ import { Mention } from '@tiptap/extension-mention' import Iframe from './tiptap-iframe' import TiptapTweet from './tiptap-tweet-type' import { find } from 'linkifyjs' -import { uniq } from 'lodash' +import { cloneDeep, uniq } from 'lodash' import { TiptapSpoiler } from './tiptap-spoiler' /** get first url in text. like "notion.so " -> "http://notion.so"; "notion" -> null */ @@ -108,5 +108,18 @@ export const exhibitExts = [ ] export function richTextToString(text?: JSONContent) { - return !text ? '' : generateText(text, exhibitExts) + if (!text) return '' + // remove spoiler tags. + const newText = cloneDeep(text) + dfs(newText, (current) => { + if (current.marks?.some((m) => m.type === TiptapSpoiler.name)) { + current.text = '[spoiler]' + } + }) + return generateText(newText, exhibitExts) +} + +const dfs = (data: JSONContent, f: (current: JSONContent) => any) => { + data.content?.forEach((d) => dfs(d, f)) + f(data) }