fix(docs): validate translated README website destinations
Use the website language inventory when comparing README links. Accept the matching localized page while retaining checks for wrong locales, changed destinations, fragments and query parameters.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { readFile, readdir, stat } from "node:fs/promises";
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
|
||||
const readmeLinkTargets = (content) =>
|
||||
const readmeLinkTargets = (content, language = "en") =>
|
||||
[...content.matchAll(/\]\(([^)\s]+)\)/gu)]
|
||||
.map((match) => match[1] ?? "")
|
||||
.filter(
|
||||
@@ -9,6 +9,18 @@ const readmeLinkTargets = (content) =>
|
||||
!target.startsWith("#") &&
|
||||
!/^README(?:_[A-Za-z0-9-]+)?\.md$/u.test(target),
|
||||
)
|
||||
.map((target) => {
|
||||
if (language === "en" || !URL.canParse(target)) return target;
|
||||
const url = new URL(target);
|
||||
const prefix = `/${language}/`;
|
||||
if (
|
||||
url.origin !== "https://rea.tools" ||
|
||||
!url.pathname.startsWith(prefix)
|
||||
)
|
||||
return target;
|
||||
url.pathname = url.pathname.slice(prefix.length - 1);
|
||||
return url.href;
|
||||
})
|
||||
.sort();
|
||||
|
||||
const requireText = (issues, path, content, expected) => {
|
||||
@@ -83,10 +95,17 @@ export const documentationFactIssues = async (root, catalog) => {
|
||||
}
|
||||
}
|
||||
const canonicalLinks = readmeLinkTargets(readmes.get("README.md") ?? "");
|
||||
const languageInventory = JSON.parse(
|
||||
await readFile(join(root, "website/locales/languages.json"), "utf8"),
|
||||
);
|
||||
const readmeLanguages = new Map(
|
||||
languageInventory.map(({ readme, code }) => [readme, code]),
|
||||
);
|
||||
for (const path of readmePaths.filter((path) => path !== "README.md"))
|
||||
if (
|
||||
JSON.stringify(readmeLinkTargets(readmes.get(path) ?? "")) !==
|
||||
JSON.stringify(canonicalLinks)
|
||||
JSON.stringify(
|
||||
readmeLinkTargets(readmes.get(path) ?? "", readmeLanguages.get(path)),
|
||||
) !== JSON.stringify(canonicalLinks)
|
||||
)
|
||||
issues.push(`${path}: documentation links differ from README.md`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user