diff --git a/lib/shared/markdown.js b/lib/shared/markdown.js --- a/lib/shared/markdown.js +++ b/lib/shared/markdown.js @@ -257,6 +257,45 @@ const stripSpoilersFromNotifications = (text: string): string => text.replace(replaceSpoilerRegex, spoilerReplacement); +const stripSpoilersFromMarkdownAST = ( + ast: SingleASTNode[], +): SingleASTNode[] => { + return ast.map(node => { + if (Array.isArray(node)) { + return { + ...node, + content: stripSpoilersFromMarkdownAST(node), + }; + } + + const { content, items, type } = node; + + if (type === 'spoiler') { + return { + type: 'text', + content: spoilerReplacement, + }; + } + if (content && typeof content === 'string') { + return node; + } + if (items) { + return { + ...node, + items: stripSpoilersFromMarkdownAST(items), + }; + } + if (content) { + return { + ...node, + content: stripSpoilersFromMarkdownAST(content), + }; + } + + return node; + }); +}; + export { paragraphRegex, paragraphStripTrailingNewlineRegex, @@ -278,4 +317,5 @@ parseList, matchMentions, stripSpoilersFromNotifications, + stripSpoilersFromMarkdownAST, }; diff --git a/lib/shared/messages/text-message-spec.js b/lib/shared/messages/text-message-spec.js --- a/lib/shared/messages/text-message-spec.js +++ b/lib/shared/messages/text-message-spec.js @@ -20,6 +20,7 @@ type ASTNode, type SingleASTNode, stripSpoilersFromNotifications, + stripSpoilersFromMarkdownAST, } from '../markdown'; import { threadIsGroupChat } from '../thread-utils'; import { stringForUser } from '../user-utils'; @@ -89,8 +90,9 @@ messageTitle({ messageInfo, markdownRules }) { const { text } = messageInfo; const parser = SimpleMarkdown.parserFor(markdownRules); - const ast = parser(text, { disableAutoBlockNewlines: true }); - + const ast = stripSpoilersFromMarkdownAST( + parser(text, { disableAutoBlockNewlines: true }), + ); return getFirstNonQuotedRawLine(ast).trim(); },