Skip to content

Commit a853fd1

Browse files
committed
chore: improve Javadoc link verification
1 parent a12f735 commit a853fd1

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/utils/remark/javadoc.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,31 @@ const asUrl = (name: string): string => {
2121
return `${name0}.html` + (hash ? `#${hash}` : "");
2222
};
2323

24+
const error = (err: any): never => {
25+
if (process.env.NODE_ENV === "production") {
26+
console.error(err);
27+
28+
// throwing an error does not exit the build process, it silently fails
29+
// we don't want missing pages, so exit the process instead
30+
process.exit(1);
31+
} else {
32+
throw err;
33+
}
34+
};
35+
2436
const parse = async (url: string, { targets }: Options): Promise<string | null> => {
2537
const match = /^jd:(.+?)(?::(.+?))?(?::(.+?))?$/.exec(url);
2638
if (!match) {
27-
return null;
39+
if (url.startsWith("jd:")) {
40+
error(new Error(`Failed to parse Javadoc link "${url}"`));
41+
}
42+
43+
return null; // not a Javadoc link
2844
}
2945

3046
const target = targets[match[1]];
3147
if (!target) {
32-
return null;
48+
error(new Error(`Unknown target for Javadoc link "${url}"`));
3349
}
3450

3551
const targetUrl = typeof target !== "string" ? target.url : target;
@@ -40,25 +56,15 @@ const parse = async (url: string, { targets }: Options): Promise<string | null>
4056
}
4157

4258
const module = match[3] ? match[2] : typeof target !== "string" ? target.module : undefined;
43-
44-
const parsed: string = `${targetUrl}/${module ? `${module}/` : ""}${asUrl(name)}`;
59+
const parsed = `${targetUrl}/${module ? `${module}/` : ""}${asUrl(name)}`;
4560

4661
const result = await deadOrAlive(parsed, {
4762
findUrls: false,
4863
followMetaHttpEquiv: false,
4964
userAgent: "PaperMC/docs (https://docs.papermc.io)",
5065
});
5166
if (result.status !== "alive") {
52-
const error = new Error(`Javadoc link "${url}" is not valid`);
53-
if (process.env.NODE_ENV === "production") {
54-
console.error(error);
55-
56-
// throwing an error does not exit the build process, it silently fails
57-
// we don't want missing pages, so exit the process instead
58-
process.exit(1);
59-
} else {
60-
throw error;
61-
}
67+
error(new Error(`Received dead status for Javadoc link "${url}"`));
6268
}
6369

6470
return parsed;

0 commit comments

Comments
 (0)