spatie/laravel-markdown-response
Serve markdown versions of your Laravel HTML pages for AI agents and bots. Detect markdown requests via Accept: text/markdown, known user agents, or .md URLs. Driver-based conversion (local PHP or Cloudflare Workers AI), with caching and HTML preprocessing.
By default, the package converts HTML to markdown locally using league/html-to-markdown. If you need better conversion quality or JavaScript rendering support, you can switch to an external driver.
Set the driver via the MARKDOWN_RESPONSE_DRIVER environment variable.
The League driver runs locally, requires no external services, and works out of the box.
MARKDOWN_RESPONSE_DRIVER=league
Options are passed directly to the HtmlConverter constructor. See the league/html-to-markdown documentation for available options.
// config/markdown-response.php
'driver_options' => [
'league' => [
'options' => [
'strip_tags' => true,
'hard_break' => true,
],
],
],
The Cloudflare driver uses the Workers AI API to convert HTML to markdown server-side. It sends the HTML as a file upload and returns the converted markdown.
MARKDOWN_RESPONSE_DRIVER=cloudflare
CLOUDFLARE_ACCOUNT_ID=your-account-id
CLOUDFLARE_API_TOKEN=your-api-token
To get your credentials:
You can create your own driver by implementing the MarkdownDriver interface:
namespace App\Drivers;
use Spatie\MarkdownResponse\Drivers\MarkdownDriver;
class PandocDriver implements MarkdownDriver
{
public function convert(string $html): string
{
// Your conversion logic here
}
}
Then bind it to the MarkdownDriver interface in a service provider:
use App\Drivers\PandocDriver;
use Spatie\MarkdownResponse\Drivers\MarkdownDriver;
$this->app->singleton(MarkdownDriver::class, PandocDriver::class);
You can switch drivers on a per-conversion basis using the using() method:
use Spatie\MarkdownResponse\Facades\Markdown;
$markdown = Markdown::using('cloudflare')->convert($html);
How can I help you explore Laravel packages today?