Skip to content

Instantly share code, notes, and snippets.

@assertchris
Last active March 23, 2023 19:05
Show Gist options
  • Save assertchris/e898154ac834695b9a05039a84076c30 to your computer and use it in GitHub Desktop.
Save assertchris/e898154ac834695b9a05039a84076c30 to your computer and use it in GitHub Desktop.
<?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),
]
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment