example.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$converter->when( | |
fn(Heading $node) => // => reflection-based overloading | |
HtmlElement::fromString( // => complex types from string template syntax | |
'<h1 class="font-2xl">:text</h1>', | |
['text' => $node->text] | |
) | |
); | |
$converter->convert('# hello world'); // => <h1 class="font-2xl">hello world</h1> | |
// benefits: | |
// - very easy to set up | |
// - don't need to know how to manually create custom renderers or about the internal structure | |
// - can further manipulate complex types once the basics are bootstrapped using this approach | |
$converter->when( | |
fn(Heading $node) => HtmlElement::fromString( | |
match ($node->level) { | |
default => '<h1 class="font-2xl">:text</h1>', | |
2 => '<span><a href="#:id">link</a><h2 class="font-xl">:text</h2></span>', | |
3 => '<h3 id=":id" class="font-lg">:text</h3>', | |
}, | |
[ | |
'text' => $node->text, | |
'id' => slug($node->text), | |
] | |
) | |
); |