Skip to content

Commit

Permalink
Merge pull request #327 from AndreyNautilus/docs/update_readme_and_tests
Browse files Browse the repository at this point in the history
docs(readme): explain named regex groups in replacementPatterns
  • Loading branch information
tcort authored Nov 5, 2024
2 parents 71ccb41 + 3c31a25 commit 3003004
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -232,6 +232,10 @@ Options:
"pattern": "%20",
"replacement": "-",
"global": true
},
{
"pattern": "images/(?<filename>.*)",
"replacement": "assets/$<filename>"
}
],
"httpHeaders": [
Expand Down
27 changes: 27 additions & 0 deletions test/markdown-link-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/(?<filename>.*)', replacement: '$<filename>'}
]}, 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);
Expand Down
9 changes: 9 additions & 0 deletions test/regex-groups-replacement.md
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3003004

Please sign in to comment.