Skip to content

Commit

Permalink
feat: add filterLinks function
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke committed Nov 19, 2019
1 parent d653f2c commit 99c99a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Asynchronous method that runs a site wide scan. Options come in the form of an o
- `port` (number) - When the `path` is provided as a local path on disk, the `port` on which to start the temporary web server. Defaults to a random high range order port.
- `recurse` (boolean) - By default, all scans are shallow. Only the top level links on the requested page will be scanned. By setting `recurse` to `true`, the crawler will follow all links on the page, and continue scanning links **on the same domain** for as long as it can go. Results are cached, so no worries about loops.
- `linksToSkip` (array) - An array of regular expression strings that should be skipped during the scan.
- `filterLinks` (function) - A function that's called for each link with the link URL as its only argument. Return `true` to skip the link or `false` to check it.

#### linkinator.LinkChecker()
Constructor method that can be used to create a new `LinkChecker` instance. This is particularly useful if you want to receive events as the crawler crawls. Exposes the following events:
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CheckOptions {
path: string;
recurse?: boolean;
linksToSkip?: string[];
filterLink?: (arg0: string) => boolean;
}

export enum LinkState {
Expand Down Expand Up @@ -144,7 +145,7 @@ export class LinkChecker extends EventEmitter {
})
.filter(match => !!match);

if (skips.length > 0) {
if (skips.length > 0 || (opts.checkOptions.filterLink && opts.checkOptions.filterLink(opts.url.href))) {
const result: LinkResult = {
url: opts.url.href,
state: LinkState.SKIPPED,
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/filter/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<a href="https://good.com">I'm good</a>
<a href="http://www.filterme.com">I should be filtered</a>
<a href="https://example.com/filtermetoo">I should also be filtered</a>
</body>
</html>
18 changes: 18 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ describe('linkinator', () => {
);
});

it('should skip links if passed a filterLink function', async () => {
const scope = nock('https://good.com')
.head('/')
.reply(200);
const results = await check({
path: 'test/fixtures/filter',
filterLink: (href) => {
return href.includes('filterme')
}
});
assert.ok(results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.SKIPPED).length,
2
);
scope.done();
});

it('should report broken links', async () => {
const scope = nock('http://fake.local')
.head('/')
Expand Down

0 comments on commit 99c99a1

Please sign in to comment.