Open platform to collect and prioritize feedback
fix(ui): Strip markdown from auto-generated title Auto-generated titles would preserve markdown syntax and html characters This strips markdown and html from the post body when updating the title
widal00140870e88ecad
diff --git a/public/components/common/form/CommentEditor.tsx b/public/components/common/form/CommentEditor.tsxindex 64c9c0fd8..b3394ac37 100644--- a/public/components/common/form/CommentEditor.tsx+++ b/public/components/common/form/CommentEditor.tsx@@ -10,6 +10,7 @@ import Paragraph from "@tiptap/extension-paragraph"import Text from "@tiptap/extension-text"import HardBreak from "@tiptap/extension-hard-break"import { i18n } from "@lingui/core"+import { plainText } from "@fider/services/markdown"import "./CommentEditor.scss"@@ -313,11 +314,11 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {const updated = ({ editor }: { editor: Editor; transaction: any }): void => {// Get the current markdown contentconst markdown = isRawMarkdownMode ? editor.getText() : editor.storage.markdown.getMarkdown()- // Always get plain text regardless of mode- const plainText = editor.getText()+ // Get plain text by processing the markdown content to strip markdown syntax+ const plainTextContent = plainText(editor.getText())// Pass both markdown and plain text to parent- props.onChange && props.onChange(markdown, plainText)+ props.onChange && props.onChange(markdown, plainTextContent)// Track the current state of images in the document// This will also check for removed imagesdiff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.tsindex f033c5bac..9bba59524 100644--- a/public/services/markdown.spec.ts+++ b/public/services/markdown.spec.ts@@ -66,6 +66,11 @@ How are you?`,'<p><a href="monero:83zJ2jMbBoxJkhtpaRLk6fQVrvfmGbd8gYUL7FSdLxU91JSpiWXoLUtAMGqmfvfq3qRS5gJUvMY7oLFSx71wxhKRGG6ypMt" target="_blank" rel="noopener nofollow" class="text-link">monero</a> <a href="bitcoin:1CgLs6CxXMAY4Pj4edQq5vyaFoP9NdqVKH" target="_blank" rel="noopener nofollow" class="text-link">bitcoin</a> <a target="_blank" rel="noopener nofollow" class="text-link">litecoin</a></p>',expectedPlainText: "monero bitcoin litecoin",},+ {+ input: "Jane's & Jim's > [Matt](https://example.com)",+ expectedFull: '<p>Jane\'s & Jim\'s > <a target="_blank" rel="noopener nofollow" href="https://example.com" class="text-link">Matt</a></p>',+ expectedPlainText: "Jane's & Jim's > Matt",+ },]testCases.forEach((x) => {diff --git a/public/services/markdown.ts b/public/services/markdown.tsindex 69dd87cd5..bfb6bd181 100644--- a/public/services/markdown.ts+++ b/public/services/markdown.ts@@ -78,11 +78,26 @@ const entities: { [key: string]: string } = {const encodeHTML = (s: string) => s.replace(/[<>]/g, (tag) => entities[tag] || tag)const sanitize = (input: string) => (DOMPurify.isSupported ? DOMPurify.sanitize(input) : input)+// Helper function to decode HTML entities back to readable characters+const decodeHtmlEntities = (text: string): string => {+ return text.replace(/&[#\w]+;/g, (entity) => {+ const entities: { [key: string]: string } = {+ "'": "'",+ """: '"',+ "&": "&",+ "<": "<",+ ">": ">",+ " ": " ",+ }+ return entities[entity] || entity+ })+}export const full = (input: string): string => {return sanitize(marked(encodeHTML(input), { renderer: fullRenderer }).trim())}export const plainText = (input: string): string => {- return sanitize(marked(encodeHTML(input), { renderer: plainTextRenderer }).trim())+ const text = sanitize(marked(input, { renderer: plainTextRenderer }).trim())+ return decodeHtmlEntities(text).trim()}