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

Unwrap figure elements #11

Closed
4 tasks done
aKaradzhov97 opened this issue May 8, 2024 · 9 comments
Closed
4 tasks done

Unwrap figure elements #11

aKaradzhov97 opened this issue May 8, 2024 · 9 comments
Labels
🤷 no/invalid This cannot be acted upon 👎 phase/no Post cannot or will not be acted on

Comments

@aKaradzhov97
Copy link

aKaradzhov97 commented May 8, 2024

Initial checklist

Problem

I have a NextJS app, which makes use of MDX.
The images rendered in that app are wrapped in figure element. By default, these elements are wrapped in a paragraph. Since, figure is closely related to img, I thought we could add support for figure elements also here, because this package solves the same issue.

When rendering figure elements in MDX, they're wrapped in a paragraph producing hydration errors (NextJS).

Solution

Simply unwrap also figure elements.

Alternatives

I cannot think of any.

@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 May 8, 2024
@wooorm
Copy link
Member

wooorm commented May 8, 2024

Please spend more time framing your question. It is unclear what you want. See https://github.com/remarkjs/.github/blob/main/support.md#asking-quality-questions

@wooorm wooorm added the 🙉 open/needs-info This needs some more info label May 8, 2024

This comment has been minimized.

@aKaradzhov97
Copy link
Author

Sorry, I've improved my explanation of the problem. Does that make sense?

@wooorm
Copy link
Member

wooorm commented May 8, 2024

The images rendered in that app are wrapped in figure element.

How?

When rendering figure elements in MDX, they're wrapped in a paragraph producing hydration errors (NextJS).

Please share the MDX file you have and share the errors you see.
Share the actual input you have, and share what the expected output you’d like to get is.

@aKaradzhov97
Copy link
Author

aKaradzhov97 commented May 10, 2024

It's a custom implementation - I am passing a custom implementation of the img element, in which I wrap the img tag with a figure and add a figcaption. Here's what I do:

import Picture from "./components/Picture";

export const MDXComponentsMap = {
  img: Picture,
  ... // omitted for the sake of brevity
}

// "components/Picture"
const Picture = ({ src, alt = "", title, className }: Props): JSX.Element => (
  <StyledFigure className={className}>
    <img src={src} alt={alt} />
    {title && <figcaption>{title}</figcaption>}
  </StyledFigure>
);

Then in my MDX, I do something like this:

<img src"..." title="Caption" />

And the output is:

<p>
  <figure>
    <img src"..." />
    <figcaption>Caption</figcaption>
  </figure>
</p>

While the desired output should look like this:

<figure>
  <img src"..." />
  <figcaption>Caption</figcaption>
</figure>

Does that make sense?

@wooorm
Copy link
Member

wooorm commented May 10, 2024

You don’t need anything. This plugin already does what you want.

This plugin works on the markdown/MDX AST.
Your component overwrite works much later, at the final React rendering.
So this plugin doesn’t works earlier. It is unrelated to your component.

@wooorm wooorm closed this as completed May 10, 2024
@wooorm wooorm added the 🤷 no/invalid This cannot be acted upon label May 10, 2024

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 🙉 open/needs-info This needs some more info labels May 10, 2024
@aKaradzhov97
Copy link
Author

Actually, it works for me and my use case and I believe it will also work for others too.
I added the following in the node_modules folder:

else if (
  child.type === 'image' ||
  child.type === 'figure' || // This
  child.type === 'imageReference'
)

@remcohaszing
Copy link
Member

There is no mdast node of type figure. You’re confusing mdast nodes with HTML or JSX elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤷 no/invalid This cannot be acted upon 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

3 participants