-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathsplitParagraphSegments.ts
More file actions
87 lines (76 loc) · 2.35 KB
/
splitParagraphSegments.ts
File metadata and controls
87 lines (76 loc) · 2.35 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Matches markdown links and images in a string.
// Group 1 (full link): [text](url) e.g. [Click here](https://example.com)
// Group 2: link text e.g. "Click here"
// Group 3: link url e.g. "https://example.com"
// Group 4 (full image):  e.g. 
// Group 5: alt text e.g. "Logo"
// Group 6: image url e.g. "https://example.com/logo.png"
const linkRegex = /(\[([^\[]+)\]\(([^\)]+)\))|(\!\[([^\[]+)\]\(([^\)]+)\))/g;
/**
* @internal
*/
interface MarkdownSegment {
text: string;
url: string;
type: 'text' | 'link' | 'image';
}
const isValidUrl = (url: string) => {
if (!url) {
return false;
}
// Accept common non-http schemes and relative paths
if (
url.startsWith('data:') ||
url.startsWith('blob:') ||
url.startsWith('/') ||
url.startsWith('./') ||
url.startsWith('../')
) {
return true;
}
try {
const parsed = new URL(url);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch (_) {
return false;
}
};
function pushText(result: MarkdownSegment[], text: string) {
const last = result[result.length - 1];
if (last && last.type === 'text') {
last.text += text;
} else {
result.push({ type: 'text', text, url: '' });
}
}
/**
* @internal
*/
export function splitParagraphSegments(text: string): MarkdownSegment[] {
const result: MarkdownSegment[] = [];
let lastIndex = 0;
let match: RegExpExecArray | null = null;
while ((match = linkRegex.exec(text)) !== null) {
if (match.index > lastIndex) {
pushText(result, text.slice(lastIndex, match.index));
}
if (match[2] && match[3]) {
if (isValidUrl(match[3])) {
result.push({ type: 'link', text: match[2], url: match[3] });
} else {
pushText(result, match[0]);
}
} else if (match[5] && match[6]) {
if (isValidUrl(match[6])) {
result.push({ type: 'image', text: match[5], url: match[6] });
} else {
pushText(result, match[0]);
}
}
lastIndex = linkRegex.lastIndex;
}
if (lastIndex < text.length) {
pushText(result, text.slice(lastIndex));
}
return result;
}