Serve Markdown Versions of Your Laravel Pages to AI Agents

Last updated on by

Serve Markdown Versions of Your Laravel Pages to AI Agents image

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/markdown HTTP header
  • A .md suffix 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.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

The latest

View all →
Laravel Quota: Usage Budgets for Calendar Periods image

Laravel Quota: Usage Budgets for Calendar Periods

Read article
Find the Security Vulnerabilities in Your Laravel App with Sensagraph image

Find the Security Vulnerabilities in Your Laravel App with Sensagraph

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
Passwordless Sign-In with Fortify Two-Factor Support in Laravel image

Passwordless Sign-In with Fortify Two-Factor Support in Laravel

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
AI Review for Laravel Upgrades image

AI Review for Laravel Upgrades

Read article