Make Image as block level node? #941
-
Hello is it possible to somehow override image node functionality so it's considered as block level node instead of inline? Original extension code: https://github.com/simonvomeyser/commonmark-ext-lazy-image/blob/master/src/LazyImageRenderer.php Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You don't need to change the image node from an inline to a block, you just need to move it outside of its parent class RemoveImagesFromParagraphsListener
{
public function onDocumentParsed(DocumentParsedEvent $event): void
{
$document = $event->getDocument();
$images = (new Query())->where(Query::type(Image::class))->findAll();
foreach ($images as $image) {
if ($image->parent() instanceof Paragraph::class && $image->previous() === null && $image->next() === null) {
// This image is inside a paragraph that doesn't contain anything else, so remove that paragraph and put the image there instead
$image->parent()->replaceWith($image);
}
}
}
} See https://commonmark.thephpleague.com/2.3/customization/event-dispatcher/ for more information on working with events and https://commonmark.thephpleague.com/2.3/customization/abstract-syntax-tree/ for more details on working with the AST. Alternatively, you could override/decorate the |
Beta Was this translation helpful? Give feedback.
You don't need to change the image node from an inline to a block, you just need to move it outside of its parent
Paragraph
block and into the rootDocument
block. Something like this should work (untested):