remarkjs / remark-rehype

plugin that turns markdown into HTML to support rehype

Home Page:https://remark.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Options passed via `unified-engine` are not working

akrawitz opened this issue · comments

Initial checklist

Affected packages and versions

remark-rehype: 11.0.0; unified-engine: 11.2.0

Link to runnable example

https://codesandbox.io/p/sandbox/remark-rehype-issue-jzrg2s

Steps to reproduce

(I'm running Node 21.2.0 & Yarn 4.0.2, but the sandbox is running 20.9.0 & Yarn 1.22.19 and shows the same issue).

Please use the sandbox linked above.
The markdown input is in in.md.
Run yarn example1 in the sandbox terminal. The code being executed is in example1.js.
The output is written to out1.html.

Since allowDangerousHtml: true is passed to both remark-rehype and rehype-stringify, the raw html in the markdown should appear in the resulting HTML. However, the options are not being handled properly by remark-rehype. This issue is specific to using unified-engine, because it passes an additional parameter with a FileSet to all plugins (https://github.com/unifiedjs/unified-engine/tree/main?tab=readme-ov-file#fileset). This is normally ignored, but remark-rehype has two optional parameters destination and options, so the options are assigned to destination and the FileSet is assigned to options. As a result, line 151 in remark-rehype/lib/index.js ignores the actual options (which are in destination):

return /** @type {HastRoot} */ (toHast(tree, options || destination))

A workaround is shown by yarn example2. The code being executed is in example2.js, and the output is written to out2.html.
The key to the workaround is that I've wrapped remark-rehype in a function call that reassigns the parameters:

(options, fileSet) => { return remarkRehype({}, options, fileSet); }

This works because now the empty object gets assigned to destination and the options get assigned to options. This workaround helps to confirm that the issue is with how options are handled by the combination of unified-engine and remark-rehype.

Expected behavior

In out1.html, the raw HTML from in.md should be included:

<h1>Header 1</h1>
<p>Paragraph 1.</p>
<div>Raw HTML</div>
<h2>Header 2</h2>
<p>Paragraph 2.</p>

Actual behavior

In out1.html, the raw HTML from in.md is not included:

<h1>Header 1</h1>
<p>Paragraph 1.</p>
<h2>Header 2</h2>
<p>Paragraph 2.</p>

Runtime

Other (please specify in steps to reproduce)

Package manager

Other (please specify in steps to reproduce)

OS

Windows

Build and bundle tools

Rollup

Hey!

Ah, right. It could be interesting to see if the previous majors of those plugins had the same problem or not. They had more handling around the parameters.

I’d say for a solution there also needs to be better handling in this project, remark-rehype, around a given file set

Hi! This was marked as ready to be worked on! Note that while this is ready to be worked on, nothing is said about priority: it may take a while for this to be solved.

Is this something you can and want to work on?

Team: please use the area/* (to describe the scope of the change), platform/* (if this is related to a specific one), and semver/* and type/* labels to annotate this. If this is first-timers friendly, add good first issue and if this could use help, add help wanted.