Skip to content

Commit

Permalink
Fix suggestion for image link checker (#2163)
Browse files Browse the repository at this point in the history
Closes #2159.

This change also results in more suggestions happening than we had
before, as shown in the tests.
  • Loading branch information
Eric-Arellano authored Oct 22, 2024
1 parent 073f1ce commit bd93f3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
28 changes: 26 additions & 2 deletions scripts/js/lib/links/InternalLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ test.describe("Validate links", () => {
let testFile = new File("docs/testpath.mdx", new Set());
const results = testLink.check([testFile]);
expect(results).toEqual(
"❌ Could not find link '../testpath'. Appears in:\n docs/test1/test2/testorigin.mdx",
"❌ Could not find link '../testpath'. Appears in:\n" +
" docs/test1/test2/testorigin.mdx ❓ Did you mean '/testpath'?",
);
});

Expand Down Expand Up @@ -107,6 +108,27 @@ test.describe("Validate links", () => {
);
});

test("existing image", () => {
let testLink = new InternalLink("/images/my-img.png", [
"docs/test/testorigin.mdx",
]);
let imgFile = new File("public/images/my-img.png", new Set());
const results = testLink.check([imgFile]);
expect(results).toBeUndefined();
});

test("non-existing image", () => {
let testLink = new InternalLink("/images/my-img.png", [
"docs/test/testorigin.mdx",
]);
let imgFile = new File("public/images/another-img.png", new Set());
const results = testLink.check([imgFile]);
expect(results).toEqual(
"❌ Could not find link '/images/my-img.png'. Appears in:\n" +
" docs/test/testorigin.mdx ❓ Did you mean '/images/another-img.png'?",
);
});

test("relative path and multiple origin files", () => {
let testLink = new InternalLink("../testpath", [
"docs/test/testorigin.mdx",
Expand All @@ -120,7 +142,9 @@ test.describe("Validate links", () => {
let testFile2 = new File("docs/test/test2/testpath.mdx", new Set());
const results = testLink.check([testFile1, testFile2]);
expect(results).toEqual(
"❌ Could not find link '../testpath'. Appears in:\n docs/test/test2/testorigin.mdx\n docs/test/test3/testorigin.mdx",
"❌ Could not find link '../testpath'. Appears in:\n" +
" docs/test/test2/testorigin.mdx ❓ Did you mean '/testpath'?\n" +
" docs/test/test3/testorigin.mdx ❓ Did you mean '/testpath'?",
);
});

Expand Down
18 changes: 7 additions & 11 deletions scripts/js/lib/links/InternalLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ export class InternalLink {
let suggestionPath: String = "";
let suggestionPathAnchors: string[] = [];

const possiblePaths = this.possibleFilePaths(originFile);
const pathNoExtension = possiblePaths[0].replace(/\.[^\/.]+$/, "");

existingFiles.forEach((file) => {
let score = levenshtein.get(pathNoExtension, file.path);
const candidatePath = file.path.startsWith("public/")
? file.path.replace(/^public/, "")
: file.path.replace(/\.[^\/.]+$/, "").replace(/^docs/, "");
let score = levenshtein.get(this.value, candidatePath);
if (score < minScoreLink) {
minScoreLink = score;
suggestionPath = file.path;
suggestionPath = candidatePath;
suggestionPathAnchors = Array.from(file.anchors);
}
});
Expand All @@ -137,9 +137,7 @@ export class InternalLink {
}

if (this.anchor == "") {
return `❓ Did you mean '${suggestionPath
.replace(/\.[^\/.]+$/, "")
.replace(/^docs/, "")}'?`;
return `❓ Did you mean '${suggestionPath}'?`;
}

// Find a new valid anchor
Expand All @@ -164,9 +162,7 @@ export class InternalLink {
return null;
}

return `❓ Did you mean '${suggestionPath
.replace(/\.[^\/.]+$/, "")
.replace(/^docs/, "")}${suggestionAnchor}'?`;
return `❓ Did you mean '${suggestionPath}${suggestionAnchor}'?`;
}

/**
Expand Down

0 comments on commit bd93f3a

Please sign in to comment.