hylia
hylia copied to clipboard
Transform adds figure to paragraph
The transformation of article images to figures replaces the image with a figure
and figcaption
. Now figure
s are not allowed in p
tags, which by default is the parent of the image. Some browsers will render empty p
tags above and below the figure
, which leads to additional unwanted space.
I fixed this locally by changing the transformation to this:
image.parentElement.replaceWith(figure)
Is the assumption ok that every image (from markdown content) will have a surrounding p
tag? Or would it be better to test for the parent and its content?
I've had this issue as well. Thanks for investigating @thomasmassmann :bouquet:
I'm getting closer to what I want. If you have images with text before/after it, the text in my first patch is gone. Now the image is removed from the parent (p
tag) and then added right after the parent if there is still some text in it.
figure.appendChild(image.cloneNode(true));
figure.appendChild(figCaption);
const parent = image.parentElement;
parent.removeChild(image);
if (parent.innerHTML.length) {
parent.parentNode.insertBefore(figure, parent.nextSibling);
} else {
parent.replaceWith(figure);
}
Now when you have more than one inline image it would be great to split the parent and have the new figure sit in between. Any hints?
Origanal behavior

becomes
<p>
<figure>...</figure>
</p>
which browsers will render as
<p></p>
<figure>...</figure>
<p></p>
With current patch
Image only

becomes
<figure>...</figure>
Image inline with text
Some text before.

Some text after.
becomes
<p>Some text before. Some text after.</p>
<figure>...</figure>
Multiple image inline with text
Some text before.

Some text after.

End of text.
becomes
<p>Some text before. Some text after. End of text.</p>
<figure>...</figure>
<figure>...</figure>
But it should be
<p>Some text before.</p>
<figure>...</figure>
<p>Some text after.</p>
<figure>...</figure>
<p>End of text.</p>
I appreciate the work going into this. I'm a bit stacked to be thinking about it right now, but it's certainly one to keep an eye on.
Sure, that's nothing super critical to worry about right now. I want to discuss things first and see what possible problems and side effects are possible before creating a PR. And also get some feedback from others who use hylia for their projects.
Thank you, Thomas. I'm really glad you spotted this because I certainly don't want to be shipping bad HTML :)