A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/OnkarRuikar/markdownlint-rule-search-replace below:

OnkarRuikar/markdownlint-rule-search-replace: A custom markdownlint rule to replace strings/patterns

markdownlint-rule-search-replace

A custom markdownlint rule to search and replace patterns.

In markdown files, sometimes we want to replace certain characters or patterns. For example,

Or specific cases like replace three backticks with one. [ref]

Or ban certain words e.g. "wtf".

In such scenarios the markdownlint-rule-search-replace rule can be used to flag or fix such occurrences.

Use following command to install

npm install markdownlint-rule-search-replace --save-dev

There are various ways markdownlint can be configured using objects, config files etc. For more information on markdownlint configuration refer options.config.

Using .markdownlint.json config file

You'll have to add a configuration entry in the .markdownlint.json file. For example,

{
  "default": true,
  "MD001": false,
  "search-replace": {
    "rules": [
      {
        "name": "ellipsis",
        "message": "Do not use three dots '...' for ellipsis.",
        "information": "https://example.com/rules/ellipsis",
        "search": "...",
        "replace": "",
        "searchScope": "text"
      },
      {
        "name": "curly-double-quotes",
        "message": "Do not use curly double quotes.",
        "searchPattern": "/“|”/g",
        "replace": "\""
      }
    ]
  }
}

Here,

Properties are case-sensitive and are in camel case.
Note: search and searchPattern are interchangeable. The property search is used if both are supplied.

In patterns, to escape characters use \\. For example,

{
  "default": true,
  "search-replace": {
    "rules": [
      {
        "name": "test",
        "message": "bla bla bla",
        "searchPattern": "^/\\.\\.\\.(.*)\\.\\.\\.$/mg",
        "replace": "-- $1 --"
      }
    ]
  }
}

This will replace line ...abcd... with -- abcd --.

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", null, "website"],
        "searchScope": "all"
      }
    ]
  }
}

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.

The rule can be disabled for specific section or file. For example, if you want to disable the rule for a particular section:

...

### Markdown rules to follow

The rules are:

<!-- markdownlint-disable search-replace -->

- Do not use three dots '...' for ellipsis. Use '…' instead.
- Do not use two hyphens '--' use m-dash '—'.
<!-- markdownlint-enable search-replace -->

...

Here, the markdownlint will not apply the search-replace rule on the list. For more options refer Configuration section on markdownlint repo page.

There are various ways to run markdownlint.

Use following command for markdownlint-cli:

markdownlint test.md -r markdownlint-rule-search-replace
# or
markdownlint test.md -r markdownlint-rule-search-replace --fix

Add the rule object to the customRules array:

const markdownlint = require("markdownlint");
const searchReplace = require("markdownlint-rule-search-replace");

const options = {
  files: ["myMarkdown.md"],
  config: {
    default: true,
    "search-replace": {
      rules: [
        {
          name: "m-dash",
          message: "Don't use '--'.",
          search: "--",
          replace: "—",
        },
      ],
    },
  },
  customRules: [searchReplace],
};

markdownlint(options, function callback(err, result) {
  if (!err) {
    console.log(result.toString());
  }
});
Projects using this custom rule

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4