From 0f37396c081d89f3da01558d86f269da4f65a6d9 Mon Sep 17 00:00:00 2001 From: Alexandr Tovmach Date: Sat, 26 Oct 2019 17:45:49 +0300 Subject: [PATCH] feat: Added skipLocalhost option (#17) --- README.md | 3 +++ index.js | 5 +++++ test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2f05449..4439030 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 3024ef4..867f713 100644 --- a/index.js +++ b/index.js @@ -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] = []; diff --git a/test.js b/test.js index 7791384..432c078 100644 --- a/test.js +++ b/test.js @@ -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: { @@ -55,7 +57,11 @@ 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 } }) ); @@ -63,13 +69,17 @@ describe('remark-lint-no-dead-urls', () => { 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 @@ -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); + }); + }); + }); });