Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PatrickJoannisse's avatar

Is there anything like MDX but with Livewire instead?

Coming from React and I'm kind of charmed by Livewire but I have a use case that's essentially content created with Markdown but sprinkled with Livewire components. Is that already a thing?

0 likes
2 replies
LaryAI's avatar
Level 58

As of my knowledge cutoff in early 2023, there isn't a direct equivalent of MDX (Markdown + JSX) for Laravel Livewire. However, you can achieve a similar result by parsing Markdown content and then rendering Livewire components within that content.

Here's a basic approach to achieve this:

  1. Parse the Markdown content and convert it to HTML.
  2. Identify placeholders within the Markdown that represent Livewire components.
  3. Replace those placeholders with the actual Livewire component renderings.

Here's a simple example of how you might implement this:

First, you would write your Markdown with some kind of identifiable placeholders for Livewire components. For example:

# My Article

This is a paragraph with some text.

[LivewireComponent:counter]

Another paragraph with more text.

Then, in your Livewire component or controller, you would parse the Markdown and replace the placeholders with Livewire components:

use Livewire\Component;
use Parsedown;

class MyComponent extends Component
{
    public $content;

    public function mount()
    {
        $markdown = <<<EOT
# My Article

This is a paragraph with some text.

[LivewireComponent:counter]

Another paragraph with more text.
EOT;

        $this->content = $this->parseContent($markdown);
    }

    private function parseContent($markdown)
    {
        // Parse the Markdown to HTML
        $parsedown = new Parsedown();
        $html = $parsedown->text($markdown);

        // Replace the Livewire component placeholders with actual component renderings
        $html = preg_replace_callback('/\[LivewireComponent:(.*?)\]/', function ($matches) {
            // Assuming you have a Livewire component named like the placeholder
            $componentName = $matches[1];
            return \Livewire\Livewire::mount($componentName)->html();
        }, $html);

        return $html;
    }

    public function render()
    {
        return view('livewire.my-component', ['content' => $this->content]);
    }
}

In your Blade view (livewire.my-component), you would then output the parsed content:

<div>
    {!! $content !!}
</div>

Please note that this is a simplified example. In a real-world scenario, you would need to handle security concerns (like XSS attacks) by sanitizing the HTML content and ensuring that only safe Livewire components can be embedded. You might also want to create a more robust system for defining and replacing placeholders with Livewire components.

Remember to include Livewire and Parsedown in your project:

composer require livewire/livewire
composer require erusev/parsedown

This approach allows you to mix Markdown content with Livewire components, achieving a result similar to what MDX does for React.

Please or to participate in this conversation.