Clean up markdown with postprocessors | 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 markdown with postprocessors

 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 markdown with postprocessors
=====================================

###  On this page

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

After HTML is converted to markdown, the result runs through postprocessors. These clean up the markdown output, for example by removing leftover HTML tags or collapsing excessive blank lines.

The package ships with two postprocessors:

- `RemoveHtmlTagsPostprocessor`: strips any HTML tags that survived the conversion, such as `` tags from syntax highlighting. Enabled by default.
- `CollapseBlankLinesPostprocessor`: reduces three or more consecutive blank lines to a single blank line, and trims leading/trailing whitespace. Enabled by default.

You can configure which postprocessors run in the config file:

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

'postprocessors' => [
    Spatie\MarkdownResponse\Postprocessors\RemoveHtmlTagsPostprocessor::class,
    Spatie\MarkdownResponse\Postprocessors\CollapseBlankLinesPostprocessor::class,
],
```

Postprocessors run in the order they are listed.

Create a custom postprocessor
-----------------------------------------------------------------------------------------------------------------------------------------------

A postprocessor is an invokable class that implements the `Postprocessor` interface:

```
namespace App\Postprocessors;

use Spatie\MarkdownResponse\Postprocessors\Postprocessor;

class AddFrontMatter implements Postprocessor
{
    public function __invoke(string $markdown): string
    {
        return "---\nsource: my-app\n---\n\n" . $markdown;
    }
}
```

Then register it in the config file:

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

'postprocessors' => [
    Spatie\MarkdownResponse\Postprocessors\RemoveHtmlTagsPostprocessor::class,
    Spatie\MarkdownResponse\Postprocessors\CollapseBlankLinesPostprocessor::class,
    App\Postprocessors\AddFrontMatter::class,
],
```
