-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass plugin options to setParserPlugin (#3297)
* pass plugin options to setParserPlugin some remark plugins require options to be passed to remark. for example, `remark.use(parserPlugin, requiredPlugin)`. This commit passes the pluginOptions to setParserPlugins. setParserPlugins returns an array of parsers. Now array elements can also be arrays of two elements: parser, and options. * undo unnecessary space * add gatsby-remark-custom-blocks * format
- Loading branch information
1 parent
c1d47bb
commit eea6220
Showing
8 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
*.un~ | ||
yarn.lock | ||
src | ||
flow-typed | ||
coverage | ||
decls | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# gatsby-remark-custom-blocks | ||
|
||
Adds custom blocks to `MarkdownRemark` using [remark-custom-blocks](https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-custom-blocks). | ||
|
||
## Install | ||
|
||
`npm install --save gatsby-remark-custom-blocks` | ||
|
||
## How to use | ||
|
||
```javascript | ||
// In your gatsby-config.js | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-transformer-remark`, | ||
options: { | ||
plugins: [ | ||
{ | ||
resolve: "gatsby-remark-custom-blocks", | ||
options: { | ||
blocks: { | ||
danger: "custom-block-danger", | ||
info: "custom-block-info", | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "gatsby-remark-custom-blocks", | ||
"version": "1.0.0", | ||
"description": "Gatsby remark plugin for adding custom blocks in markdown", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "yarn prebuild && babel --out-dir dist --ignore __tests__ src", | ||
"prepublish": "yarn build" | ||
}, | ||
"repository": "https://github.com/AlahmadiQ8/gatsby-remark-custom-blocks", | ||
"keywords": [ | ||
"gatsby", | ||
"gatsby-plugin", | ||
"markdown", | ||
"remark" | ||
], | ||
"author": "Mohammad Asad Mohammad <m91.alahmadi@gmail.com>", | ||
"license": "MIT", | ||
"private": false, | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"rimraf": "^2.6.2", | ||
"unist-util-find": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"remark-custom-blocks": "^1.0.6" | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/gatsby-remark-custom-blocks/src/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Remark = require(`remark`) | ||
const find = require(`unist-util-find`) | ||
const plugin = require(`../index`) | ||
|
||
describe(`gatsby-remark-custom-blocks`, () => { | ||
let remark | ||
|
||
beforeEach(() => { | ||
remark = new Remark() | ||
}) | ||
|
||
it(`errors if missing required plugin options`, () => { | ||
expect(plugin.setParserPlugins).toThrow(`missing required "blocks" option`) | ||
}) | ||
|
||
it(`creates nodes of blocks given in options`, () => { | ||
const parserPlugins = plugin.setParserPlugins({ | ||
blocks: { someType: `test`, anotherType: `another` }, | ||
}) | ||
const [parser, options] = parserPlugins[0] | ||
remark.use(parser, options) | ||
const markdownAST = remark.parse(` | ||
[[someType]] | ||
| content | ||
[[anotherType]] | ||
| content`) | ||
expect(find(markdownAST, { type: `someTypeCustomBlock` })).toBeTruthy() | ||
expect(find(markdownAST, { type: `anotherTypeCustomBlock` })).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const isNil = require(`lodash/isNil`) | ||
const has = require(`lodash/has`) | ||
const remarkCustomBlocks = require(`remark-custom-blocks`) | ||
|
||
module.exports.setParserPlugins = options => { | ||
if (isNil(options) || !has(options, `blocks`)) { | ||
throw Error(`missing required "blocks" option`) | ||
} | ||
return [[remarkCustomBlocks, options.blocks]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters