AI crawlers and agents are increasingly consuming web content, but they don't need your full HTML—navigation menus, JavaScript bundles, and decorative markup are noise they have to work around. Spatie's Laravel Markdown Response package lets your Laravel app detect when a bot or agent is requesting a page and respond with a clean markdown version instead.
No changes to controllers or views are required. The package works at the middleware level, intercepting responses and converting the HTML to markdown before sending it back.
Installation
composer require spatie/laravel-markdown-response
The package auto-registers itself. To get started, wrap any routes you want to expose as markdown with the ProvideMarkdownResponse middleware:
use Spatie\MarkdownResponse\Middleware\ProvideMarkdownResponse; Route::middleware(ProvideMarkdownResponse::class)->group(function () { Route::get('/blog', [BlogController::class, 'index']); Route::get('/blog/{article}', [BlogController::class, 'show']); Route::get('/docs/{page}', [DocsController::class, 'show']);});
You can also apply the middleware globally if you want it to cover all routes.
How Detection Works
The middleware identifies markdown requests through three mechanisms:
- An
Accept: text/markdownHTTP header - A
.mdsuffix on the URL (e.g./blog.md) - Known AI bot user agents, including GPTBot and ClaudeBot
When a .md URL suffix is used, the package registers a RewriteMarkdownUrls middleware globally to strip the suffix before routing, so /blog.md resolves to your /blog route as normal, then gets converted on the way out.
You can also use PHP attributes directly on a controller class or method to control conversion without the middleware. #[ProvideMarkdown] opts a controller or action into markdown conversion, while #[DoNotProvideMarkdown] explicitly excludes it.
Conversion and Caching
By default, conversion is handled locally using the league/html-to-markdown driver, however, a Cloudflare Workers AI driver is also available as an alternative.
Converted responses are cached automatically. The default TTL is one hour, but both caching behavior and duration are configurable. To publish the config file:
php artisan vendor:publish --tag="markdown-response-config"
A few of the available options:
| Option | Default | Description |
|---|---|---|
enabled |
true |
Toggle markdown conversion on/off |
driver |
league |
league or cloudflare |
detect_via_accept_header |
true |
Respond to Accept: text/markdown |
detect_via_md_suffix |
true |
Respond to .md URL suffixes |
cache.enabled |
true |
Cache converted responses |
cache.ttl |
3600 |
Cache duration in seconds |
Direct Conversion
If you need to convert HTML to markdown outside of the middleware, the Markdown facade provides a direct interface:
use Spatie\MarkdownResponse\Facades\Markdown; $markdown = Markdown::convert($html);
You can find the full documentation at spatie.be/docs/laravel-markdown-response and the source code on GitHub.