Skip to content

Commit

Permalink
Fix top level relative link checking, added test, added .linkspector.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed May 8, 2024
1 parent 0538423 commit ac5564b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .linkspector.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
- README.md
useGitIgnore: true
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@ If there are no errors, linkspector shows the following message:
- [ ] Experimaental mode to gather all links and check them in batches to study performance gains.
- [ ] Proxy support to connect puppeteer to a remote service.
- [ ] Puppeteer config support.

## Contributing

If you would like to contribute to Linkspector, please read the [contributing guidelines](/CONTRIBUTING.md).
39 changes: 36 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ test("linkspector should check image links in Markdown file", async () => {

expect(hasErrorLinks).toBe(false);
expect(results.length).toBe(1);
expect(results[0].link).toBe("https://commons.wikimedia.org/wiki/Main_Page#/media/File:Praia_do_Ribeiro_do_Cavalo2.jpg");
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");
});

Expand Down Expand Up @@ -76,5 +78,36 @@ test("linkspector should check relative links in Markdown file", async () => {
expect(results[4].status).toBe("alive");
expect(results[5].status).toBe("alive");
expect(results[6].status).toBe("error");
}
);
});

test("linkspector should check top-level relative 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(
"./.linkspector.test.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(false);
expect(results.length).toBe(4);
});
20 changes: 15 additions & 5 deletions lib/check-file-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ function checkFileExistence(link, file) {

try {
let fileDir = path.dirname(file);
let [urlWithoutSection, sectionId] = link.url.split("#");
let filePath = path.resolve(fileDir, urlWithoutSection);
let urlWithoutSection = link.url;
let sectionId = null;

if (link.url.startsWith("#")) {
sectionId = link.url.slice(1);
if (link.url.includes("#")) {
[urlWithoutSection, sectionId] = link.url.split("#");
}

let filePath;

if (urlWithoutSection.startsWith("/")) {
filePath = path.join(process.cwd(), urlWithoutSection);
} else if (urlWithoutSection.startsWith("#") || urlWithoutSection === "") {
sectionId = urlWithoutSection.slice(1);
filePath = file;
} else {
filePath = path.resolve(fileDir, urlWithoutSection);
}

if (fs.existsSync(filePath)) {
Expand Down Expand Up @@ -68,4 +78,4 @@ function checkFileExistence(link, file) {
return { statusCode, status, errorMessage };
}

export { checkFileExistence };
export { checkFileExistence };

0 comments on commit ac5564b

Please sign in to comment.