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 support for directory indexes #71

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Asynchronous method that runs a site wide scan. Options come in the form of an o
- `path` (string) - A fully qualified path to the url to be scanned, or the path to the directory on disk that contains files to be scanned. *required*.
- `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.
- `directoryIndexes` (boolean) - If your site uses directory indexes, this setting avoids false-positives on relative links that would be correctly resolved by a server, but fail a `GET`/`HEAD` request.
- `linksToSkip` (array) - An array of regular expression strings that should be skipped during the scan.

#### linkinator.LinkChecker()
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CheckOptions {
port?: number;
path: string;
recurse?: boolean;
directoryIndexes?: boolean;
linksToSkip?: string[];
}

Expand Down Expand Up @@ -100,6 +101,11 @@ export class LinkChecker extends EventEmitter {
* @returns A list of crawl results consisting of urls and status codes
*/
private async crawl(opts: CrawlOptions): Promise<LinkResult[]> {
// Support directory indexes by adding a trailing slash if required
if (opts.checkOptions.directoryIndexes && !opts.url.match(/\/$|\./)) {
opts.url += '/';
}

// Check to see if we've already scanned this url
if (opts.cache.has(opts.url)) {
return opts.results;
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/relative/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<body>
<a href="ohai.html">go right over there</a>
<a href="nested/index.html">go right over there</a>
<a href="nested">go right over there</a>
<a href="nested/">go right over there</a>
</body>
</html>
3 changes: 2 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ describe('linkinator', () => {
const results = await check({
path: 'test/fixtures/relative',
recurse: true,
directoryIndexes: true,
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 4);
assert.strictEqual(results.links.length, 5);
});

it('should handle fetch exceptions', async () => {
Expand Down