Skip to content

Commit

Permalink
feat: Added skipLocalhost option (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrtovmach authored and David Clark committed Oct 26, 2019
1 parent 0c25d41 commit 0f37396
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ All options are optional. The options object may contain any of the following pr
- **skipOffline** `{boolean}` - Default: `false`.
By default, if you are offline when you run the check you will receive a warning.
If you want to let offline runs quietly pass, set this option to `true`.
- **skipLocalhost** `{boolean}` - Default: `false`.
By default, `localhost` links are treated the same as other links, so if your project is not running locally you'll receive a warning.
If you want to ignore `localhost` links (e.g. `http://localhost/*`, `http://127.0.0.1/*`), set this option to `true`.
- **gotOptions** `{Object}` - Passed through [check-links] to [Got]. See documentation for [Got options](https://github.com/sindresorhus/got#options). With these options, you can customize retry logic, specify custom headers, and more. Here are some specific Got options that you might want to use:
- **gotOptions.baseUrl** `{string}` - Used as the base URL against which relative URLs are checked.
By default, relative URLs are ignored: you must provide this option to check them.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function noDeadUrls(ast, file, options) {
const aggregate = (node) => {
const url = node.url;
if (!url) return;
if (
options.skipLocalhost &&
/^(https?:\/\/)(localhost|127\.0\.0\.1)(:\d+)?/.test(url)
)
return;

if (!urlToNodes[url]) {
urlToNodes[url] = [];
Expand Down
62 changes: 59 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ describe('remark-lint-no-dead-urls', () => {
Here is a [good link](https://www.github.com).
Here is a [bad link](https://github.com/unified/oops).
Here is a [local link](http://localhost:3000).
`,
{
gotOptions: {
Expand All @@ -55,21 +57,29 @@ describe('remark-lint-no-dead-urls', () => {
checkLinks.mockReturnValue(
Promise.resolve({
'https://www.github.com': { status: 'alive', statusCode: 200 },
'https://github.com/unified/oops': { status: 'dead', statusCode: 404 }
'https://github.com/unified/oops': {
status: 'dead',
statusCode: 404
},
'http://localhost:3000': { status: 'dead', statusCode: 404 }
})
);

return lint.then((vFile) => {
expect(checkLinks).toHaveBeenCalledTimes(1);
expect(checkLinks.mock.calls[0][0]).toEqual([
'https://www.github.com',
'https://github.com/unified/oops'
'https://github.com/unified/oops',
'http://localhost:3000'
]);

expect(vFile.messages.length).toBe(1);
expect(vFile.messages.length).toBe(2);
expect(vFile.messages[0].reason).toBe(
'Link to https://github.com/unified/oops is dead'
);
expect(vFile.messages[1].reason).toBe(
'Link to http://localhost:3000 is dead'
);
});
},
15000
Expand Down Expand Up @@ -164,4 +174,50 @@ describe('remark-lint-no-dead-urls', () => {
expect(vFile.messages.length).toBe(0);
});
});

describe('skipLocalhost: true', () => {
test('localhost', () => {
const lint = processMarkdown(
dedent`
- [http://localhost](http://localhost)
- [http://localhost/alex/test](http://localhost/alex/test)
- [http://localhost:3000](http://localhost:3000)
- [http://localhost:3000/alex/test](http://localhost:3000/alex/test)
- [https://localhost](http://localhost)
- [https://localhost/alex/test](http://localhost/alex/test)
- [https://localhost:3000](http://localhost:3000)
- [https://localhost:3000/alex/test](http://localhost:3000/alex/test)
`,
{
skipLocalhost: true
}
);

return lint.then((vFile) => {
expect(vFile.messages.length).toBe(0);
});
});

test('local IP 127.0.0.1', () => {
const lint = processMarkdown(
dedent`
- [http://127.0.0.1](http://127.0.0.1)
- [http://127.0.0.1:3000](http://127.0.0.1:3000)
- [http://127.0.0.1/alex/test](http://127.0.0.1)
- [http://127.0.0.1:3000/alex/test](http://127.0.0.1:3000)
- [https://127.0.0.1](http://127.0.0.1)
- [https://127.0.0.1:3000](http://127.0.0.1:3000)
- [https://127.0.0.1/alex/test](http://127.0.0.1)
- [https://127.0.0.1:3000/alex/test](http://127.0.0.1:3000)
`,
{
skipLocalhost: true
}
);

return lint.then((vFile) => {
expect(vFile.messages.length).toBe(0);
});
});
});
});

0 comments on commit 0f37396

Please sign in to comment.