-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmarkdown.ts
More file actions
29 lines (28 loc) · 887 Bytes
/
markdown.ts
File metadata and controls
29 lines (28 loc) · 887 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
const MARKDOWN_LINK_RE =
/(?<link>\[.*?]\((?<url>.*?)\)|<img.*?src="(?<url2>.*?)".*?>)/g;
/**
* HTML & Mardown Function to replace relative image paths with absolute paths
*/
export function resolveMarkdownRelativeLinks(
content: string,
cdnBaseURL: string,
) {
return content.replace(
MARKDOWN_LINK_RE,
(match, _, url: string | undefined, url2: string) => {
const path = url || url2;
// If path is already a URL, return the match
if (path.startsWith("http") || path.startsWith("https")) {
return match;
}
// match a link (e.g. [./example](./example), will replace the link, not the text)
if (url) {
return match.replace(
`(${path})`,
`(${cdnBaseURL}/${path.replace(/^\.\//, "")})`,
);
}
return match.replace(path, `${cdnBaseURL}/${path.replace(/^\.\//, "")}`);
},
);
}