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

[v2] plugins not running with invalid HTML/JSX #1577

Closed
Ehesp opened this issue Jun 16, 2021 · 2 comments
Closed

[v2] plugins not running with invalid HTML/JSX #1577

Ehesp opened this issue Jun 16, 2021 · 2 comments
Labels
📚 area/docs This affects documentation 🙋 no/question This does not need any changes 💎 v2 Issues related to v2

Comments

@Ehesp
Copy link

Ehesp commented Jun 16, 2021

I've successfully been using next-mdx-remote on a project, so upgraded the library locally to v2 to reap the benefits. Everything is going well, however I have ran into an "issue" which I was able to circumvent in v1.

Im dealing with a situation where the MDX content is unknown, so I apply a few plugins to prevent misuse of the content.

For example, parsing the following MDX:

<img src="...">

<td>Foo</td>

For this to be valid JSX, it should have a closing slash. Running this via @mdx-js/mdx throws the following error:

Unexpected closing tag `</td>`, expected corresponding closing tag for `<img>`

In v1 I was able to write a quick plugin which scanned HTML elements and basically sanitised them:

const content = mdx('...', {
  remarkPlugins: [
    sanitizeHtml
  ]
});

This plugin looked something like:

function sanitizeHtml() {
  return tree => {
    visit(tree, 'jsx', visitor);
  };

  function visitor(node) {
    // A `jsx` node might be a HTML element from the Markdown, or a MDX
    // component. We only want to sanitize the HTML ones.
    if (isHtml(node.value)) {
      node.value = sanitizeHtml(node.value, {
        allowedTags: [...sanitizeHtml.defaults.allowedTags, 'img'],
        allowedAttributes: {
          ...sanitizeHtml.defaults.allowedAttributes,
          img: [...sanitizeHtml.defaults.allowedAttributes.img, 'alt', 'width', 'height'],
        },
      });
    }
  }
}

A very hacky solution, but it worked for my use-case. However, after upgrading to v2 the plugins are simply not executed and the error is thrown straight away, not allowing me to modify the tree before being sent to the MDX compiler.

Obviously writing correct HTML/JSX would be the first step, but thought I'd highlight an upgrade "issue".

Thanks

@Ehesp Ehesp changed the title [v2] plugins not running with invalid HTML [v2] plugins not running with invalid HTML/JSX Jun 16, 2021
@ChristianMurphy
Copy link
Member

Right, in MDX version 2, MD and JSX are parsed at the same time as part of #628
this is also noted in the MDX 2 release plan #1041

@ChristianMurphy ChristianMurphy added 💎 v2 Issues related to v2 📚 area/docs This affects documentation 🙋 no/question This does not need any changes and removed 🐛 type/bug This is a problem 🔍 status/open labels Jun 16, 2021
@Ehesp
Copy link
Author

Ehesp commented Jun 16, 2021

Thanks - makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 area/docs This affects documentation 🙋 no/question This does not need any changes 💎 v2 Issues related to v2
Development

No branches or pull requests

2 participants