@@ -2,25 +2,47 @@ package audio
22
33import "regexp"
44
5+ // Pre-compiled regexes for performance (called per stream chunk).
6+ var (
7+ mdFencedCodeRe = regexp .MustCompile ("(?s)```[^`]*```" )
8+ mdInlineCodeRe = regexp .MustCompile ("`([^`]+)`" )
9+ mdBoldStarRe = regexp .MustCompile (`\*\*([^*]+)\*\*` )
10+ mdItalicStarRe = regexp .MustCompile (`\*([^*]+)\*` )
11+ mdBoldUnderRe = regexp .MustCompile (`__([^_]+)__` )
12+ mdItalicUnderRe = regexp .MustCompile (`_([^_]+)_` )
13+ mdLinkRe = regexp .MustCompile (`\[([^\]]+)\]\([^)]+\)` )
14+ mdHeadingRe = regexp .MustCompile (`(?m)^#+\s+` )
15+
16+ ttsTextBlockRe = regexp .MustCompile (`(?s)\[\[tts:text\]\](.*?)\[\[/tts:text\]\]` )
17+ ttsVoiceBlockRe = regexp .MustCompile (`(?s)\[\[tts\]\].*?\[\[/tts\]\]` )
18+ ttsBareTagRe = regexp .MustCompile (`\[\[/?tts(?::[^\]]*)?\]\]` )
19+ )
20+
521// stripMarkdown removes common markdown formatting so TTS reads prose, not
622// syntax characters. Preserves inner text of bold/italic/inline code/links.
723func stripMarkdown (text string ) string {
8- text = regexp . MustCompile ( "(?s)```[^`]*```" ) .ReplaceAllString (text , "" )
9- text = regexp . MustCompile ( "`([^`]+)`" ) .ReplaceAllString (text , "$1" )
10- text = regexp . MustCompile ( `\*\*([^*]+)\*\*` ) .ReplaceAllString (text , "$1" )
11- text = regexp . MustCompile ( `\*([^*]+)\*` ) .ReplaceAllString (text , "$1" )
12- text = regexp . MustCompile ( `__([^_]+)__` ) .ReplaceAllString (text , "$1" )
13- text = regexp . MustCompile ( `_([^_]+)_` ) .ReplaceAllString (text , "$1" )
14- text = regexp . MustCompile ( `\[([^\]]+)\]\([^)]+\)` ) .ReplaceAllString (text , "$1" )
15- text = regexp . MustCompile ( `(?m)^#+\s+` ) .ReplaceAllString (text , "" )
24+ text = mdFencedCodeRe .ReplaceAllString (text , "" )
25+ text = mdInlineCodeRe .ReplaceAllString (text , "$1" )
26+ text = mdBoldStarRe .ReplaceAllString (text , "$1" )
27+ text = mdItalicStarRe .ReplaceAllString (text , "$1" )
28+ text = mdBoldUnderRe .ReplaceAllString (text , "$1" )
29+ text = mdItalicUnderRe .ReplaceAllString (text , "$1" )
30+ text = mdLinkRe .ReplaceAllString (text , "$1" )
31+ text = mdHeadingRe .ReplaceAllString (text , "" )
1632 return text
1733}
1834
19- // stripTtsDirectives removes [[tts...]] markup from text.
20- // `[[tts:text]]...[[/tts:text]]` blocks keep their inner content.
21- // Bare `[[tts]]` and `[[tts:something]]` tags are removed entirely.
22- func stripTtsDirectives (text string ) string {
23- text = regexp .MustCompile (`(?s)\[\[tts:text\]\](.*?)\[\[/tts:text\]\]` ).ReplaceAllString (text , "$1" )
24- text = regexp .MustCompile (`\[\[tts(?::[^\]]*)?\]\]` ).ReplaceAllString (text , "" )
35+ // StripTTSDirectives removes [[tts...]] markup from text.
36+ // `[[tts:text]]...[[/tts:text]]` blocks keep their inner content (voice + text display).
37+ // `[[tts]]...[[/tts]]` blocks are removed entirely including content (voice only, no text).
38+ // Bare `[[tts:something]]` tags without closing are removed.
39+ // Exported for use by channels TTS auto-apply.
40+ func StripTTSDirectives (text string ) string {
41+ // 1. [[tts:text]]...[[/tts:text]] → keep inner content (transcript mode)
42+ text = ttsTextBlockRe .ReplaceAllString (text , "$1" )
43+ // 2. [[tts]]...[[/tts]] → remove entirely including content (voice-only mode)
44+ text = ttsVoiceBlockRe .ReplaceAllString (text , "" )
45+ // 3. Remove any remaining bare/unclosed tags
46+ text = ttsBareTagRe .ReplaceAllString (text , "" )
2547 return text
2648}
0 commit comments