Skip to content

Commit aff6de4

Browse files
authoredFeb 9, 2022
Add support for passing options to remark-rehype (#1935)
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 3f739a3 commit aff6de4

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
 

Diff for: ‎packages/mdx/lib/core.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @typedef {import('./plugin/recma-document.js').RecmaDocumentOptions} RecmaDocumentOptions
55
* @typedef {import('./plugin/recma-stringify.js').RecmaStringifyOptions} RecmaStringifyOptions
66
* @typedef {import('./plugin/recma-jsx-rewrite.js').RecmaJsxRewriteOptions} RecmaJsxRewriteOptions
7+
* @typedef {import('remark-rehype').Options} RemarkRehypeOptions
78
*
89
* @typedef BaseProcessorOptions
910
* @property {boolean} [jsx=false]
@@ -22,6 +23,8 @@
2223
* List of remark (mdast, markdown) plugins.
2324
* @property {PluggableList} [rehypePlugins]
2425
* List of rehype (hast, HTML) plugins.
26+
* @property {RemarkRehypeOptions} [remarkRehypeOptions]
27+
* Options to pass through to `remark-rehype`.
2528
*
2629
* @typedef {Omit<RecmaDocumentOptions & RecmaStringifyOptions & RecmaJsxRewriteOptions, 'outputFormat'>} PluginOptions
2730
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
@@ -70,6 +73,7 @@ export function createProcessor(options = {}) {
7073
recmaPlugins,
7174
rehypePlugins,
7275
remarkPlugins,
76+
remarkRehypeOptions = {},
7377
SourceMapGenerator,
7478
...rest
7579
} = options
@@ -103,7 +107,12 @@ export function createProcessor(options = {}) {
103107
pipeline
104108
.use(remarkMarkAndUnravel)
105109
.use(remarkPlugins || [])
106-
.use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes})
110+
.use(remarkRehype, {
111+
...remarkRehypeOptions,
112+
allowDangerousHtml: true,
113+
/* c8 ignore next */
114+
passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes]
115+
})
107116
.use(rehypePlugins || [])
108117

109118
if (format === 'md') {

Diff for: ‎packages/mdx/readme.md

+19
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ List of recma plugins.
193193
This is a new ecosystem, currently in beta, to transform [esast][] trees
194194
(JavaScript).
195195

196+
###### `options.remarkRehypeOptions`
197+
198+
Options to pass through to [`remark-rehype`][remark-rehype].
199+
The option `allowDangerousHtml` will always be set to `true` and the MDX nodes
200+
are passed through.
201+
In particular, you might want to pass `clobberPrefix`, `footnoteLabel`, and
202+
`footnoteBackLabel`.
203+
204+
<details>
205+
<summary>Example</summary>
206+
207+
```js
208+
compile({value: ''}, {remarkRehypeOptions: {clobberPrefix: 'comment-1'}})
209+
```
210+
211+
</details>
212+
196213
###### `options.mdExtensions`
197214

198215
List of markdown extensions, with dot (`Array<string>`, default: `['.md',
@@ -1061,6 +1078,8 @@ abide by its terms.
10611078

10621079
[rehype-plugins]: https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins
10631080

1081+
[remark-rehype]: https://github.com/remarkjs/remark-rehype
1082+
10641083
[mdx-syntax]: https://mdxjs.com/docs/what-is-mdx/#mdx-syntax
10651084

10661085
[use]: #use

Diff for: ‎packages/mdx/test/compile.js

+27
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,33 @@ test('markdown (math, `remark-math`, `rehype-katex`)', async () => {
10801080
)
10811081
})
10821082

1083+
test('remark-rehype options', async () => {
1084+
assert.equal(
1085+
renderToStaticMarkup(
1086+
React.createElement(
1087+
await run(
1088+
compileSync('Text[^1]\n\n[^1]: Note.', {
1089+
remarkPlugins: [remarkGfm],
1090+
remarkRehypeOptions: {
1091+
footnoteLabel: 'Notes',
1092+
footnoteBackLabel: 'Back'
1093+
}
1094+
})
1095+
)
1096+
)
1097+
),
1098+
`<p>Text<sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref="true" aria-describedby="footnote-label">1</a></sup></p>
1099+
<section data-footnotes="true" class="footnotes"><h2 id="footnote-label" class="sr-only">Notes</h2>
1100+
<ol>
1101+
<li id="user-content-fn-1">
1102+
<p>Note. <a href="#user-content-fnref-1" data-footnote-backref="true" class="data-footnote-backref" aria-label="Back">↩</a></p>
1103+
</li>
1104+
</ol>
1105+
</section>`,
1106+
'should pass options to remark-rehype'
1107+
)
1108+
})
1109+
10831110
test('MDX (JSX)', async () => {
10841111
assert.equal(
10851112
renderToStaticMarkup(

1 commit comments

Comments
 (1)

vercel[bot] commented on Feb 9, 2022

@vercel[bot]

Successfully deployed to the following URLs:

Please sign in to comment.