Skip to content

Commit

Permalink
Merge pull request #41 from UmbrellaDocs/relativelinks
Browse files Browse the repository at this point in the history
Fix top level relative link checking, added test, added `.linkspector.yml`
  • Loading branch information
gaurav-nelson authored May 8, 2024
2 parents 0538423 + 8003644 commit 9524b58
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 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 };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umbrelladocs/linkspector",
"version": "0.3.4",
"version": "0.3.5",
"description": "Uncover broken links in your content.",
"type": "module",
"main": "linkspector.js",
Expand Down

0 comments on commit 9524b58

Please sign in to comment.