Skip to content

Commit bdb7d62

Browse files
tkraakjescalan
authored andcommitted
Add support for markdown-it plugins (#22)
* load markdown-it plugins * test markdown-it plugins * add section to readme on markdown plugins and options (#1)
1 parent 836d4d6 commit bdb7d62

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Any of these plugins can be customized by passing options described below.
8484
| **delimiters** | Delimiters used for html-escaped expressions | `['{{', '}}']` |
8585
| **unescapeDelimiters** | Delimiters used for unescaped expressions | `['{{{', '}}}']` |
8686
| **markdown** | Options passed in to [markdown-it](https://github.com/markdown-it/markdown-it) constructor | `{ typographer: true, linkify: true }` |
87+
| **markdownPlugins** | Plugins to be loaded by [markdown-it](https://github.com/markdown-it/markdown-it) parser. See below for more details. | |
8788
| **content** | Options passed to the [reshape-content](https://github.com/reshape/content) plugin | `{ md: renderMarkdown, mdi: renderMarkdownInline }` |
8889
| **parser** | custom html parser if desired. pass `false` to use the default html parser | `sugarml` |
8990
| **retext** | Plugins to be passed to the [reshape-retext](https://github.com/reshape/retext) plugin | `[smartypants]` ([ref](https://github.com/wooorm/retext-smartypants)) |
@@ -129,6 +130,26 @@ Would render without additional paragraph wrappings or unexpected title renders:
129130
<p> Hello, I am #1 and this is <a href='#'>my link</a>.
130131
```
131132

133+
### Markdown Plugins
134+
135+
You can pass an array of [markdown-it plugins](https://www.npmjs.com/browse/keyword/markdown-it-plugin) via the `markdownPlugins` option with or without their own options.
136+
137+
```js
138+
const reshape = require('reshape')
139+
const standard = require('reshape-standard')
140+
const emoji = require('markdown-it-emoji')
141+
const anchor = require('markdown-it-anchor')
142+
const toc = require('markdown-it-table-of-contents')
143+
144+
reshape(standard(markdownPlugins: [
145+
emoji,
146+
anchor,
147+
[toc, { containerClass: 'toc' }]
148+
]))
149+
.process(someHtml)
150+
.then((res) => console.log(res.output()))
151+
```
152+
132153
### License & Contributing
133154

134155
- Details on the license [can be found here](LICENSE.md)

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ module.exports = function reshapeStandard (options = {}) {
5050
if (!contentOpt.md) {
5151
const markdownOpt = { typographer: true, linkify: true }
5252
const md = new MarkdownIt(Object.assign({}, markdownOpt, options.markdown))
53+
// if the user has provided markdown-it plugins, load them into the parser instance
54+
;(options.markdownPlugins || []).forEach((plugin) => {
55+
Array.isArray(plugin) ? md.use(...plugin) : md.use(plugin)
56+
})
5357
contentOpt.md = md.render.bind(md)
5458
contentOpt.mdi = md.renderInline.bind(md)
5559
}

test/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,18 @@ test('prependPlugins option', (t) => {
117117
t.truthy(out.plugins[0] === 'test')
118118
t.truthy(out2.plugins[0] === 'test')
119119
})
120+
121+
test('markdownPlugins option', (t) => {
122+
const plugins = [1, 2, 3]
123+
let count = 0
124+
function MarkdownIt () {}
125+
MarkdownIt.prototype.use = (plugin) => {
126+
t.truthy(plugins[count] === plugin)
127+
count++
128+
}
129+
MarkdownIt.prototype.render = () => {}
130+
MarkdownIt.prototype.renderInline = () => {}
131+
const undo = standardRewired.__set__('MarkdownIt', MarkdownIt)
132+
standardRewired({ markdownPlugins: plugins })
133+
undo()
134+
})

0 commit comments

Comments
 (0)