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

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
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Tinkerwell logo

Tinkerwell

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

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

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

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

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

PhpStorm

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

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
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
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
Lucky Media logo

Lucky Media

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

Lucky Media
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

The latest

View all →
New Expressive Model Attributes in Laravel 13.2.0 image

New Expressive Model Attributes in Laravel 13.2.0

Read article
Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More image

Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More

Read article
Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill image

Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill

Read article
Building Transaction-Safe Multi-Document Operations in Laravel image

Building Transaction-Safe Multi-Document Operations in Laravel

Read article
Ship AI with Laravel: Building Your First Agent with Laravel 13's AI SDK image

Ship AI with Laravel: Building Your First Agent with Laravel 13's AI SDK

Read article
OG Kit: Generate Dynamic Open Graph Images with HTML and CSS image

OG Kit: Generate Dynamic Open Graph Images with HTML and CSS

Read article