-
QuestionHi, I've been struggling to properly implement a simple filter on all text nodes. Quite straightforward with a regular expression, but I can't modify the string content with a Listener. I've been messing around inline or block renderers, but it looks like I would have to make a custom renderer for every block. Is there a way to achieve this easily? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Maybe you could try adding an event listener that responds to the Here's some quick and dirty code I wrote without my IDE that might have bugs, but should get you close: <?php
class AddNonBreakingSpacesBeforeColonsProcessor
{
public function onDocumentParsed(DocumentParsedEvent $event)
{
$document = $event->getDocument();
$walker = $document->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
if (!($node instanceof Text) || !$event->isEntering()) {
continue;
}
$content = $node->getContent();
// TODO: Perform your regex find-and-replace here on $content
$node->setContent($content);
}
}
} Then add it to your $environment->addEventListener(DocumentParsedEvent::class, [new AddNonBreakingSpacesBeforeColonsProcessor(), 'onDocumentParsed']); I hope that helps! |
Beta Was this translation helpful? Give feedback.
-
Hi Colin, thank you for answering. I missed this Element class, which is quite obvious. I'm not sure I can use it though, as the text node content get escaped in the renderer, so I would need to split the text node in two, with an additional Inline node between. Looks difficult to do. I will try to add a pre renderer on the whole markdown input, or have a custom Document renderer. Will post my findings here. |
Beta Was this translation helpful? Give feedback.
-
Here's what I did: In the Extension:
TypographyRenderer
Any other little tweak I'll need will go there, gotta be careful on the regular expression though! |
Beta Was this translation helpful? Give feedback.
Here's what I did:
In the Extension:
TypographyRenderer
Any other little tweak I'll need will go there,…