remark-autofix
v0.6.1
Published
A remark plugin to apply fixes from warnings raised by other plugins
Downloads
19
Readme
remark-autofix
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:
- 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:
Installation
npm install remark-autofix
# or
yarn add remark-autofixUsage Examples
NOTE Chained calls to a remark processor's use method must occur in the following order with the following arguments:
use(remark2retext, retextProcessor)- The
retextProcessormust define aretextprocessor, which should emitvfilemessages.
- The
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
retextplugins to emitvfilemessages
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:
message(vfile-message)
For supported plugins, each message has the following relevant custom properties in addition to the vfile-message standard:
actualstring identifying the part of thevfilethat should be altered or removed.expectedarray of strings. For certain plugins, the array may be empty to indicate that theactualvalue 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
remarkprocessor - the
remark-retextprocessor - A
retextprocessor created by chaining unified'susemethod on:- a parser such as retext-english
- one or more
retextsupported plugins to emitvfilemessages
The plugin works with mdast to represent markdown and nlcst to represent text.
