markdownlint-rule-search-replace
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "markdownlint-rule-search-replace", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A custom markdownlint rule for search and replaces", | ||
@@ -23,3 +23,6 @@ "main": "rule.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"markdownlint-rule-helpers": "^0.16.0" | ||
} | ||
} |
@@ -60,3 +60,4 @@ # markdownlint-rule-search-replace | ||
Here, | ||
- `search-replace`: An array of search-replace definitions. | ||
- `search-replace`: The rule configuration object. | ||
- `rules`: An array of search-replace definitions. | ||
- search-replace definition: defines search term/pattern and replacement. | ||
@@ -68,3 +69,3 @@ - `name`: name of the definition | ||
- `replace`: The replacement string, e.g. `https`. Regex properties like `$1` can be used if `search_pattern` is being used. | ||
- `skip_code`: Optional. All code(inline and block), which is inside backticks, will be skipped. | ||
- `skip_code`: Optional. All code(inline and block), which is inside backticks, will be skipped. | ||
@@ -71,0 +72,0 @@ Note, `search` and `search_pattern` are interchangeable. The property `search` is used if both are supplied. |
88
rule.js
@@ -5,2 +5,3 @@ // @ts-check | ||
const helpers = require("markdownlint-rule-helpers"); | ||
@@ -10,7 +11,9 @@ module.exports = { | ||
description: "Custom rule", | ||
information: new URL("https://github.com/OnkarRuikar/markdownlint-rule-search-replace"), | ||
information: new URL( | ||
"https://github.com/OnkarRuikar/markdownlint-rule-search-replace" | ||
), | ||
tags: ["replace"], | ||
function: (params, onError) => { | ||
if(!params.config.rules) { | ||
if (!params.config.rules) { | ||
return; | ||
@@ -20,3 +23,5 @@ } | ||
const content = params.lines.join("\n"); | ||
const [backticksData, htmlCommentData] = gatherInfo(content); | ||
const lineMetadata = helpers.getLineMetadata(params); | ||
const codeRanges = helpers.codeBlockAndSpanRanges(params, lineMetadata); | ||
const htmlCommentRanges = gatHtmlCommentRanges(content, params.lines); | ||
@@ -28,13 +33,12 @@ params.config.rules.forEach((rule) => { | ||
while ((result = regex.exec(content)) !== null) { | ||
const match = result[0]; | ||
if(rule.skip_code && isCode(result.index, backticksData)) { | ||
continue; | ||
if (rule.skip_code && isCode(result.index, codeRanges, params.lines)) { | ||
continue; | ||
} | ||
if(isHTMLComment(result.index, htmlCommentData)) { | ||
if (isHTMLComment(result.index, htmlCommentRanges, params.lines)) { | ||
continue; | ||
} | ||
const [lineNo, columnNo] = getLocation(params.lines, result.index); | ||
const match = result[0]; | ||
const [lineNo, columnNo] = getLocation(result.index, params.lines); | ||
@@ -49,3 +53,3 @@ let replacement = ""; | ||
onError({ | ||
lineNumber: lineNo, | ||
lineNumber: lineNo + 1, | ||
detail: rule.name + ": " + rule.message, | ||
@@ -55,3 +59,3 @@ context: match, | ||
fixInfo: { | ||
lineNumber: lineNo, | ||
lineNumber: lineNo + 1, | ||
editColumn: columnNo + 1, | ||
@@ -67,3 +71,2 @@ deleteCount: match.length, | ||
const escapeForRegExp = (str) => { | ||
@@ -73,3 +76,2 @@ return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); | ||
/** | ||
@@ -91,9 +93,7 @@ * Converts string "/abc/ig" to new RegEx("abc", "ig"). | ||
/** | ||
* Given position in the document returns line and column number. | ||
*/ | ||
const getLocation = (lines, pos) => { | ||
let lineNo = 1; | ||
const getLocation = (pos, lines) => { | ||
let lineNo = 0; | ||
for (const line of lines) { | ||
@@ -109,63 +109,41 @@ pos -= line.length + 1; | ||
/** | ||
* Collect start and end position data of all code fences and html comments. | ||
*/ | ||
const gatherInfo = (content) => { | ||
const backtickRgx = /`+/g; | ||
const backticksData = []; | ||
const gatHtmlCommentRanges = (content, lines) => { | ||
const regex = /<!--\.*-->/gm; | ||
const ranges = []; | ||
let match = null; | ||
let lastLength = null; | ||
while ((match = backtickRgx.exec(content)) !== null) { | ||
const length = match[0].length; | ||
const pos = match.index; | ||
if(!lastLength) { | ||
backticksData.push([length, pos]); | ||
lastLength = length; | ||
} else if(lastLength === length) { | ||
backticksData.push([length, pos]); | ||
lastLength = null; | ||
} | ||
while ((match = regex.exec(content)) !== null) { | ||
const pos = getLocation(match.index, lines); | ||
ranges.push([...pos, match[0].length]); | ||
} | ||
const htmlCommentRgx = /<!--|-->/g; | ||
const htmlCommentData = []; | ||
while ((match = htmlCommentRgx.exec(content)) !== null) { | ||
htmlCommentData.push([match[0].length, match.index]); | ||
} | ||
return [backticksData, htmlCommentData]; | ||
return ranges; | ||
}; | ||
/** | ||
* Tells if the position lies inside a code block. | ||
*/ | ||
const isCode = (pos, data) => { | ||
return isPartOf(pos, data); | ||
const isCode = (pos, ranges, lines) => { | ||
return isPartOf(pos, ranges, lines); | ||
}; | ||
/** | ||
* Tells if the position lies inside an HTML comment. | ||
*/ | ||
const isHTMLComment = (pos, data) => { | ||
return isPartOf(pos, data); | ||
const isHTMLComment = (pos, ranges, lines) => { | ||
return isPartOf(pos, ranges, lines); | ||
}; | ||
/** | ||
* Tells if the position lies inside any block in the 'data' array. | ||
* Tells if the position lies inside any range | ||
*/ | ||
const isPartOf = (pos, data) => { | ||
for(let i = 0; i < data.length; i+=2 ) { | ||
const start = data[i][1]; | ||
const end = data[i+1][0] + data[i+1][1]; | ||
if(start <= pos && pos <= end ) { | ||
const isPartOf = (pos, ranges, lines) => { | ||
for (const [rLine, rColumn, rLength] of ranges) { | ||
const [line, column] = getLocation(pos, lines); | ||
if (rLine === line && rColumn <= column && column <= rColumn + rLength) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; |
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
122
12182
1
237
+ Addedmarkdownlint-rule-helpers@0.16.0(transitive)