|
48 | 48 | *
|
49 | 49 | * NOTE: This function 1st of 2 that is git-forge specific.
|
50 | 50 | */
|
| 51 | + // eslint-disable-next-line max-statements |
51 | 52 | const rawifyForgeUrl = function (forgeFileUrl) {
|
52 | 53 | if (forgeFileUrl === null) {
|
53 | 54 | return null;
|
54 | 55 | }
|
55 | 56 |
|
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); |
59 | 112 | };
|
60 | 113 |
|
61 | 114 | /**
|
|
86 | 139 | const previewFileUrl = params.get('url');
|
87 | 140 | if (previewFileUrl === null) {
|
88 | 141 | reportError('Missing required parameter "url"');
|
| 142 | + // reportError('Please use "...?url=..." vs the old "...?..."'); |
89 | 143 | }
|
90 |
| - |
91 |
| - return rawifyForgeUrl(previewFileUrl); |
| 144 | + return rawifyForgeUrlStr(previewFileUrl).href; |
92 | 145 | };
|
93 | 146 |
|
94 | 147 | const RE_GITLAB_PATH = /^\/[^/]+\/.+\/(-\/)?(blob|raw)\/[^/]+/;
|
|
0 commit comments