Skip to content

Commit

Permalink
pass plugin options to setParserPlugin (#3297)
Browse files Browse the repository at this point in the history
* 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
Mohammad Asad Mohammad authored and KyleAMathews committed Dec 22, 2017
1 parent c1d47bb commit eea6220
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-remark-custom-blocks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
34 changes: 34 additions & 0 deletions packages/gatsby-remark-custom-blocks/.npmignore
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
31 changes: 31 additions & 0 deletions packages/gatsby-remark-custom-blocks/README.md
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",
},
},
},
],
},
},
];
```
33 changes: 33 additions & 0 deletions packages/gatsby-remark-custom-blocks/package.json
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 packages/gatsby-remark-custom-blocks/src/__tests__/index.js
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()
})
})
10 changes: 10 additions & 0 deletions packages/gatsby-remark-custom-blocks/src/index.js
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]]
}
11 changes: 9 additions & 2 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ module.exports = (
for (let plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (let parserPlugin of requiredPlugin.setParserPlugins()) {
remark = remark.use(parserPlugin)
for (let parserPlugin of requiredPlugin.setParserPlugins(
plugin.pluginOptions
)) {
if (_.isArray(parserPlugin)) {
const [parser, options] = parserPlugin
remark = remark.use(parser, options)
} else {
remark = remark.use(parserPlugin)
}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10416,6 +10416,12 @@ relay-runtime@1.4.1:
fbjs "^0.8.14"
relay-debugger-react-native-runtime "0.0.10"

remark-custom-blocks@^1.0.6:
version "1.0.6"
resolved "https://registry.npmjs.org/remark-custom-blocks/-/remark-custom-blocks-1.0.6.tgz#2a756630cbfa6943ee69fa76432c59e4cd36112e"
dependencies:
space-separated-tokens "^1.1.1"

remark-html@6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/remark-html/-/remark-html-6.0.1.tgz#5094d2c71f7941fdb2ae865bac76627757ce09c1"
Expand Down Expand Up @@ -10883,7 +10889,7 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

rimraf@2, rimraf@^2.2.6, rimraf@^2.2.8, rimraf@^2.3.2, rimraf@^2.3.3, rimraf@^2.4.4, rimraf@^2.5.0, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1:
rimraf@2, rimraf@^2.2.6, rimraf@^2.2.8, rimraf@^2.3.2, rimraf@^2.3.3, rimraf@^2.4.4, rimraf@^2.5.0, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
dependencies:
Expand Down Expand Up @@ -11614,7 +11620,7 @@ sourcemapped-stacktrace@^1.1.6:
dependencies:
source-map "0.5.6"

space-separated-tokens@^1.0.0:
space-separated-tokens@^1.0.0, space-separated-tokens@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-1.1.1.tgz#9695b9df9e65aec1811d4c3f9ce52520bc2f7e4d"
dependencies:
Expand Down Expand Up @@ -12696,7 +12702,7 @@ unist-builder@^1.0.0, unist-builder@^1.0.1:

unist-util-find@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/unist-util-find/-/unist-util-find-1.0.1.tgz#1062bbb6928c7a97c6adc89b53745d4c46c222a2"
resolved "https://registry.npmjs.org/unist-util-find/-/unist-util-find-1.0.1.tgz#1062bbb6928c7a97c6adc89b53745d4c46c222a2"
dependencies:
lodash.iteratee "^4.5.0"
remark "^5.0.1"
Expand Down

0 comments on commit eea6220

Please sign in to comment.