onkarruikar / markdownlint-rule-search-replace Goto Github PK
View Code? Open in Web Editor NEWA custom markdownlint rule to replace strings/patterns
License: MIT License
A custom markdownlint rule to replace strings/patterns
License: MIT License
When applying the formatting rules of mdn/content to mdn/translated-content, we ran into a slight issue regarding smart (curly) quotes with the Chinese locale. Apparently, it is grammatically accurate to use smart quotes in place of normal quotes, so the rule to search and replace smart quotes causes localization issues.
It would be great to see a filter
option added, which is an object containing optional include
and exclude
properties, which are either a string or array of strings containing file globs for that rule to apply to or ignore. For example:
{
"name": "curly-double-quotes",
"message": "Don't use curly double quotes",
"searchPattern": "/“|”/g",
"replace": "\"",
"searchScope": "text",
"filter": {"exclude": "**/zh-cn/**"} // Ignore all Simplified Chinese files
},
{
"name": "m-dash",
"message": "Don't use '--'. Use m-dash — instead",
"search": " -- ",
"replace": " — ",
"searchScope": "text",
"filter": {"exclude": ["README.md", "**/es/**"]} // Ignore README.md in root, and all Spanish files
},
{
"name": "em-dash",
"message": "Don't use '--'. Use em-dash — instead",
"search": " -- ",
"replace": " — ",
"searchScope": "text",
"filter": {"include": "**/es/**"} // Only apply to Spanish files
},
Here is our .markdownlint.json
file with a custom rule, per the documentation:
This is erroring out when actually linting a file:
I believe the error is occurring here, but I'm not able to debug it:
markdownlint-rule-search-replace/rule.js
Lines 17 to 21 in 4ef9041
To make a rule with multiple search words without replacements the README suggests to use an array with undefined elements (a sparse array):
A list of words and corresponding list of replacements can be provided in a single rule:
{ "default": true, "search-replace": { "rules": [ { "name": "bad-spellings", "message": "Incorrect spelling", "search": ["e-mail", "wtf", "web site"], "replace": ["email", , "website"], "skipCode": false } ] } }
But altough valid in javascript, this is not valid in JSON! Javascripts undefined
doesn't exist in JSON (nor YAML), so this cannot be entered into the markdownlint configuration files, it can only be used when calling markdownlint from js.
JSON has null
which maps to javascripts null
, but setting replace
to null results in the word being replaced by "null"
.
This is a feature request to be able to define a custom URL for each rule in the config that is used when that rule shows an error.
Currently each error shows the URL of this repository:
https://github.com/OnkarRuikar/markdownlint-rule-search-replace/blob/main/rule.js#L139-L140
For my use case, the error is seen by people in GitHub pull requests that are not familiar with how markdownlint
works. So when they are reading the error, a link to this repository doesn't help them.
Instead, if I was able to add custom URLs to either documentation or the rule definition in my config, they would be able to better understand the rule and how to fix the error.
Thanks for reading and your consideration! This is a helpful tool—I appreciate you working on it 💖
searchPattern: "/(?<=[\\w\\d ])\\n(?=[\\w\\d])/g"
results in:
search-replace Custom rule [This rule threw an exception: Property 'range' of onError parameter is incorrect.]
what's interesting is that the rule actually seems to work, it's just a problem with the output. this rule is meant to catch linebreaks within a sentence. e.g.
The following line
break should not exist.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.