Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Unwrap multiple image nodes #1

Closed
cedricdelpoux opened this issue Aug 12, 2018 · 5 comments
Closed

Unwrap multiple image nodes #1

cedricdelpoux opened this issue Aug 12, 2018 · 5 comments
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 🦋 type/enhancement This is great to have

Comments

@cedricdelpoux
Copy link

Hello, I was trying to make a gatsby plugin to unwrap images and I found your plugin. Thank you for that. What about unwrap multiple images ?

If my markdown looks like:

![](img1)
![](img2)

The generated output would be:

<p>
   <img />
   <img />
</p>

I tried

visit(markdownAST, `image`, (node, index, parent) => {
    const [firstChildNode, ...childNodes] = parent.children
    parent.type = firstChildNode.type
    parent.url = firstChildNode.url
    parent.alt = firstChildNode.alt
    parent.title = firstChildNode.title
    unwrappedImagesNodes.push(parent)

    childNodes.map(childNode => {
      unwrappedImagesNodes.push(childNode)
    })
  })

but I did not work. I very new to AST syntax and I don't find an util to create a node beside an other node. Because I replace the parent node with the first child node. But whaut about the other ones?

@johno
Copy link
Member

johno commented Aug 13, 2018

Yeah, I think what we'd need to do is check if all children in a node are images, and if they are we can slice in the images to replace the parent. We can do this by manipulating parent.parent.children (I think).

For now, though you can also separate the images with a newline. The rendered output will be the same as what you're desiring.

![](img1)

![](img2)

@johno johno added the 🦋 type/enhancement This is great to have label Aug 13, 2018
@johno johno changed the title unwrap multiples images nodes Unwrap multiple image nodes Aug 13, 2018
@cedricdelpoux
Copy link
Author

Do you know how can I replace a paragraph (single node) with it's children (multiples nodes) ? I could not found
The only way I found was to set

parent.type = "html"
parent.value = "<img/><img />"

But it's not a good solution because my images are not images node anymore so I'm breaking the following plugins looking for images

@wooorm wooorm closed this as completed in 0e4aa2c Jun 18, 2019
@wooorm
Copy link
Member

wooorm commented Jun 18, 2019

@johno I solved this in 0e4aa2c. I updated the whole repo to reflect the rest of the ecosystem as well so instead of a PR, could you review if the current behaviour makes sense? If so, we can cut a 1.0.0!

@johno
Copy link
Member

johno commented Jun 18, 2019

This lgtm! Nice work on the thorough tests. I'll add you as an owner to the package now.

@wooorm
Copy link
Member

wooorm commented Jun 18, 2019

Great! Released! 💯

@wooorm wooorm added ⛵️ status/released 🗄 area/interface This affects the public interface labels Aug 14, 2019
@wooorm wooorm added the 💪 phase/solved Post is done label Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 🦋 type/enhancement This is great to have
Development

No branches or pull requests

3 participants