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

Support multi-line search #4

Merged
merged 2 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdownlint-rule-search-replace",
"version": "1.0.2",
"version": "1.0.3",
"description": "A custom markdownlint rule for search and replaces",
"main": "rule.js",
"author": "Onkar Ruikar",
Expand Down
31 changes: 22 additions & 9 deletions rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const gatHtmlCommentRanges = (content, lines) => {

if (match[0].includes("\n")) {
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]);
}
}
} else {
Expand Down Expand Up @@ -161,12 +161,25 @@ module.exports = {
const match = result[0];
const [lineNo, columnNo] = getLocation(result.index, params.lines);

let replacement = "";
replacement = rule.search
? rule.replace
: match.replace(new RegExp(regex), rule.replace);

report(rule, match, lineNo, columnNo, 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);
}
}
}
},
Expand Down
27 changes: 27 additions & 0 deletions tests/regex-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,30 @@ test("replaceLineFix", (t) => {
"Output doesn't match."
);
});

test("multilineSearch", (t) => {
const inputFile = "./tests/data/options-tests.md";
const options = {
config: {
default: true,
"search-replace": {
rules: [
{
name: "test",
message: "test",
searchPattern: "/some text\\nsome -- text\\nsome/mg",
replace: "not applicable",
},
],
},
},
customRules: [searchReplace],
resultVersion: 3,
files: [inputFile],
};
const result = markdownlint.sync(options);
const expected = `./tests/data/options-tests.md: 3: search-replace Custom rule [test: test] [Context: "column: 4 text:'some text'"]
./tests/data/options-tests.md: 4: search-replace Custom rule [test: test] [Context: "column: 1 text:'some -- text'"]
./tests/data/options-tests.md: 5: search-replace Custom rule [test: test] [Context: "column: 1 text:'some'"]`;
t.is(result.toString(), expected, "Unexpected result.");
});