markdownlint-rule-search-replace
Advanced tools
Comparing version 1.0.6 to 1.0.7
{ | ||
"name": "markdownlint-rule-search-replace", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "A custom markdownlint rule for search and replaces", | ||
@@ -23,2 +23,3 @@ "main": "rule.js", | ||
"test": "ava tests/*-tests.js", | ||
"lint-check": "eslint --max-warnings 0 .", | ||
"lint": "eslint --max-warnings 0 --fix .", | ||
@@ -25,0 +26,0 @@ "pretty-check": "prettier --check .", |
@@ -19,3 +19,3 @@ # markdownlint-rule-search-replace | ||
Or ban certain words. | ||
Or ban certain words e.g. "wtf". | ||
@@ -70,7 +70,7 @@ In such scenarios the `markdownlint-rule-search-replace` rule can be used to flag or fix such occurrences. | ||
- search-replace definition: defines search term/pattern and replacement. | ||
- `name`: name of the definition | ||
- `message`: corresponding message | ||
- `search`: text to search | ||
- `searchPattern`: regex pattern to search. Include flags as well, as if you are defining a regex literal in JavaScript, e.g. `/http/g`. | ||
- `replace`: Optional. The replacement string, e.g. `https`. Regex properties like `$1` can be used if `searchPattern` is being used. | ||
- `name`: name of the definition. | ||
- `message`: corresponding message. | ||
- `search`: text or array of texts to search | ||
- `searchPattern`: regex pattern or array of patterns to search. Include flags as well, as if you are defining a regex literal in JavaScript, e.g. `/http/g`. | ||
- `replace`: Optional. The replacement string(s), e.g. `https`. Regex properties like `$1` can be used if `searchPattern` is being used. | ||
- `skipCode`: Optional. All code(inline and block), which is inside backticks, will be skipped. | ||
@@ -100,2 +100,23 @@ | ||
A list of words and corresponding list of replacements can be provided in a single rule: | ||
```json | ||
{ | ||
"default": true, | ||
"search-replace": { | ||
"rules": [ | ||
{ | ||
"name": "bad-spellings", | ||
"message": "Incorrect spelling", | ||
"search": ["e-mail", "wtf", "web site"], | ||
"replace": ["email", , "website"], | ||
"skipCode": false | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
This is a good way to group related search replace terms in one rule. Make sure the replacements are at same indices as the corresponding search terms. In above example, the word "wtf" will get flagged but won't be auto fixed. Use empty replacement(`""`) if you wish to remove it. | ||
### Disable rule options | ||
@@ -170,4 +191,5 @@ | ||
``` | ||
## Projects using this custom rule | ||
- MDN Web Docs - [code](https://github.com/mdn/content/blob/main/.markdownlint-cli2.jsonc#L125) |
36
rule.js
@@ -127,3 +127,3 @@ // @ts-check | ||
if (typeof replacement !== "undefined" && replacement !== null) { | ||
if (replacement !== undefined && replacement !== null) { | ||
options.fixInfo = { | ||
@@ -143,2 +143,3 @@ lineNumber: lineNo + 1, | ||
let rules = params.config.rules; | ||
const content = params.lines.join("\n"); | ||
@@ -149,3 +150,32 @@ const lineMetadata = helpers.getLineMetadata(params); | ||
for (const rule of params.config.rules) { | ||
// expand multivalue rules | ||
const listRules = []; | ||
for (const rule of rules) { | ||
if ( | ||
(rule.search && Array.isArray(rule.search)) || | ||
(rule.searchPattern && Array.isArray(rule.searchPattern)) | ||
) { | ||
listRules.push(rule); | ||
} | ||
} | ||
rules = rules.filter((r) => !listRules.includes(r)); | ||
for (const rule of listRules) { | ||
if (rule.search) { | ||
for (const [index, word] of rule.search.entries()) { | ||
const clone = { ...rule }; | ||
clone.search = word; | ||
clone.replace = rule.replace[index]; | ||
rules.push(clone); | ||
} | ||
} else if (rule.searchPattern) { | ||
for (const [index, pattern] of rule.searchPattern.entries()) { | ||
const clone = { ...rule }; | ||
clone.searchPattern = pattern; | ||
clone.replace = rule.replace[index]; | ||
rules.push(clone); | ||
} | ||
} | ||
} | ||
for (const rule of rules) { | ||
const regex = rule.search | ||
@@ -182,3 +212,3 @@ ? new RegExp(escapeForRegExp(rule.search), "g") | ||
let replacement = null; | ||
if (typeof rule.replace !== "undefined") { | ||
if (rule.replace !== undefined) { | ||
replacement = rule.search | ||
@@ -185,0 +215,0 @@ ? rule.replace |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14485
199
192