Clean up HTML with preprocessors | laravel-markdown-response | Spatie

 SPATIE

  Laravel Markdown Response
============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-markdown-response](https://spatie.be/docs/laravel-markdown-response/v1)  Advanced-usage  Clean up HTML with preprocessors

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-markdown-response/v1)

- [ Introduction ](https://spatie.be/docs/laravel-markdown-response/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-markdown-response/v1/requirements)
- [ Install the package ](https://spatie.be/docs/laravel-markdown-response/v1/installation-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/laravel-markdown-response/v1/questions-issues)
- [ Support us ](https://spatie.be/docs/laravel-markdown-response/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-markdown-response/v1/about-us)
- [ Changelog ](https://spatie.be/docs/laravel-markdown-response/v1/changelog)

Basic usage
-----------

- [ Serve markdown to AI agents ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/serve-markdown-to-ai-agents)
- [ Cache converted responses ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/cache-converted-responses)
- [ Choose a conversion driver ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/choose-a-conversion-driver)
- [ Test your setup ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/test-your-setup)

Advanced usage
--------------

- [ Clean up HTML with preprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-html-with-preprocessors)
- [ Clean up markdown with postprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-markdown-with-postprocessors)
- [ Customize request detection ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/customize-request-detection)
- [ Listen to conversion events ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/listen-to-conversion-events)
- [ Convert HTML directly ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/convert-html-directly)
- [ CDN and cache layers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/cdn-and-cache-layers)
- [ Response headers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/response-headers)

 Clean up HTML with preprocessors
================================

###  On this page

1. [ Create a custom preprocessor ](#content-create-a-custom-preprocessor)

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 `` tags, `` tags, and stylesheet `` tags. Enabled by default.
- `RemoveNavigationPreprocessor`: strips `` elements. Not enabled by default.
- `RemoveHeaderPreprocessor`: strips `` elements. Not enabled by default.
- `RemoveFooterPreprocessor`: strips `` 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.

Create a custom preprocessor
--------------------------------------------------------------------------------------------------------------------------------------------

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('/]*class="[^"]*\bad\b[^"]*"[^>]*>.*?/is', '', $html);
    }
}
```

Then register it in the config file:

```
// config/markdown-response.php

'preprocessors' => [
    Spatie\MarkdownResponse\Preprocessors\RemoveScriptsAndStylesPreprocessor::class,
    App\Preprocessors\RemoveAds::class,
],
```
