0.6.1 • Published 3 years ago

remark-autofix v0.6.1

Weekly downloads
19
License
MIT
Repository
github
Last release
3 years ago

remark-autofix

Tests Package

This project, remark-autofix, is a remark plugin to apply fixes from warnings raised by retext plugins. The fixes are applied to the markdown abstract syntax tree when running remark-retext in bridge mode.

Supported Plugins

By default, this plugin only fixes vfile messages emitted from the following retext plugins:

By passing the options parameter, following the API, this plugin is tested to support:

Installation

npm install remark-autofix
# or
yarn add remark-autofix

Usage Examples

NOTE Chained calls to a remark processor's use method must occur in the following order with the following arguments:

  1. use(remark2retext, retextProcessor)
    • The retextProcessor must define a retext processor, which should emit vfile messages.
  2. use(autofix[, options])
    • for the options parameter, see the API.

Remove repeated words in Markdown

With retext-repeated-words:

const remark = require('remark');
const unified = require('unified');
const english = require('retext-english');
const remark2retext = require('remark-retext');
const repeated = require('retext-repeated-words');
const autofix = require('remark-autofix');

const inputMarkdown = `## Example
This link [link](https://example.com/) is not not duplicated.
`
const processor = remark().use(
  remark2retext, unified().use(english).use(repeated)
).use(autofix);

const outputMarkdown = processor.processSync(inputMarkdown).toString();

The outputMarkdown should be:

## Example

This [link](https://example.com/) is not duplicated.

Censor profanities in Markdown

With retext-profanities:

const remark = require('remark');
const unified = require('unified');
const english = require('retext-english');
const remark2retext = require('remark-retext');
const profanities = require('retext-profanities');
const autofix = require('remark-autofix');

const inputMarkdown = `Ah geez, you are not a loser.
`
const processor = remark().use(
  remark2retext, unified().use(english).use(profanities)
).use(autofix, {
  fixers: {
    'retext-profanities': (message) => {
    // Censor all but first letter of certain cuss words
    if (message.profanitySeverity >= 2 ) {
      return message.actual.replace(/\B./g,'-')
    }
  }
});

const outputMarkdown = processor.processSync(inputMarkdown).toString();

The outputMarkdown should be:

Ah g---, you are not a l----.

API

remark().use(remark2retext, retextProcessor).use(autofix, options)

remark and remark2retext

These must be imported from remark and remark-retext.

retextProcessor

A retext processor created by chaining unified's use method on:

  • a parser such as retext-english
  • one or more supported retext plugins to emit vfile messages
autofix

This is imported from this package, remark-autofix. It applies fixes to markdown from all supported vfile messages emitted from retextProcessor.

options

This is an optional object with one fixers property containing an object defined below.

options.fixers

This is an object to map retext plugin names to custom functions. See supported plugin names.

Each function provided in fixers should have the following signature:

Parameters:

For supported plugins, each message has the following relevant custom properties in addition to the vfile-message standard:

  • actual string identifying the part of the vfile that should be altered or removed.
  • expected array of strings. For certain plugins, the array may be empty to indicate that the actual value should be removed.

Return:

  • (string or null)

The plugin takes no action if the function returns null. A returned string becomes the sole value to consider from the message. The plugin evaluates all returned values from partially overlapping location ranges for the value of a single replacement. The plugin replaces all mdast nodes in the range with a single mdast node taking on the following value:

  • If possible, the first (by location.start) of the unique values returned for all overlapping messages
  • Else, a value with the maximum longest common substring with the full range of overlapping messages

Ecosystem

This repository works in conjunction with

  • the remark processor
  • the remark-retext processor
  • A retext processor created by chaining unified's use method on:

The plugin works with mdast to represent markdown and nlcst to represent text.

License

MIT licensed