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

docs(readme): explain named regex groups in replacementPatterns #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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)