Skip to content

Commit 20b6635

Browse files
committed
Factors out raification into a separate function
1 parent c326915 commit 20b6635

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

htmlpreview.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,33 @@
3636
'Git_Sr_Ht'
3737
]);
3838

39+
/**
40+
* Takes any URL to a file on a known git forge,
41+
* and returns the raw version of that files URL on the same forge.
42+
* If it already is the raw version,
43+
* this function just returns it as is.
44+
* @param {URL} forgeFileUrl - Any URL,
45+
* potentially pointing to a git hosted raw (plain-text) file
46+
* @returns {URL} The raw version of the (git hosted) file URL.
47+
*
48+
* NOTE: This function 1st of 2 that is git-forge specific.
49+
*/
50+
const rawifyForgeUrl = function (forgeFileUrl) {
51+
if (forgeFileUrl === null) {
52+
return null;
53+
}
54+
55+
return forgeFileUrl
56+
.replace(/\/\/github\.com/, '//raw.githubusercontent.com')
57+
.replace(/\/blob\//, '/').replace(/\/raw\//, '/');
58+
};
59+
3960
/**
4061
* If the first parameter is a URL to a file on a known git forge,
4162
* returns the URL to the raw version of this file
4263
* (vs the HTML/Web view of it).
4364
* @returns {string} The raw version of the (git hosted) file URL
4465
* requested to be previewed.
45-
*
46-
* NOTE: This function 1 of 2 that is git-forge specific.
4766
*/
4867
const getRawFileUrl = function () {
4968
if (location.search.length === 0) {
@@ -59,9 +78,7 @@
5978
throw new SyntaxError('Missing required parameter "url"!');
6079
}
6180

62-
return previewFileUrl
63-
.replace(/\/\/github\.com/, '//raw.githubusercontent.com')
64-
.replace(/\/blob\//, '/').replace(/\/raw\//, '/');
81+
return rawifyForgeUrl(previewFileUrl);
6582
};
6683

6784
/**
@@ -73,7 +90,7 @@
7390
* or `(null, null)` if unsupported/unidentified/
7491
* not a git hosted raw file.
7592
*
76-
* NOTE: This is function 2 of 2 that is git-forge specific.
93+
* NOTE: This is function 2nd of 2 that is git-forge specific.
7794
*/
7895
// eslint-disable-next-line max-statements
7996
const extractForge = function (url) {

0 commit comments

Comments
 (0)