Skip to content

Commit dd44b01

Browse files
committed
Finnish initial support for a lot of the remaining target forges
1 parent 014221a commit dd44b01

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ Currently supported git forges:
2626

2727
- [x] GitHub
2828
- [x] BitBucket
29-
- [ ] GitLab
30-
- [ ] ForgeJo (CodeBerg)
31-
- [ ] SourceHut
29+
- [x] GitLab
30+
- [x] gitlab.com
31+
- [x] lab.allmende.io
32+
- [x] gitlab.opensourceecology.de
33+
- [x] ForgeJo
34+
- [x] codeberg.org
35+
- [x] SourceHut
3236
- [ ] Gitea
3337

3438
We have a collection of the [file URLs](forges.md) for the above.

htmlpreview.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,67 @@
4848
*
4949
* NOTE: This function 1st of 2 that is git-forge specific.
5050
*/
51+
// eslint-disable-next-line max-statements
5152
const rawifyForgeUrl = function (forgeFileUrl) {
5253
if (forgeFileUrl === null) {
5354
return null;
5455
}
5556

56-
return forgeFileUrl
57-
.replace(/\/\/github\.com/, '//raw.githubusercontent.com')
58-
.replace(/\/blob\//, '/').replace(/\/raw\//, '/');
57+
const forge = extractForge(forgeFileUrl);
58+
const sw = forge[0];
59+
if (sw === null) {
60+
// do nothing
61+
} else if (sw === FORGE_SOFTWARES.GitHub) {
62+
forgeFileUrl.hostname = 'raw.githubusercontent.com';
63+
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
64+
/^(\/[^/]+\/[^/]+)\/(blob|raw)\/([^/]+\/)/,
65+
'$1/$3'
66+
);
67+
} else if (sw === FORGE_SOFTWARES.BitBucket) {
68+
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
69+
/^(\/[^/]+\/[^/]+)\/src\/([^/]+\/)/,
70+
'$1/raw/$2'
71+
);
72+
} else if (sw === FORGE_SOFTWARES.GitLab) {
73+
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
74+
/^(\/[^/]+\/.+?)\/(-\/)?blob\/([^/]+\/)/,
75+
'$1/-/raw/$3'
76+
);
77+
} else if (sw === FORGE_SOFTWARES.ForgeJo) {
78+
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
79+
/^(\/[^/]+\/[^/]+)\/src\/([^/]+\/)/,
80+
'$1/raw/$2'
81+
);
82+
} else if (sw === FORGE_SOFTWARES.SourceHut) {
83+
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
84+
/^(\/~[^/]+\/[^/]+)\/tree\/([^/]+)\/item\/([^/]+)/,
85+
'$1/blob/$2/$3'
86+
);
87+
} else {
88+
reportError('Unsupported git-forge software: ' + sw.toString());
89+
}
90+
return forgeFileUrl;
91+
};
92+
93+
/**
94+
* Takes any URL to a file on a known git forge,
95+
* and returns the raw version of that files URL on the same forge.
96+
* If it already is the raw version,
97+
* this function just returns it as is.
98+
* @param {string} previewFileUrl - Any URL,
99+
* potentially pointing to a git hosted raw (plain-text) file
100+
* @returns {URL} The raw version of the (git hosted) file URL.
101+
*
102+
* NOTE: This function 1st of 2 that is git-forge specific.
103+
*/
104+
const rawifyForgeUrlStr = function (previewFileUrl) {
105+
let previewFileUrlParsed;
106+
try {
107+
previewFileUrlParsed = new URL(previewFileUrl);
108+
} catch (err) {
109+
reportError('Invalid URL provided in parameter "url"');
110+
}
111+
return rawifyForgeUrl(previewFileUrlParsed);
59112
};
60113

61114
/**
@@ -86,9 +139,9 @@
86139
const previewFileUrl = params.get('url');
87140
if (previewFileUrl === null) {
88141
reportError('Missing required parameter "url"');
142+
// reportError('Please use "...?url=..." vs the old "...?..."');
89143
}
90-
91-
return rawifyForgeUrl(previewFileUrl);
144+
return rawifyForgeUrlStr(previewFileUrl).href;
92145
};
93146

94147
const RE_GITLAB_PATH = /^\/[^/]+\/.+\/(-\/)?(blob|raw)\/[^/]+/;

0 commit comments

Comments
 (0)