unified

Project: remcohaszing/remark-prettier

Package: remark-prettier@2.0.0

  1. Dependents: 0
  2. Check and format markdown or MDX using Prettier as a remark plugin
  1. remark 202
  2. unified 179
  3. markdown 155
  4. remark-plugin 72
  5. mdx 38

Remark Prettier

github actions codecov npm prettier

Check and format markdown using Prettier as a remark plugin

Installation

remark-prettier has a peer dependency on prettier

npm install prettier remark-prettier

Usage

By default this plugin does 2 things:

It has support for Prettier configuration files: .editorconfig, .prettierrc*, and .prettierignore.

For example, running remark --use remark-prettier . may yield:

README.md
  18:30-19:1  warning  Replace `⏎` with `·`  replace  prettier
        38:1  warning  Insert `⏎`            insert   prettier
  40:32-41:1  warning  Delete `⏎`            delete   prettier

This can also be spcified in a .remarkrc file:

{
  "plugins": ["remark-prettier"]
}

This plugin can also be used with programmatically:

import remark from 'remark';
import remarkPrettier from 'remark-prettier';
import { readSync } from 'to-vfile';

remark()
  .use(remarkPrettier)
  .process(readSync('README.md'))
  .then(({ messages, value }) => {
    // Formatted content
    console.log(value);

    // Prettier formatting violations
    console.dir(messages);
  });

remark-prettier registers a unified compiler. This means this plugin is used for formatting the document. Usually this is done by remark-stringify. When building a custom unified processor, remark-stringify can be omitted. If multiple compilers are registered, the last one registered is used.

import remarkParse from 'remark-parse';
import remarkPrettier from 'remark-prettier';
import { readSync } from 'to-vfile';
import unified from 'unified';

unified()
  .use(remarkParse)
  .use(remarkPrettier)
  .process(readSync('README.md'))
  .then(({ messages, value }) => {
    // Formatted content
    console.log(value);

    // Prettier formatting violations
    console.dir(messages);
  });

Options

The first argument may contain the options below. A second argument may specify overrides for the Prettier configuration, although this isn’t recommended.

format

By default remark-prettier registers a unified compiler, which formats the document using Prettier. This behaviour can be disabled by setting this option to false. (Default true)

report

By default remark-prettier reports differences from document as Prettier would format it. This behaviour can be disabled by setting this option to false. (Default true)

See also

License

MIT © Remco Haszing