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.
By default, this plugin only fixes vfile
messages emitted from the following retext
plugins:
- retext-spell
- retext-quotes
- retext-diacritics
- retext-contractions
- retext-repeated-words
- retext-sentence-spacing
- retext-indefinite-article
- retext-redundant-acronyms
By passing the options parameter, following the API, this plugin is tested to support:
npm install remark-autofix
# or
yarn add remark-autofix
NOTE Chained calls to a remark processor's use method must occur in the following order with the following arguments:
use(remark2retext, retextProcessor)
- The
retextProcessor
must define aretext
processor, which should emitvfile
messages.
- The
use(autofix[, options])
- for the options parameter, see the API.
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.
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----.
These must be imported from remark
and remark-retext
.
A retext
processor created by chaining unified
's use
method on:
- a parser such as
retext-english
- one or more supported
retext
plugins to emitvfile
messages
This is imported from this package, remark-autofix
.
It applies fixes to markdown from all supported vfile
messages emitted from retextProcessor
.
This is an optional object with one fixers
property containing an object defined below.
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:
message
(vfile-message)
For supported plugins, each message
has the following relevant custom properties in addition to the vfile-message
standard:
actual
string identifying the part of thevfile
that should be altered or removed.expected
array of strings. For certain plugins, the array may be empty to indicate that theactual
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
This repository works in conjunction with
- the
remark
processor - the
remark-retext
processor - A
retext
processor created by chaining unified'suse
method on:- a parser such as retext-english
- one or more
retext
supported plugins to emitvfile
messages
The plugin works with mdast
to represent markdown and nlcst
to represent text.