From 3c31a25bb8f99d3886320fd89bac10fd5188945e Mon Sep 17 00:00:00 2001 From: Andrey Potapov Date: Mon, 27 May 2024 01:26:07 +0200 Subject: [PATCH] docs: add named regex groups to readme add test for named regex groups in replacementPatterns --- README.md | 6 +++++- test/markdown-link-check.test.js | 27 +++++++++++++++++++++++++++ test/regex-groups-replacement.md | 9 +++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/regex-groups-replacement.md diff --git a/README.md b/README.md index e7b2a92..008fab5 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ Options: `config.json`: * `ignorePatterns`: An array of objects holding regular expressions which a link is checked against and skipped for checking in case of a match. -* `replacementPatterns`: An array of objects holding regular expressions which are replaced in a link with their corresponding replacement string. This behavior allows (for example) to adapt to certain platform conventions hosting the Markdown. The special replacement `{{BASEURL}}` can be used to dynamically link to the current working directory (for example that `/` points to the root of your current working directory). +* `replacementPatterns`: An array of objects holding regular expressions which are replaced in a link with their corresponding replacement string. This behavior allows (for example) to adapt to certain platform conventions hosting the Markdown. The special replacement `{{BASEURL}}` can be used to dynamically link to the current working directory (for example that `/` points to the root of your current working directory). This parameter supports named regex groups the same way as `string.replace` [method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement) in node. * `httpHeaders`: The headers are only applied to links where the link **starts with** one of the supplied URLs in the `urls` section. * `timeout` timeout in [zeit/ms](https://www.npmjs.com/package/ms) format. (e.g. `"2000ms"`, `20s`, `1m`). Default `10s`. * `retryOn429` if this is `true` then retry request when response is an HTTP code 429 after the duration indicated by `retry-after` header. @@ -232,6 +232,10 @@ Options: "pattern": "%20", "replacement": "-", "global": true + }, + { + "pattern": "images/(?.*)", + "replacement": "assets/$" } ], "httpHeaders": [ diff --git a/test/markdown-link-check.test.js b/test/markdown-link-check.test.js index a46cfc5..9ebeaff 100644 --- a/test/markdown-link-check.test.js +++ b/test/markdown-link-check.test.js @@ -370,6 +370,33 @@ describe('markdown-link-check', function () { done(); }); }); + + it('should correctly resolve regex named groups in replacement patterns', function (done) { + markdownLinkCheck(fs.readFileSync(path.join(dirname, 'regex-groups-replacement.md')).toString().replace(/%%BASE_URL%%/g, 'file://' + dirname), {baseUrl: 'file://' + dirname, projectBaseUrl: 'file://' + dirname + "/..",replacementPatterns: [ + {pattern: '^/', replacement: "{{BASEURL}}/"}, + {pattern: 'folder-to-be-ignored/(?.*)', replacement: '$'} + ]}, function (err, results) { + expect(err).to.be(null); + expect(results).to.be.an('array'); + + const expected = [ + { statusCode: 200, status: 'alive' }, + { statusCode: 200, status: 'alive' }, + { statusCode: 200, status: 'alive' }, + { statusCode: 200, status: 'alive' } + ]; + + expect(results.length).to.be(expected.length); + + for (let i = 0; i < results.length; i++) { + expect(results[i].statusCode).to.be(expected[i].statusCode); + expect(results[i].status).to.be(expected[i].status); + } + + done(); + }); + }); + it('check hash links', function (done) { markdownLinkCheck(fs.readFileSync(path.join(dirname, 'hash-links.md')).toString(), {}, function (err, result) { expect(err).to.be(null); diff --git a/test/regex-groups-replacement.md b/test/regex-groups-replacement.md new file mode 100644 index 0000000..8503a33 --- /dev/null +++ b/test/regex-groups-replacement.md @@ -0,0 +1,9 @@ +# Sample + +This is a test file leveraging special replacement patterns: + +![img](hello.jpg) (alive) +![txt](file.md) (alive) + +![img](folder-to-be-ignored/hello.jpg) (replaced and then alive) +![txt](folder-to-be-ignored/file.md) (replaced and then alive)