From aff6de4fad5955b804d51cd109248425d786fb1c Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Wed, 9 Feb 2022 10:32:27 +0100 Subject: [PATCH] Add support for passing options to `remark-rehype` (#1935) Reviewed-by: Christian Murphy Reviewed-by: Titus Wormer --- packages/mdx/lib/core.js | 11 ++++++++++- packages/mdx/readme.md | 19 +++++++++++++++++++ packages/mdx/test/compile.js | 27 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/mdx/lib/core.js b/packages/mdx/lib/core.js index ef5bf55f3..96e34d709 100644 --- a/packages/mdx/lib/core.js +++ b/packages/mdx/lib/core.js @@ -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] @@ -22,6 +23,8 @@ * List of remark (mdast, markdown) plugins. * @property {PluggableList} [rehypePlugins] * List of rehype (hast, HTML) plugins. + * @property {RemarkRehypeOptions} [remarkRehypeOptions] + * Options to pass through to `remark-rehype`. * * @typedef {Omit} PluginOptions * @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions @@ -70,6 +73,7 @@ export function createProcessor(options = {}) { recmaPlugins, rehypePlugins, remarkPlugins, + remarkRehypeOptions = {}, SourceMapGenerator, ...rest } = options @@ -103,7 +107,12 @@ export function createProcessor(options = {}) { pipeline .use(remarkMarkAndUnravel) .use(remarkPlugins || []) - .use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes}) + .use(remarkRehype, { + ...remarkRehypeOptions, + allowDangerousHtml: true, + /* c8 ignore next */ + passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes] + }) .use(rehypePlugins || []) if (format === 'md') { diff --git a/packages/mdx/readme.md b/packages/mdx/readme.md index 2444cd2a0..17448e9d3 100644 --- a/packages/mdx/readme.md +++ b/packages/mdx/readme.md @@ -193,6 +193,23 @@ List of recma plugins. This is a new ecosystem, currently in beta, to transform [esast][] trees (JavaScript). +###### `options.remarkRehypeOptions` + +Options to pass through to [`remark-rehype`][remark-rehype]. +The option `allowDangerousHtml` will always be set to `true` and the MDX nodes +are passed through. +In particular, you might want to pass `clobberPrefix`, `footnoteLabel`, and +`footnoteBackLabel`. + +
+Example + +```js +compile({value: '…'}, {remarkRehypeOptions: {clobberPrefix: 'comment-1'}}) +``` + +
+ ###### `options.mdExtensions` List of markdown extensions, with dot (`Array`, default: `['.md', @@ -1061,6 +1078,8 @@ abide by its terms. [rehype-plugins]: https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins +[remark-rehype]: https://github.com/remarkjs/remark-rehype + [mdx-syntax]: https://mdxjs.com/docs/what-is-mdx/#mdx-syntax [use]: #use diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js index ba382d965..9b19012a9 100644 --- a/packages/mdx/test/compile.js +++ b/packages/mdx/test/compile.js @@ -1080,6 +1080,33 @@ test('markdown (math, `remark-math`, `rehype-katex`)', async () => { ) }) +test('remark-rehype options', async () => { + assert.equal( + renderToStaticMarkup( + React.createElement( + await run( + compileSync('Text[^1]\n\n[^1]: Note.', { + remarkPlugins: [remarkGfm], + remarkRehypeOptions: { + footnoteLabel: 'Notes', + footnoteBackLabel: 'Back' + } + }) + ) + ) + ), + `

Text1

+

Notes

+
    +
  1. +

    Note.

    +
  2. +
+
`, + 'should pass options to remark-rehype' + ) +}) + test('MDX (JSX)', async () => { assert.equal( renderToStaticMarkup(