Skip to content

Commit

Permalink
Merge pull request #39 from UmbrellaDocs/dev-0.3.3
Browse files Browse the repository at this point in the history
Dev 0.3.3
  • Loading branch information
gaurav-nelson authored May 6, 2024
2 parents c35eb55 + 1bf0a40 commit 9dc2d13
Show file tree
Hide file tree
Showing 14 changed files with 4,517 additions and 1,956 deletions.
1 change: 0 additions & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm install && rm -rf node_modules
- run: npm ci
- run: npm publish --access public
env:
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Run tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm test
17 changes: 4 additions & 13 deletions DEV_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ To contribute to this project, you'll need to set up your development environmen

Before you begin, make sure you have the following prerequisites installed on your system:

1. **Node.js**: This project requires Node.js, a JavaScript runtime, to build and run. You can download and install Node.js from the official website: [Node.js Download](https://nodejs.org/).
* **Node.js**: This project requires Node.js, a JavaScript runtime, to build and run. You can download and install Node.js from the official website: [Node.js Download](https://nodejs.org/).

To check if Node.js is installed, open your terminal and run:

Expand All @@ -16,18 +16,9 @@ Before you begin, make sure you have the following prerequisites installed on yo

You should see the installed Node.js version.

2. **pnpm**: Although `npm` is included with Node.js and is used to manage project dependencies. In this project, we are using `pnpm` as a package manager. See [pnpm Installation](https://pnpm.io/installation) for more information on installing `pnpm`.
To check if `pnpm` is installed, open your terminal and run:

```bash
bun -v
```

Make sure `pnpm` is up to date.

## Installation

After ensuring you have Node.js and `pnpm` installed, follow these steps to set up your development environment:
After ensuring you have Node.js installed, follow these steps to set up your development environment:

1. **Clone the Repository**: Fork and clone this repository to your local machine:

Expand All @@ -41,10 +32,10 @@ After ensuring you have Node.js and `pnpm` installed, follow these steps to set
cd linkspector
```

3. **Install Dependencies**: Use `pnpm` to install project dependencies:
3. **Install Dependencies**: Use `npm` to install project dependencies:

```bash
pnpm install
npm install
```

This command will download and install all the required packages specified in the `package.json` file.
Expand Down
80 changes: 80 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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(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].status).toBe("alive");
});

test("linkspector should check 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(
"./test/fixtures/relative/.relativeTest.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(7);
expect(results[0].status).toBe("alive");
expect(results[1].status).toBe("alive");
expect(results[2].status).toBe("alive");
expect(results[3].status).toBe("alive");
expect(results[4].status).toBe("alive");
expect(results[5].status).toBe("alive");
expect(results[6].status).toBe("error");
}
);
10 changes: 7 additions & 3 deletions lib/batch-check-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ 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
for (let link of filteredNodes) {
try {
if (isUrl(link.url)) {
const response = await fetch(link.url);
const response = await fetch(link.url, {
method: "HEAD",
redirect: "follow",
});
let message = response.redirected ? `redirected to ${response.url}` : null;
const statusCode = response.status;

if (response.ok) {
const linkStatus = createLinkStatus(link, "alive", statusCode);
const linkStatus = createLinkStatus(link, "alive", statusCode, message);
linkStatusList.push(linkStatus);
continue;
} else if (aliveStatusCodes && aliveStatusCodes.includes(statusCode)) {
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
Loading

0 comments on commit 9dc2d13

Please # to comment.