Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore list for links #21

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ jobs:
echo "Tests from code:"
npm test
echo "Tests from file with TARGET_FILE_PATH env variable:"
TARGET_FILE_PATH='./README.md' node index.js
TARGET_FILE_PATH='./README.md' npm test
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ launch the command with `TARGET_FILE_PATH` environment variable:
TARGET_FILE_PATH='./README.md' node node_modules/.bin/img-link-checker
```

> Note: if you need to ignore some of the links - you can specify
> an optional second argument with an array of links that should
> not be checked:
> ```
> const { checkFile } = require('img-link-checker');
>
> checkFile('./README.md', ['https://www.linkedin.com/in/test/']);
> ```

# Thanks
If this link and image checking tool was helpful to you, please give it a **★ Star**
on [GitHub](https://github.com/Marketionist/img-link-checker).
32 changes: 18 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ const { promisify } = require('util');
const fs = require('fs');
const axios = require('axios');

const checkFile = async (filePath) => {
const checkFile = async (filePath, ignoreLinks = []) => {
const pathToFile = process.env.TARGET_FILE_PATH || filePath;
const readFile = promisify(fs.readFile);
const text = await readFile(filePath, 'utf8');
const text = await readFile(pathToFile, 'utf8');

// Filter out only links that start from http(s) and end with ) or " or '
// Filter out only links that start with http(s) and end with ) or " or '
// Using negative lookahead ?! to filter out localhost:
const links = text.match(/https?:\/\/(?!.*(localhost:)).*?[\)|"|']/gi);

if (links) {
// Clear links from last character ) or " or '
const clearedLinks = links.map((value) => value.slice(0, -1));
let filteredLinks = links.map((value) => value.slice(0, -1));

console.log(`Found ${clearedLinks.length} link(s):`);
console.log(clearedLinks);
// Remove the ignored links if any
if (ignoreLinks.length > 0) {
filteredLinks = filteredLinks.filter((value) => {
return !ignoreLinks.includes(value);
});
}

console.log(`Found ${filteredLinks.length} link(s):`);
console.log(filteredLinks);

const responses = await Promise.all(clearedLinks.map(async (value) => {
const responses = await Promise.all(filteredLinks.map(async (value) => {
try {
const response = await axios.get(value);

Expand All @@ -31,25 +39,21 @@ const checkFile = async (filePath) => {
const statusCodeOk = 200;

responses.map((value, index) => {
if (value !== statusCodeOk) { brokenLinks.push(clearedLinks[index]); }
if (value !== statusCodeOk) { brokenLinks.push(filteredLinks[index]); }
});

if (brokenLinks.length > 0) {
console.error('Broken links list:', brokenLinks);
process.exit(1);
} else {
return clearedLinks;
return filteredLinks;
}
} else {
console.log(`No links found in ${filePath}`);
console.log(`No links found in ${pathToFile}`);
}

};

if (process.env.TARGET_FILE_PATH) {
checkFile(process.env.TARGET_FILE_PATH);
}

module.exports = {
checkFile
};
8 changes: 7 additions & 1 deletion tests/links.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const { checkFile } = require('../index.js');

checkFile('./README.md');
checkFile(
'./README.md',
[
'https://github.com/Marketionist/img-link-checker/actions',
'https://www.linkedin.com/in/test/'
]
);