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

rehype-attr plugin not working #758

Closed
4 tasks done
eoinmurphy opened this issue Jul 31, 2023 · 5 comments
Closed
4 tasks done

rehype-attr plugin not working #758

eoinmurphy opened this issue Jul 31, 2023 · 5 comments
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on

Comments

@eoinmurphy
Copy link

eoinmurphy commented Jul 31, 2023

Initial checklist

Affected packages and versions

8.0.7

Link to runnable example

https://codesandbox.io/p/sandbox/bold-water-s8v68v?file=%2Fsrc%2FApp.js%3A14%2C1

Steps to reproduce

(This example is based on the rehype-attr examples)

Attempt to render the below Markdown string using react-markdown with the rehype-attr plugin:

import ReactMarkdown from "react-markdown";
import rehypeAttrs from "rehype-attr";

// Example: https://github.com/jaywcjlove/rehype-attr#markdown-example-3
const markdown = `Npm stands for *node* <!--rehype:style=color: red--> package manager.`;

export default function App() {
  return (
    <ReactMarkdown rehypePlugins={[rehypeAttrs, { properties: "attr" }]}>
      {markdown}
    </ReactMarkdown>
  );
}

Runtime: Node 18.12.0

I did search for past issues related to this, but couldn't find any.

Expected behavior

I expect the HTML output of this parsed Markdown to be

<p>Npm stands for <em style="color: red">node</em><!--rehype:style=color: red--> package manager.</p>

Actual behavior

The following is rendered in the body:

Npm stands for node <!--rehype:style=color: red--> package manager.

Looking at the outputted HTML:

<p>Npm stands for node 
    <!-- -->
    <!--rehype:style=color: red--&>
    <!-- --> 
    package manager.
</p>

It seems like it's trying to take the HTML comment into account, but renders two empty comments within the <p> tag instead.

Note: I'm getting the Markdown from Strapi and then passing the body of that API response into react-markdown. I've inspected the values received from the API in the console and all looks to be as expected:

{
  id: 7,
  attributes: {
    title: 'Rehype Test',
    date: '2023-07-31',
    body: 'Npm stands for *node* <!--rehype:style=color: red--> package manager.',
    slug: 'rehype-test',
    createdAt: '2023-07-31T14:17:25.804Z',
    updatedAt: '2023-07-31T14:49:18.733Z',
    publishedAt: '2023-07-31T14:17:27.021Z',
    author: 'Eoin Murphy',
}

Runtime

No response

Package manager

yarn 1

OS

macOS

Build and bundle tools

Next.js

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Jul 31, 2023
@ChristianMurphy
Copy link
Member

Welcome @eoinmurphy! 👋
rehype-attr relies on HTML embedded in markdown which you have disabled in your example.

To enable HTML see https://github.com/remarkjs/react-markdown#appendix-a-html-in-markdown
If you choose to do so, also see security considerations for embedding HTML https://github.com/remarkjs/react-markdown#security

@ChristianMurphy ChristianMurphy closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2023
@ChristianMurphy ChristianMurphy added the 🙋 no/question This does not need any changes label Jul 31, 2023
@github-actions

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually labels Jul 31, 2023
@eoinmurphy
Copy link
Author

Thanks for the quick response, @ChristianMurphy! I've just added rehype-raw, but the attribute I expect is still not being added to the rendered output. 🤔

Here's what's in my Markdown editor in Strapi:

1.
Npm stands for *node* <!--rehype:style=color: red--> package manager.

2.
Npm stands for <em class="test">node</em> package manager.

3.
Npm stands for *node*<!--rehype:class=test--> package manager.

But each result in the following:

<!-- 1 -->
<p>Npm stands for <em>node</em> <!-- --> package manager.</p>

<!-- 2, 3 -->
<p>Npm stands for <em>node</em> package manager.</p>

I've also updated the CodeSandbox to show this behavior.

Am I missing something silly here?

@ChristianMurphy
Copy link
Member

rehype-attr relies on parsed HTML (a hast tree), rehype-raw generates that parsed hast tree.
rehype-raw needs to run before rehype-attr, but you are running it after.

@eoinmurphy
Copy link
Author

Ah, that was it, I have it working now on my project. I also ran rehype-sanitize before which stripped it too, I guess. Will spend some time with rehype-sanitize to ensure that this is as secure as possible.

Thanks so much for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

2 participants