---
title: Fixing images on Laravel Vapor with League\Commonmark
url: "https://aaronfrancis.com/writing/fixing-images-on-laravel-vapor-with-leaguecommonmark"
path: "/writing/fixing-images-on-laravel-vapor-with-leaguecommonmark"
type: technical_article
author: Aaron Francis
published: 2021-07-20
updated: 2026-11-10
topic: "Laravel & PHP"
description: Writing a Commonmark extension to so your images render on Laravel Vapor
keywords: [laravel vapor, commonmark, markdown extension, image urls, asset helper, s3, cloudfront, document parsing, php league, environment variables, graham campbell, markdown package]
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Fixing images on Laravel Vapor with League\Commonmark

If you are using The PHP League's terrific [Commonmark markdown parsing library](https://github.com/thephpleague/commonmark) _and_ hosting your site on [Laravel Vapor](https://vapor.laravel.com/), then you may be running into an issue where your images are not showing up.

[Vapor's documentation](https://docs.vapor.build/1.0/projects/deployments.html#assets) states that you must use the `asset` helper to generate the correct URLs for your images:

> Because all of your assets will be served via S3 / CloudFront, you should always generate URLs to these assets using Laravel's asset helper. Vapor injects an `ASSET_URL` environment variable which Laravel's asset helper will use when constructing your URLs.

When you're working in markdown it's not quite that easy though, because you don't have access to the `asset` helper in markdown.

The solution is to write a Commonmark extension to do modify the URL on the fly, as the document is being parsed.

If you aren't familiar with Commonmark extensions, they are a convenient way for you to add functionality to the markdown parser. There are a number of [first party](https://commonmark.thephpleague.com/1.6/extensions/overview/) ones, as well as many, many community authored ones.

We're going to write one here to look for image nodes and wrap the URL in the `asset` helper.

## Writing the Extension

The first step is to create the extension class. We'll call it `VaporAssetWrapping` and implement the `ExtensionInterface`.

VaporAssetWrapping.php 
```php
<?php
namespace App;

use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Extension\ExtensionInterface;

class VaporAssetWrapping implements ExtensionInterface
{
    public function register(ConfigurableEnvironmentInterface $environment)
    {
        // @TODO
    }
}
``` 

There are a [number of things](https://commonmark.thephpleague.com/1.6/customization/overview/) you can do with an extension, we are specifically going to listen for the `DocumentParsedEvent` and look for image nodes. 

The `register` method is the entry point, where we're going to add our listener. We'll also set up an empty method that will contain all of our logic.

```php
<?php
namespace App;

use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;

class VaporAssetWrapping implements ExtensionInterface
{
    public function register(ConfigurableEnvironmentInterface $environment)
    {
        $environment->addEventListener(DocumentParsedEvent::class, [$this, 'onDocumentParsed']); 
    }

    public function onDocumentParsed(DocumentParsedEvent $event)
    {
        // @TODO
    }

}
``` 

Once the document is parsed we're going to walk through all the nodes and look for images. We can do this by comparing the current node to the `Image` class that Commonmark provides. 

As we're walking the document we going to do a few things:
 
 - See if the node is an image. If not, skip it.
 - Only stop as we're entering the node. 
 - See if the image source is external by checking to see if it starts with `http`. Skip if so.
 - Wrap the relative url in the `asset` helper and update the node.
 
```php
<?php
namespace App;

use Illuminate\Support\Str;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;
use League\CommonMark\Inline\Element\Image;

class VaporAssetWrapping implements ExtensionInterface
{
    public function register(ConfigurableEnvironmentInterface $environment)
    {
        $environment->addEventListener(DocumentParsedEvent::class, [$this, 'onDocumentParsed']);
    }

    public function onDocumentParsed(DocumentParsedEvent $event)
    {
        $walker = $event->getDocument()->walker(); 

        while ($event = $walker->next()) {
            $node = $event->getNode();

            // Only look for image nodes, and only process them upon entering.
            if (!$node instanceof Image || !$event->isEntering()) {
                continue;
            }

            // If it's an absolute URL, skip it.
            if (Str::startsWith($node->getUrl(), 'http')) {
                continue;
            }

            // Pass it through to the `asset` helper.
            $node->setUrl(asset($node->getUrl()));
        } 
    }
}
```

## Registering the Extension

Now that you have your extension all written, you need to register it with the Commonmark parser.

If you are using [Graham Campbell's markdown package](https://github.com/GrahamCampbell/Laravel-Markdown) then you just need to add it to your `extensions` key in the `markdown.php` file.

config/markdown.php 
```php
return [
    'extensions' => [
        // The location of our static assets varies based on wherever
        // Vapor puts them. This will point them to the right spot.
        VaporAssetWrapping::class,
    ],
];
```

If you aren't, then you'll need to manually register it:

```php
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new VaporAssetWrapping);
```

That's all you need to do! Let me know [on twitter](https://twitter.com/aarondfrancis) if it worked for you!

---

## Related on aaronfrancis.com

- [Deploying Torchlight](/podcast/deploying-torchlight-gkdczl) *(Podcast · 2022-10-05 · shared: laravel vapor, s3)*
- [Hammerstone, a History](/podcast/hammerstone-a-history-2f8ad77f) *(Podcast · 2021-09-17 · shared: laravel vapor, commonmark)*
- [Blogging with markdown in Laravel](/writing/blogging-with-markdown-in-laravel) *(Article · 2021-04-08 · shared: commonmark, laravel vapor)*
- [Yii and the asset pipeline: part 2](/writing/yii-and-the-asset-pipeline-part-2) *(Article · 2013-06-19 · shared: s3, cloudfront)*
- [Yii, Heroku, and the asset pipeline](/writing/yii-heroku-and-the-asset-pipeline) *(Article · 2013-04-09 · shared: s3, cloudfront)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `laravel vapor` ([K-L](/llms/keywords/k-l.txt)), `commonmark` ([C](/llms/keywords/c.txt)), `markdown extension` ([M-N](/llms/keywords/m-n.txt)), `image urls` ([H-J](/llms/keywords/h-j.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
