Skip to content

Commit 2a85427

Browse files
authored
Merge pull request #279 from CanadaHonk/replacementpatterns-global
feat: add global option to replacementPatterns
2 parents 88a6403 + 976aa90 commit 2a85427

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Parameters:
7171
* `timeout` timeout in [zeit/ms](https://www.npmjs.com/package/ms) format. (e.g. `"2000ms"`, `20s`, `1m`). Default `10s`.
7272
* `httpHeaders` to apply URL specific headers, see example below.
7373
* `ignorePatterns` an array of objects holding regular expressions which a link is checked against and skipped for checking in case of a match. Example: `[{ pattern: /foo/ }]`
74-
* `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 base folder (used from `projectBaseUrl`) (for example that `/` points to the root of your local repository). Example: `[{ pattern: /^.attachments/, replacement: "file://some/conventional/folder/.attachments" }, { pattern: ^/, replacement: "{{BASEURL}}/"}]`
74+
* `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 base folder (used from `projectBaseUrl`) (for example that `/` points to the root of your local repository). Example: `[{ pattern: /^.attachments/, replacement: "file://some/conventional/folder/.attachments" }, { pattern: ^/, replacement: "{{BASEURL}}/"}]`. You can add `"global": true` to use a global regular expression to replace all instances.
7575
* `projectBaseUrl` the URL to use for `{{BASEURL}}` replacement
7676
* `ignoreDisable` if this is `true` then disable comments are ignored.
7777
* `retryOn429` if this is `true` then retry request when response is an HTTP code 429 after the duration indicated by `retry-after` header.
@@ -210,6 +210,11 @@ Options:
210210
{
211211
"pattern": "^/",
212212
"replacement": "{{BASEURL}}/"
213+
},
214+
{
215+
"pattern": "%20",
216+
"replacement": "-",
217+
"global": true
213218
}
214219
],
215220
"httpHeaders": [

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
8989

9090
if (opts.replacementPatterns) {
9191
for (let replacementPattern of opts.replacementPatterns) {
92-
let pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern);
92+
let pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern, replacementPattern.global ? 'g' : '');
9393
link = link.replace(pattern, performSpecialReplacements(replacementPattern.replacement, opts));
9494
}
9595
}

0 commit comments

Comments
 (0)