1export function transformUrl(url: string): string { 2 if (isGitHub(url)) { 3 // Transform https://github.com URLs to https://raw.githubusercontent.com 4 // because GitHub applies DDos protection which prevents us from being 5 // able to pull the contents of the LICENSE file. 6 return rawGithubUrl(url); 7 } 8 return url; 9} 10 11function rawGithubUrl(url: string): string { 12 // Transform URL 13 const ignoreSet = new Set<string>(['https:', 'github.com', 'blob']); 14 const tokens = url.split('/'); 15 const repo = []; 16 const path = []; 17 let pathStarted = false; 18 for (let i = 0; i < tokens.length; i += 1) { 19 if (tokens[i].length <= 0) { 20 continue; 21 } 22 if (tokens[i] === 'blob') { 23 pathStarted = true; 24 } 25 if (ignoreSet.has(tokens[i])) { 26 continue; 27 } 28 if (!pathStarted) { 29 repo.push(tokens[i]); 30 } else { 31 path.push(tokens[i]); 32 } 33 } 34 return `https://raw.githubusercontent.com/${repo.join('/')}/${path.join('/')}`; 35} 36 37function isGitHub(url: string): boolean { 38 return url.startsWith("https://github.com") 39} 40