MCPcopy Index your code
hub / github.com/github/docs / main

Function main

script/check-github-github-links.js:49–196  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

47}
48
49async function main() {
50 const searchStrings = ['https://docs.github.com', 'GitHub help_url', 'GitHub developer_help_url']
51
52 const foundFiles = []
53 try {
54 foundFiles.push(...JSON.parse(await fs.readFile('/tmp/foundFiles.json', 'utf-8')))
55 } catch (error) {
56 if (!(error.code && error.code === 'ENOENT')) {
57 throw error
58 }
59 }
60 if (!foundFiles.length || FORCE_DOWNLOAD) {
61 foundFiles.push(...(await getPathsWithMatchingStrings(searchStrings, 'github', 'github')))
62 await fs.writeFile('/tmp/foundFiles.json', JSON.stringify(foundFiles, undefined, 2), 'utf-8')
63 }
64 const searchFiles = [...new Set(foundFiles)] // filters out dupes
65 .filter((file) => endsWithAny(['.rb', '.yml', '.yaml', '.txt', '.pdf', '.erb', '.js'], file))
66 .filter(
67 (file) =>
68 !file.includes('test/') &&
69 !file.includes('app/views/') &&
70 !file.includes('config.') &&
71 !file.includes('app/api/description/')
72 )
73
74 const docsLinksFiles = []
75 const urlRegEx =
76 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g
77
78 try {
79 docsLinksFiles.push(...JSON.parse(await fs.readFile('/tmp/docsLinksFiles.json', 'utf-8')))
80 } catch (error) {
81 if (!(error.code && error.code === 'ENOENT')) {
82 throw error
83 }
84 }
85
86 if (!docsLinksFiles.length || FORCE_DOWNLOAD) {
87 for (const file of searchFiles) {
88 const contents = await getContents('github', 'github', 'master', file)
89
90 if (
91 contents.includes('https://docs.github.com') ||
92 contents.includes('GitHub.help_url') ||
93 contents.includes('GitHub.developer_help_url')
94 ) {
95 const docsIndices = getIndicesOf('https://docs.github.com', contents)
96 const helpIndices = getIndicesOf('GitHub.help_url', contents)
97 helpIndices.push(...getIndicesOf('GitHub.developer_help_url', contents))
98 if (docsIndices.length > 0) {
99 docsIndices.forEach((numIndex) => {
100 // Assuming we don't have links close to 500 characters long
101 const docsLink = contents.substring(numIndex, numIndex + 500).match(urlRegEx)
102 const linkURL = new URL(docsLink[0].toString().replace(/[^a-zA-Z0-9]*$|\\n$/g, ''))
103 const linkPath = linkURL.pathname + linkURL.hash
104 docsLinksFiles.push({ linkPath, file })
105 })
106 }

Callers 1

Calls 5

getContentsFunction · 0.90
endsWithAnyFunction · 0.85
getIndicesOfFunction · 0.85
regexIndexOfFunction · 0.85

Tested by

no test coverage detected