Skip to content
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

fix(ci): don't report a broken fragment if duplicate fragment exists #35577

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions scripts/log-url-issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* - Markdown header updates
*/

import fs from "node:fs/promises";
import fs from "node:fs";
import {
execGit,
getRootDir,
Expand All @@ -14,11 +14,37 @@ import {
stringToFragment,
} from "./utils.js";

const rootDir = getRootDir();
const fileCache = new Map();
const anchorCache = new Map();
const deletedSlugs = [];
const addedFragmentDetails = [];
let deletedFragmentDetails = [];
let isAllOk = true;

function getFileContent(path) {
if (fileCache.has(path)) {
return fileCache.get(path);
}

const content = fs.readFileSync(path, "utf-8");
fileCache.set(path, content);
return content;
}

function getFileAnchors(path) {
if (anchorCache.has(path)) {
return anchorCache.get(path);
}
const content = getFileContent(path);
const headerAnchors = [...content.matchAll(/^#+ .*?$/gm)]
.map((match) => match[0].toLowerCase())
.map((h) => h.replace(/#+ /g, ""))
.map((h) => stringToFragment(h));
anchorCache.set(path, headerAnchors);
return headerAnchors;
}

function getDeletedSlugs(fromStaging = true) {
let result = "";

Expand Down Expand Up @@ -61,8 +87,6 @@ function getDeletedSlugs(fromStaging = true) {
);
}
}

console.log("deletedSlugs", deletedSlugs);
}

function getFragmentDetails(fromStaging = true) {
Expand Down Expand Up @@ -97,15 +121,25 @@ function getFragmentDetails(fromStaging = true) {
.map((header) => header.replace(/-#+ /g, ""))
.map((header) => stringToFragment(header))
.filter((header) => !addedFragments.includes(header))
.forEach((header) => deletedFragmentDetails.push(`${path}#${header}`));
.filter((header) => {
// check if another header with same name exists in the file
const headerAnchors = getFileAnchors(
`${rootDir}/files/en-us/${path}/index.md`,
);
return !headerAnchors.includes(header);
})
.forEach((header) => {
const fragment = `${path}#${header}`;
if (!deletedFragmentDetails.includes(fragment)) {
deletedFragmentDetails.push(fragment);
}
});

addedFragments.forEach((header) =>
addedFragmentDetails.push(`${path}#${header}`),
);
}
}

console.log("deletedFragmentDetails", deletedFragmentDetails);
}

if (process.argv[2] !== "--workflow") {
Expand All @@ -125,10 +159,14 @@ if (deletedSlugs.length < 1 && deletedFragmentDetails.length < 1) {
process.exit(0);
}

for await (const filePath of walkSync(getRootDir())) {
console.log("deletedSlugs", deletedSlugs);
console.log("deletedFragmentDetails", deletedFragmentDetails);

for await (const filePath of walkSync(rootDir)) {
if (filePath.endsWith("index.md")) {
try {
const content = await fs.readFile(filePath, "utf-8");
const content =
fileCache.get(filePath) ?? fs.readFileSync(filePath, "utf-8");
const relativePath = filePath.substring(filePath.indexOf("files/en-us"));

// check deleted links
Expand Down
1 change: 1 addition & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function getLocations(content, searchValue) {
* Convert Markdown header into URL slug.
*/
export function stringToFragment(text) {
if (!text) return text;
return text
.trim()
.replace(/["#$%&+,/:;=?@[\]^`{|}~')(\\]/g, "")
Expand Down
Loading