diff --git a/.changeset/msc4454.md b/.changeset/msc4454.md new file mode 100644 index 000000000..af548e99e --- /dev/null +++ b/.changeset/msc4454.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +spoilered text now gets replaced with `[Spoiler]` in the plain text fallback, as per MSC4454 diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 383678e1a..4b6cab231 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -196,6 +196,8 @@ const elementToPlainText = (node: CustomElement, children: string): string => { } }; +const SPOILERINPUTREGEX = /\|\|.+?\|\|/g; + /** * convert slate internal representation to a plain text string that can be sent to the server * @param node the slate node @@ -213,8 +215,9 @@ export const toPlainText = ( if (Array.isArray(node)) return node.map((n) => toPlainText(n, isMarkdown, stripNickname, nickNameReplacement)).join(''); if (Text.isText(node)) { + let { text } = node; + text = text.replaceAll(SPOILERINPUTREGEX, '[Spoiler]'); if (stripNickname && nickNameReplacement) { - let { text } = node; nickNameReplacement?.keys().forEach((key) => { const replacement = nickNameReplacement.get(key) ?? ''; text = text.replaceAll(key, replacement); @@ -224,8 +227,8 @@ export const toPlainText = ( : text; } return isMarkdown - ? unescapeMarkdownBlockSequences(node.text, unescapeMarkdownInlineSequences) - : node.text; + ? unescapeMarkdownBlockSequences(text, unescapeMarkdownInlineSequences) + : text; } const children = node.children.map((n) => toPlainText(n, isMarkdown)).join('');