markdownlint-rule-search-replace
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "markdownlint-rule-search-replace", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A custom markdownlint rule for search and replaces", | ||
@@ -5,0 +5,0 @@ "main": "rule.js", |
@@ -78,2 +78,22 @@ # markdownlint-rule-search-replace | ||
In patterns, to escape characters use `\\`. For example, | ||
```json | ||
{ | ||
"default": true, | ||
"search-replace": { | ||
"rules": [ | ||
{ | ||
"name": "test", | ||
"message": "bla bla bla", | ||
"searchPattern": "^/\\.\\.\\.(.*)\\.\\.\\.$/mg", | ||
"replace": "-- $1 --" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
This will replace line `...abcd...` with `-- abcd --`. | ||
### Disable rule options | ||
@@ -121,7 +141,21 @@ | ||
```js | ||
const markdownlint = require("markdownlint"); | ||
const searchReplace = require("markdownlint-rule-search-replace"); | ||
const options = { | ||
... | ||
"customRules": [searchReplace] | ||
files: ["myMarkdown.md"], | ||
config: { | ||
default: true, | ||
"search-replace": { | ||
rules: [ | ||
{ | ||
name: "m-dash", | ||
message: "Don't use '--'.", | ||
search: "--", | ||
replace: "—", | ||
}, | ||
], | ||
}, | ||
}, | ||
customRules: [searchReplace], | ||
}; | ||
@@ -128,0 +162,0 @@ |
73
rule.js
@@ -18,9 +18,4 @@ // @ts-check | ||
const stringToRegex = (str) => { | ||
let pattern = (str.match(/\/(.+)\/.*/) || [])[1]; | ||
if (!pattern) { | ||
pattern = escapeForRegExp(str); | ||
} | ||
const pattern = (str.match(/\/(.+)\/.*/) || ["", "a^"])[1]; | ||
const flags = (str.match(/\/.+\/(.*)/) || ["", "g"])[1]; | ||
return new RegExp(pattern, flags); | ||
@@ -64,7 +59,7 @@ }; | ||
const parts = match[0].split("\n"); | ||
for (const [i, p] of parts.entries()) { | ||
for (const [i, part] of parts.entries()) { | ||
if (i === 0) { | ||
ranges.push([...pos, p.length]); | ||
ranges.push([...pos, part.length]); | ||
} else { | ||
ranges.push([pos[0] + i, 0, p.length]); | ||
ranges.push([pos[0] + i, 0, part.length]); | ||
} | ||
@@ -126,2 +121,21 @@ } | ||
function: (params, onError) => { | ||
const report = (rule, match, lineNo, columnNo, replacement) => { | ||
const options = { | ||
lineNumber: lineNo + 1, | ||
detail: rule.name + ": " + rule.message, | ||
context: `column: ${columnNo + 1} text:'${match}'`, | ||
range: [columnNo + 1, match.length], | ||
}; | ||
if (typeof replacement !== "undefined") { | ||
options.fixInfo = { | ||
lineNumber: lineNo + 1, | ||
editColumn: columnNo + 1, | ||
deleteCount: match.length, | ||
insertText: replacement, | ||
}; | ||
} | ||
onError(options); | ||
}; | ||
if (!params.config.rules) { | ||
@@ -137,4 +151,5 @@ return; | ||
for (const rule of params.config.rules) { | ||
const regex = stringToRegex(rule.search || rule.searchPattern); | ||
const regex = rule.search | ||
? new RegExp(escapeForRegExp(rule.search), "g") | ||
: stringToRegex(rule.searchPattern); | ||
let result = null; | ||
@@ -153,19 +168,21 @@ while ((result = regex.exec(content)) !== null) { | ||
let replacement = ""; | ||
replacement = rule.search | ||
? rule.replace | ||
: match.replace(new RegExp(regex), rule.replace); | ||
onError({ | ||
lineNumber: lineNo + 1, | ||
detail: rule.name + ": " + rule.message, | ||
context: `column: ${columnNo + 1} text:'${match}'`, | ||
range: [columnNo + 1, match.length], | ||
fixInfo: { | ||
lineNumber: lineNo + 1, | ||
editColumn: columnNo + 1, | ||
deleteCount: match.length, | ||
insertText: replacement, | ||
}, | ||
}); | ||
// The parent project 'markdownlint' processes markdown line by line. | ||
// It doesn't allow multiline fixes. The line ending('\n') can't be removed from plugins. | ||
// That is why when the 'match' is multiline we can't suggest fix for it. | ||
// However, we can report error for each line. | ||
if (match.includes("\n")) { | ||
const parts = match.split("\n"); | ||
for (const [i, part] of parts.entries()) { | ||
if (i === 0) { | ||
report(rule, part, lineNo, columnNo); | ||
} else { | ||
report(rule, part, lineNo + i, 0); | ||
} | ||
} | ||
} else { | ||
const replacement = rule.search | ||
? rule.replace | ||
: match.replace(new RegExp(regex), rule.replace); | ||
report(rule, match, lineNo, columnNo, replacement); | ||
} | ||
} | ||
@@ -172,0 +189,0 @@ } |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12484
167
167
0
4