Fetching contributors…
Cannot retrieve contributors at this time
95 lines (64 sloc) 2.26 KB


Build Status NPM version

Prettier now includes support for prettifying Markdown (including code blocks!), so as such, I'd recommend using the official Prettier instead. See the release notes for more information

A simple utility and CLI to run prettier on code blocks within Markdown, leaving any non-code blocks untouched.

Currently works on the following languages (basically everything prettier supports!):

  • JavaScript
  • TypeScript
  • JSON
  • CSS
  • SASS
  • LESS
  • GraphQL


yarn global add @dschau/prettier-markdown


Prettier Markdown



Command line usage is simple. All options (besides --dry, which will not write files to disk) are passed directly through to prettier.

prettier-markdown src/**/*.md --single-quote --trailing-comma es5


prettierMarkdown(files, prettierOpts = {}, programOpts = {})

Usage is fairly simple. An array of markdown files are passed, as well as any prettier options, and prettier is run on the specified files.

const path = require('path');
const { prettierMarkdown } = require('@dschau/prettier-markdown');

  ['', 'blog/posts/2017-01-01-hello-world/'].map(file =>
    path.join(process.cwd(), file)
).then(files => {
  // array of files that were written

Advanced Functionality

Line highlights

Note that line highlights (e.g. like the below) are kept intact and the block is still prettified!

```javascript {1-2}
const a =   'b';
const b =   'c';

  alert('hello world');


Frontmatter, i.e. in a Gastby blog post, is preserved as authored.

title: Hello World
  - Some Tag
  - Another Tag

// this will be prettified
var a =    'a';