From d87584e7ad21760ae540c76687016a49c78435c7 Mon Sep 17 00:00:00 2001 From: denispol <19826856+denispol@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:52:02 +0100 Subject: [PATCH 1/2] refactor(utils): enhance Markdown text cleaning for TTS compatibility --- src/lib/utils/index.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 784eef434..a64ff5d4f 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -552,7 +552,31 @@ export const removeEmojis = (str: string) => { }; export const removeFormattings = (str: string) => { - return str.replace(/(\*)(.*?)\1/g, '').replace(/(```)(.*?)\1/gs, ''); + return str + // Block elements (remove completely) + .replace(/(```[\s\S]*?```)/g, '') // Code blocks + .replace(/^\|.*\|$/gm, '') // Tables}; + // Inline elements (preserve content) + .replace(/(?:\*\*|__)(.*?)(?:\*\*|__)/g, '$1') // Bold + .replace(/(?:[*_])(.*?)(?:[*_])/g, '$1') // Italic + .replace(/~~(.*?)~~/g, '$1') // Strikethrough + .replace(/`([^`]+)`/g, '$1') // Inline code + + // Links and images + .replace(/!?\[([^\]]*)\](?:\([^)]+\)|\[[^\]]*\])/g, '$1') // Links & images + .replace(/^\[[^\]]+\]:\s*.*$/gm, '') // Reference definitions + + // Block formatting + .replace(/^#{1,6}\s+/gm, '') // Headers + .replace(/^\s*[-*+]\s+/gm, '') // Lists + .replace(/^\s*(?:\d+\.)\s+/gm, '') // Numbered lists + .replace(/^\s*>[> ]*/gm, '') // Blockquotes + .replace(/^\s*:\s+/gm, '') // Definition lists + + // Cleanup + .replace(/\[\^[^\]]*\]/g, '') // Footnotes + .replace(/[-*_~]/g, '') // Remaining markers + .replace(/\n{2,}/g, '\n') // Multiple newlines }; export const cleanText = (content: string) => { From e6add2869bc0da0836d6337462e19a2f9a1555e9 Mon Sep 17 00:00:00 2001 From: denispol <19826856+denispol@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:56:12 +0100 Subject: [PATCH 2/2] Update index.ts --- src/lib/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index a64ff5d4f..6dbd90bb9 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -555,8 +555,8 @@ export const removeFormattings = (str: string) => { return str // Block elements (remove completely) .replace(/(```[\s\S]*?```)/g, '') // Code blocks - .replace(/^\|.*\|$/gm, '') // Tables}; - // Inline elements (preserve content) + .replace(/^\|.*\|$/gm, '') // Tables + // Inline elements (preserve content) .replace(/(?:\*\*|__)(.*?)(?:\*\*|__)/g, '$1') // Bold .replace(/(?:[*_])(.*?)(?:[*_])/g, '$1') // Italic .replace(/~~(.*?)~~/g, '$1') // Strikethrough