Extract code blocks from an MDAST tree
npm install remark-code-blocks> Remark plugin to extract code nodes from markdown.


```
npm i -S remark-code-blocks
`js
const toVfile = require('to-vfile')
const unified = require('unified')
const parser = require('remark-parser')
const stringify = require('remark-stringify')
const codeblocks = require('remark-code-blocks')
unified()
.use(parser)
.use(stringify)
.use(codeblocks, { / options / })
.process(toVfile('./example.md'))
.then(file => {
/ file.data.codeblocks = [ ... ] /
})
`
or use the standalone function which takes a tree as its first argument.
`js
const toVfile = require('to-vfile')
const unified = require('unified')
const parser = require('remark-parser')
const { codeblocks } = require('remark-code-blocks')
const tree = unified().use(parser).parse(toVfile('./example.md'))
const code = codeblocks(tree, { / options / })
`
The results are stored in file.data in a codeblocks property by default. You can override the name of the property using options.name.
Also exports a standalone function.
#### lang
Type: stringall
Default:
Specify a language and only extract code nodes with that language. Otherwise all code nodes are extracted.
#### name
Type: stringcodeblocks
Default:
Specify the name of the property in file.data
#### formatter
Type: function
Default: none
Add a function to run over the nodes values before storing them in file.data`
MIT © Paul Zimmer