Laravel Packages

Serve Markdown Versions of Your Laravel Pages to AI Agents

Published
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.

Sponsored

acquaintsoft logo
Acquaint Softtech

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

Visit Acquaint Softtech

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article