Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing options to remark-rehype #1935

Merged
merged 8 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/mdx/lib/core.js
Expand Up @@ -4,6 +4,7 @@
* @typedef {import('./plugin/recma-document.js').RecmaDocumentOptions} RecmaDocumentOptions
* @typedef {import('./plugin/recma-stringify.js').RecmaStringifyOptions} RecmaStringifyOptions
* @typedef {import('./plugin/recma-jsx-rewrite.js').RecmaJsxRewriteOptions} RecmaJsxRewriteOptions
* @typedef {import('remark-rehype').Options} RemarkRehypeOptions
*
* @typedef BaseProcessorOptions
* @property {boolean} [jsx=false]
Expand All @@ -22,6 +23,8 @@
* List of remark (mdast, markdown) plugins.
* @property {PluggableList} [rehypePlugins]
* List of rehype (hast, HTML) plugins.
* @property {RemarkRehypeOptions} [remarkRehypeOptions]
* Options for remark-rehype plugin.
stefanprobst marked this conversation as resolved.
Show resolved Hide resolved
*
* @typedef {Omit<RecmaDocumentOptions & RecmaStringifyOptions & RecmaJsxRewriteOptions, 'outputFormat'>} PluginOptions
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
Expand Down Expand Up @@ -70,6 +73,7 @@ export function createProcessor(options = {}) {
recmaPlugins,
rehypePlugins,
remarkPlugins,
remarkRehypeOptions = {},
SourceMapGenerator,
...rest
} = options
Expand Down Expand Up @@ -103,7 +107,15 @@ export function createProcessor(options = {}) {
pipeline
.use(remarkMarkAndUnravel)
.use(remarkPlugins || [])
.use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes})
.use(remarkRehype, {
...remarkRehypeOptions,
allowDangerousHtml: true,
passThrough: Array.isArray(remarkRehypeOptions.passThrough)
? Array.from(
new Set([...remarkRehypeOptions.passThrough, ...nodeTypes])
)
: nodeTypes
stefanprobst marked this conversation as resolved.
Show resolved Hide resolved
})
.use(rehypePlugins || [])

if (format === 'md') {
Expand Down
27 changes: 27 additions & 0 deletions packages/mdx/test/compile.js
Expand Up @@ -1080,6 +1080,33 @@ test('markdown (math, `remark-math`, `rehype-katex`)', async () => {
)
})

test('remark-reyhpe options', async () => {
stefanprobst marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(
renderToStaticMarkup(
React.createElement(
await run(
compileSync('Text[^1]\n\n[^1]: Note.', {
remarkPlugins: [remarkGfm],
remarkRehypeOptions: {
footnoteLabel: 'Notes',
footnoteBackLabel: 'Back'
}
})
)
)
),
`<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>
<section data-footnotes="true" class="footnotes"><h2 id="footnote-label" class="sr-only">Notes</h2>
<ol>
<li id="user-content-fn-1">
<p>Note. <a href="#user-content-fnref-1" data-footnote-backref="true" class="data-footnote-backref" aria-label="Back">↩</a></p>
</li>
</ol>
</section>`,
'should pass options to remark-rehype'
)
})

test('MDX (JSX)', async () => {
assert.equal(
renderToStaticMarkup(
Expand Down