-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Determine internal vs external links at parse time #884
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,43 +66,36 @@ export class FileBatch { | |
* 2. A list of InternalLink objects to validate. | ||
* 3. A list of ExternalLink objects to validate. | ||
*/ | ||
async load(): Promise<[File[], InternalLink[], ExternalLink[]]> { | ||
async load( | ||
loadExternalLinks: boolean, | ||
): Promise<[File[], InternalLink[], ExternalLink[]]> { | ||
const files: File[] = []; | ||
for (let filePath of this.toLoad) { | ||
const parsed = await parseFile(filePath); | ||
files.push(new File(filePath, parsed.anchors)); | ||
} | ||
|
||
const linksToOriginFiles = new Map<string, string[]>(); | ||
const internalLinksToOriginFiles = new Map<string, string[]>(); | ||
const externalLinksToOriginFiles = new Map<string, string[]>(); | ||
for (const filePath of this.toCheck) { | ||
const parsed = await parseFile(filePath); | ||
files.push(new File(filePath, parsed.anchors)); | ||
if (!IGNORED_FILES.has(filePath)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All three ignores are moved into |
||
addLinksToMap(filePath, parsed.rawLinks, linksToOriginFiles); | ||
} | ||
} | ||
|
||
const internalLinks: InternalLink[] = []; | ||
const externalLinks: ExternalLink[] = []; | ||
for (let [linkPath, originFiles] of linksToOriginFiles) { | ||
if (ALWAYS_IGNORED_URLS.has(linkPath)) { | ||
continue; | ||
} | ||
originFiles = originFiles.filter( | ||
(originFile) => | ||
FILES_TO_IGNORES[originFile] == null || | ||
!FILES_TO_IGNORES[originFile].includes(linkPath), | ||
); | ||
|
||
if (originFiles.length > 0) { | ||
if (linkPath.startsWith("http")) { | ||
externalLinks.push(new ExternalLink(linkPath, originFiles)); | ||
} else { | ||
internalLinks.push(new InternalLink(linkPath, originFiles)); | ||
} | ||
addLinksToMap(filePath, parsed.internalLinks, internalLinksToOriginFiles); | ||
if (loadExternalLinks) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't waste our time storing external links if not necessary. That should slightly decrease memory usage and improve performance. |
||
addLinksToMap( | ||
filePath, | ||
parsed.externalLinks, | ||
externalLinksToOriginFiles, | ||
); | ||
} | ||
} | ||
|
||
const internalLinks = Array.from(internalLinksToOriginFiles.entries()).map( | ||
([link, originFiles]) => new InternalLink(link, originFiles), | ||
); | ||
const externalLinks = Array.from(externalLinksToOriginFiles.entries()).map( | ||
([link, originFiles]) => new ExternalLink(link, originFiles), | ||
); | ||
return [files, internalLinks, externalLinks]; | ||
} | ||
|
||
|
@@ -111,19 +104,21 @@ export class FileBatch { | |
* | ||
* Logs the results to the console and returns `true` if there were no issues. | ||
*/ | ||
async check(externalLinks: boolean, otherFiles: File[]): Promise<boolean> { | ||
async check( | ||
checkExternalLinks: boolean, | ||
otherFiles: File[], | ||
): Promise<boolean> { | ||
console.log(`\n\nChecking links for ${this.description}`); | ||
|
||
const [docsFiles, internalLinkList, externalLinkList] = await this.load(); | ||
const [docsFiles, internalLinkList, externalLinkList] = | ||
await this.load(checkExternalLinks); | ||
const existingFiles = docsFiles.concat(otherFiles); | ||
|
||
const results = internalLinkList.map((link) => link.check(existingFiles)); | ||
|
||
if (externalLinks) { | ||
// For loop reduces the risk of rate-limiting. | ||
for (let link of externalLinkList) { | ||
results.push(await link.check()); | ||
} | ||
// For loop reduces the risk of rate-limiting. | ||
for (let link of externalLinkList) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
results.push(await link.check()); | ||
} | ||
|
||
let allGood = true; | ||
|
@@ -139,10 +134,18 @@ export class FileBatch { | |
|
||
export function addLinksToMap( | ||
filePath: string, | ||
links: string[], | ||
links: Set<string>, | ||
linksToOriginFiles: Map<string, string[]>, | ||
): void { | ||
if (IGNORED_FILES.has(filePath)) return; | ||
links.forEach((link) => { | ||
if ( | ||
ALWAYS_IGNORED_URLS.has(link) || | ||
FILES_TO_IGNORES[filePath]?.includes(link) | ||
) { | ||
return; | ||
} | ||
|
||
const entry = linksToOriginFiles.get(link); | ||
if (entry === undefined) { | ||
linksToOriginFiles.set(link, [filePath]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would include the
objects.inv
files, which was wrong.