-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanitizeSpeechText.ts
More file actions
32 lines (31 loc) · 1.2 KB
/
sanitizeSpeechText.ts
File metadata and controls
32 lines (31 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const URL_RE = /\b(?:https?:\/\/|www\.)[^\s<>()]+/gi;
const MARKDOWN_LINK_RE = /\[([^\]]+)]\(([^)]+)\)/g;
const MARKDOWN_IMAGE_RE = /!\[([^\]]*)]\(([^)]+)\)/g;
const HTML_TAG_RE = /<[^>]+>/g;
const CODE_FENCE_RE = /```[\s\S]*?```/g;
const INLINE_CODE_RE = /`[^`]*`/g;
const BRACKETED_META_RE = /\[(?:ref|link|url|source|sources?|debug|meta|data):[^\]]*]/gi;
const MD_HEADING_LINE_RE = /^\s{0,3}#{1,6}\s+/gm;
const MD_BLOCKQUOTE_RE = /^\s{0,3}>\s?/gm;
const MD_HR_RE = /^\s{0,3}(-{3,}|\*{3,}|_{3,})\s*$/gm;
const MD_LIST_MARKER_RE = /^\s{0,3}(?:[-*+]|\d+\.)\s+/gm;
const MD_EMPHASIS_RE = /(\*\*|__)(.*?)\1/g;
const MD_ITALIC_RE = /(^|[^\\])(?:\*|_)([^\s].*?)(?:\*|_)/g;
export function sanitizeSpeechText(input: string): string {
return input
.replace(CODE_FENCE_RE, " ")
.replace(MARKDOWN_IMAGE_RE, "$1")
.replace(MARKDOWN_LINK_RE, "$1")
.replace(URL_RE, " ")
.replace(MD_HEADING_LINE_RE, "")
.replace(MD_BLOCKQUOTE_RE, "")
.replace(MD_HR_RE, " ")
.replace(MD_LIST_MARKER_RE, "")
.replace(MD_EMPHASIS_RE, "$2")
.replace(MD_ITALIC_RE, "$1$2")
.replace(HTML_TAG_RE, " ")
.replace(INLINE_CODE_RE, " ")
.replace(BRACKETED_META_RE, " ")
.replace(/\s+/g, " ")
.trim();
}