|
36 | 36 | 'Git_Sr_Ht' |
37 | 37 | ]); |
38 | 38 |
|
| 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 | + |
39 | 60 | /** |
40 | 61 | * If the first parameter is a URL to a file on a known git forge, |
41 | 62 | * returns the URL to the raw version of this file |
42 | 63 | * (vs the HTML/Web view of it). |
43 | 64 | * @returns {string} The raw version of the (git hosted) file URL |
44 | 65 | * requested to be previewed. |
45 | | - * |
46 | | - * NOTE: This function 1 of 2 that is git-forge specific. |
47 | 66 | */ |
48 | 67 | const getRawFileUrl = function () { |
49 | 68 | if (location.search.length === 0) { |
|
59 | 78 | throw new SyntaxError('Missing required parameter "url"!'); |
60 | 79 | } |
61 | 80 |
|
62 | | - return previewFileUrl |
63 | | - .replace(/\/\/github\.com/, '//raw.githubusercontent.com') |
64 | | - .replace(/\/blob\//, '/').replace(/\/raw\//, '/'); |
| 81 | + return rawifyForgeUrl(previewFileUrl); |
65 | 82 | }; |
66 | 83 |
|
67 | 84 | /** |
|
73 | 90 | * or `(null, null)` if unsupported/unidentified/ |
74 | 91 | * not a git hosted raw file. |
75 | 92 | * |
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. |
77 | 94 | */ |
78 | 95 | // eslint-disable-next-line max-statements |
79 | 96 | const extractForge = function (url) { |
|
0 commit comments