spatie/laravel-markdown-response
Serve markdown versions of your Laravel HTML pages for AI agents and bots. Detect markdown requests via Accept: text/markdown, known user agents, or .md URLs. Driver-based conversion (local PHP or Cloudflare Workers AI), with caching and HTML preprocessing.
Before HTML is converted to markdown, it runs through preprocessors that strip elements which don't belong in a markdown document, like scripts, navigation menus, or advertising.
The package ships with four preprocessors:
RemoveScriptsAndStylesPreprocessor: strips <script> tags, <style> tags, and stylesheet <link> tags. Enabled by default.RemoveNavigationPreprocessor: strips <nav> elements. Not enabled by default.RemoveHeaderPreprocessor: strips <header> elements. Not enabled by default.RemoveFooterPreprocessor: strips <footer> elements. Not enabled by default.You can configure which preprocessors run in the config file:
// config/markdown-response.php
'preprocessors' => [
Spatie\MarkdownResponse\Preprocessors\RemoveScriptsAndStylesPreprocessor::class,
Spatie\MarkdownResponse\Preprocessors\RemoveNavigationPreprocessor::class,
],
Preprocessors run in the order they are listed.
A preprocessor is an invokable class that implements the Preprocessor interface:
namespace App\Preprocessors;
use Spatie\MarkdownResponse\Preprocessors\Preprocessor;
class RemoveAds implements Preprocessor
{
public function __invoke(string $html): string
{
return preg_replace('/<div\b[^>]*class="[^"]*\bad\b[^"]*"[^>]*>.*?<\/div>/is', '', $html);
}
}
Then register it in the config file:
// config/markdown-response.php
'preprocessors' => [
Spatie\MarkdownResponse\Preprocessors\RemoveScriptsAndStylesPreprocessor::class,
App\Preprocessors\RemoveAds::class,
],
How can I help you explore Laravel packages today?