spatie/laravel-markdown-response
Serve clean markdown versions of your Laravel HTML pages for AI agents and bots. Detects requests via Accept: text/markdown, known user agents, or .md URLs. Driver-based conversion (local PHP or Cloudflare Workers AI), caching, and HTML preprocessing included.
Pros:
Markdown::fake()).Cons:
markdown-response: prefix recommended).@prepend directives) or custom drivers may be needed.| Risk Area | Severity | Mitigation |
|---|---|---|
| Conversion accuracy | Medium | Start with League driver; switch to Cloudflare for complex pages. |
| Cache stampedes | Low | Use dedicated cache store (e.g., markdown-response) or aggressive TTLs. |
| Performance regression | Low | Benchmark with MARKDOWN_RESPONSE_CACHE_ENABLED=false to isolate overhead. |
| Attribute routing bugs | Medium | Test edge cases (e.g., method-level attributes overriding class-level). |
| Cloudflare dependency | High | Only enable if using Cloudflare; fallback to League driver in tests. |
| Testing complexity | Low | Leverage Markdown::fake() for unit tests; mock cache in integration tests. |
ProvideMarkdownResponse).Markdown facade for direct conversion (e.g., in jobs, commands).Markdown::fake()) align with Laravel’s Pest/PHPUnit support.Phase 1: Pilot Routes (Low Risk)
/docs, /blog).Accept: text/markdown).Phase 2: Global Rollout (Medium Risk)
bootstrap/app.php./dashboard, /api/*) using DoNotProvideMarkdownResponse.Phase 3: Optimization (High Value)
/products with JS rendering).| Component | Compatibility | Notes |
|---|---|---|
| Laravel Version | 9.x, 10.x, 11.x | Test with your specific version (e.g., 10.30+ for attribute support). |
| PHP Version | 8.1+ | Check for deprecated functions if using PHP < 8.1. |
| Cache Stores | Any Laravel-supported store (Redis, database, etc.) | Dedicated store recommended for shared environments. |
| HTML Structure | Semantic HTML preferred; inline styles/JS may reduce quality. | Preprocess templates if needed (e.g., remove style attributes). |
| AI User Agents | Supports known bots (GPTBot, ClaudeBot); customizable via ai_user_agents config. |
Update config if new AI bots emerge. |
| Testing Frameworks | Pest, PHPUnit | Markdown::fake() works with both. |
Pre-requisites:
Core Implementation:
composer require spatie/laravel-markdown-response.php artisan vendor:publish --tag="markdown-response-config".Testing:
Markdown::fake() to verify conversions..md URLs, Accept headers, and AI user agents.Post-Launch:
#[DoNotProvideMarkdown] to sensitive endpoints).php artisan markdown-response:clear).ai_user_agents config if new bots emerge (e.g., SerperBot).Accept header is set, or URL ends with .md.curl -H "Accept: text/markdown" http://app.com/about).#[ProvideMarkdown]) and facade usage (Markdown::convert()).How can I help you explore Laravel packages today?