From 976aa90c6513df9866282d87153139830498273e Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Tue, 17 Oct 2023 19:12:03 +0100 Subject: [PATCH] feat: add global option to replacementPatterns --- README.md | 7 ++++++- index.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c568b94..fc383c9 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Parameters: * `timeout` timeout in [zeit/ms](https://www.npmjs.com/package/ms) format. (e.g. `"2000ms"`, `20s`, `1m`). Default `10s`. * `httpHeaders` to apply URL specific headers, see example below. * `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/ }]` - * `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}}/"}]` + * `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. * `projectBaseUrl` the URL to use for `{{BASEURL}}` replacement * `ignoreDisable` if this is `true` then disable comments are ignored. * `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: { "pattern": "^/", "replacement": "{{BASEURL}}/" + }, + { + "pattern": "%20", + "replacement": "-", + "global": true } ], "httpHeaders": [ diff --git a/index.js b/index.js index b7af4ee..2cf3ebc 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,7 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) { if (opts.replacementPatterns) { for (let replacementPattern of opts.replacementPatterns) { - let pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern); + let pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern, replacementPattern.global ? 'g' : ''); link = link.replace(pattern, performSpecialReplacements(replacementPattern.replacement, opts)); } }