-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
26 lines (22 loc) · 856 Bytes
/
utils.ts
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
export function formatLinkList(input: string): string {
// Regex to match markdown-style links
const linkRegex = /\[([^\]]*)\]\(([^)]+)\)/g;
// Find all matches
const links = [...input.matchAll(linkRegex)];
if (links.length === 0) return '';
// Create a unique set of links to remove duplicates
const uniqueLinks = Array.from(new Set(links.map(match => match[2])))
.map(url => {
// Find the first link with this URL
const matchingLink = links.find(match => match[2] === url);
return matchingLink || [null, url, url];
});
// Format links into a markdown list
return uniqueLinks
.map(match => {
const title = match[1] || match[2];
const url = match[2];
return `- [${title}](${url})`;
})
.join('\n');
}