diff --git a/.linkspector.test.yml b/.linkspector.test.yml new file mode 100644 index 0000000..6988365 --- /dev/null +++ b/.linkspector.test.yml @@ -0,0 +1,3 @@ +files: + - README.md +useGitIgnore: true \ No newline at end of file diff --git a/README.md b/README.md index a612885..020ed6e 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file diff --git a/index.test.js b/index.test.js index c5457ae..da8140e 100644 --- a/index.test.js +++ b/index.test.js @@ -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"); }); @@ -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"); -} -); \ No newline at end of file +}); + +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); +}); diff --git a/lib/check-file-links.js b/lib/check-file-links.js index f12c1a8..36e21ea 100644 --- a/lib/check-file-links.js +++ b/lib/check-file-links.js @@ -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)) { @@ -68,4 +78,4 @@ function checkFileExistence(link, file) { return { statusCode, status, errorMessage }; } -export { checkFileExistence }; +export { checkFileExistence }; \ No newline at end of file