Skip to content

Commit

Permalink
Fixed image links check, added test for verifying image links
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed May 6, 2024
1 parent 8755aed commit 84dc8a3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
44 changes: 44 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect, test } from "vitest";
import { linkspector } from "./linkspector.js";

let cmd = {
json: true,
};

test("linkspector should check image links in Markdown file", async () => {
let hasErrorLinks = false;
let currentFile = ""; // Variable to store the current file name
let results = []; // Array to store the results if json is true

for await (const { file, result } of linkspector(
"./test/fixtures/image/imageTest.yml",
cmd
)) {
currentFile = file;
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
});
}
if (linkStatusObj.status === "error") {
hasErrorLinks = true;
}
}
}

expect(hasErrorLinks).toBe(true);
expect(results.length).toBe(2);
expect(results[0].link).toBe("https://commons.wikimedia.org/wiki/Main_Page#/media/File:Praia_do_Ribeiro_do_Cavalo2.jpg");
expect(results[0].status).toBe("alive");
expect(results[1].link).toBe("https://suygfuysgf6fe76afawe.com/image.jpg");
expect(results[1].status).toBe("error");
});


2 changes: 1 addition & 1 deletion lib/batch-check-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function checkHyperlinks(nodes, options = {}, filePath) {
const tempArray = [];

const filteredNodes = nodes.filter(
(node) => node.type === "link" || node.type === "definition"
(node) => node.type === "link" || node.type === "definition" || node.type === "image"
);

// First pass to check the links with default fetch
Expand Down
2 changes: 1 addition & 1 deletion lib/get-unique-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getUniqueLinks(astNodes) {
for (const node of astNodes) {
// Check if the link starts with "#" or "mailto:" and skip it
if (
node.type === "link" &&
(node.type === "link" || node.type === "definition" || node.type === "image") &&
node.url &&
!uniqueUrls.has(node.url) &&
!node.url.startsWith("#") &&
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/image/image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Check image links

Working link:
![Praia_do_Ribeiro_do_Cavalo2](https://commons.wikimedia.org/wiki/Main_Page#/media/File:Praia_do_Ribeiro_do_Cavalo2.jpg)

Broken link:
![Broken Image](https://suygfuysgf6fe76afawe.com/image.jpg)
2 changes: 2 additions & 0 deletions test/fixtures/image/imageTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dirs:
- ./test/fixtures/image

0 comments on commit 84dc8a3

Please sign in to comment.