-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext-utils.ts
More file actions
43 lines (34 loc) · 976 Bytes
/
text-utils.ts
File metadata and controls
43 lines (34 loc) · 976 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
export function sanitizeFileName(name: string) {
return name
.replace(/[\\/:*?"<>|]/g, '')
.replace(/\s+/g, ' ')
.trim();
}
export function encodePathForMarkdown(path: string) {
return encodeURI(path);
}
export function extractCodeLanguage(className: string) {
const patterns = [
/language-([\w-]+)/i,
/lang(?:uage)?-([\w-]+)/i,
/\b([\w-]+)-language\b/i,
];
for (const pattern of patterns) {
const match = className.match(pattern);
if (match?.[1]) {
return match[1];
}
}
const tokens = className
.split(/\s+/)
.map((token) => token.trim())
.filter(Boolean);
if (tokens.length === 0) return '';
const ignore = new Set(['code', 'highlight', 'hljs', 'language', 'lang']);
for (const token of tokens) {
if (!ignore.has(token.toLowerCase())) {
return token;
}
}
return tokens[0] ?? '';
}