-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathparser.ts
More file actions
87 lines (74 loc) · 3.15 KB
/
parser.ts
File metadata and controls
87 lines (74 loc) · 3.15 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
export class GithubMediaParser {
replaceInvalidGithubUrls(readme: string, owner: string, repo: string): string {
const wikiPageRegex = new RegExp(
`https://github.com/${owner}/${repo}/wiki/[a-zA-Z0-9./_-]+.(png|jpg|jpeg|mp4|webp|gif)`,
'g'
);
const wikiPagesMatches = readme.matchAll(wikiPageRegex);
for (const wikiPageMatch of wikiPagesMatches) {
const wikiPageLink = wikiPageMatch[0];
const validWikiPageLink = wikiPageLink
.replace('/wiki/', '/')
.replace('github.com', 'raw.githubusercontent.com/wiki');
readme = readme.replaceAll(wikiPageLink, validWikiPageLink);
}
const invalidGithubLinksRegex =
/https:\/\/github.com\/[a-zA-Z0-9./_-]+.(png|jpg|jpeg|mp4|webp|gif)/g;
const invalidGithubLinkMatches = readme.matchAll(invalidGithubLinksRegex);
for (const invalidGithubLinkMatch of invalidGithubLinkMatches) {
const invalidGithubLink = invalidGithubLinkMatch[0];
const validGithubLink = invalidGithubLink
.replace('github.com', 'raw.githubusercontent.com')
.replace('/blob', '');
readme = readme.replaceAll(invalidGithubLink, validGithubLink);
}
return readme;
}
findMediaUrls(readme: string, user: string, repo: string, branch: string): string[] {
const validGithubLinkRegex =
/https:\/\/(raw|user-images).githubusercontent.com\/[a-zA-Z0-9/]+\/[a-zA-Z0-9/._%+-]+.(png|jpg|jpeg|mp4|gif)/g;
const validGithubLinkMatches = readme.matchAll(validGithubLinkRegex);
const allMedia = [];
for (const validGithubLinkMatch of validGithubLinkMatches) {
const media = validGithubLinkMatch[0];
allMedia.push(media);
}
const githubAssetRegex = new RegExp(
`https://github.com/${user}/${repo}/assets/[0-9]+/[a-zA-Z0-9./_-]+`,
'g'
);
for (const validAssetUrlMatches of readme.matchAll(githubAssetRegex)) {
const asset = validAssetUrlMatches[0];
allMedia.push(asset);
}
const storedInRepoRelativeMarkdownRegex = /\(.[/a-zA-Z0-9._%+-]+.(png|jpg|jpeg|mp4|gif|svg)\)/g;
const relativeMarkdownMatches = readme.matchAll(storedInRepoRelativeMarkdownRegex);
for (const relativeMatch of relativeMarkdownMatches) {
if (relativeMatch[0].startsWith('(./')) {
relativeMatch[0] = relativeMatch[0].replace('./', '/');
}
const relativeMedia = relativeMatch[0].replace('(', '').replace(')', '');
const relativeMediaUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}${
relativeMedia.startsWith('/') ? relativeMedia : '/'.concat(relativeMedia)
}`;
allMedia.push(relativeMediaUrl);
}
const storedInRepoRelativeHtmlRegex =
/<img src=['"](\.\/)?[/a-zA-Z._%+-]+.(png|jpg|jpeg|mp4|gif|svg)['"]/g;
const relativeHtmlMatches = readme.matchAll(storedInRepoRelativeHtmlRegex);
for (const relativeMatch of relativeHtmlMatches) {
let relativeMedia = relativeMatch[0]
.replace('<img src=', '')
.replaceAll('"', '')
.replaceAll("'", '');
if (relativeMedia.startsWith('./')) {
relativeMedia = relativeMedia.replace('./', '/');
}
const relativeMediaUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}${
relativeMedia.startsWith('/') ? relativeMedia : '/'.concat(relativeMedia)
}`;
allMedia.push(relativeMediaUrl);
}
return allMedia;
}
}