Choose a conversion driver | 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)  Basic-usage  Choose a conversion driver

 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)

 Choose a conversion driver
==========================

###  On this page

1. [ Use the League driver (default) ](#content-use-the-league-driver-default)
2. [ Use the Cloudflare driver ](#content-use-the-cloudflare-driver)
3. [ Create a custom driver ](#content-create-a-custom-driver)
4. [ Switch drivers at runtime ](#content-switch-drivers-at-runtime)

By default, the package converts HTML to markdown locally using [league/html-to-markdown](https://github.com/thephpleague/html-to-markdown). If you need better conversion quality or JavaScript rendering support, you can switch to an external driver.

Set the driver via the `MARKDOWN_RESPONSE_DRIVER` environment variable.

Use the League driver (default)
-------------------------------------------------------------------------------------------------------------------------------------------------

The League driver runs locally, requires no external services, and works out of the box.

```
MARKDOWN_RESPONSE_DRIVER=league
```

Options are passed directly to the `HtmlConverter` constructor. See the [league/html-to-markdown documentation](https://github.com/thephpleague/html-to-markdown#options) for available options.

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

'driver_options' => [
    'league' => [
        'options' => [
            'strip_tags' => true,
            'hard_break' => true,
        ],
    ],
],
```

Use the Cloudflare driver
-----------------------------------------------------------------------------------------------------------------------------------

The Cloudflare driver uses the [Workers AI API](https://developers.cloudflare.com/workers-ai/) to convert HTML to markdown server-side. It sends the HTML as a file upload and returns the converted markdown.

```
MARKDOWN_RESPONSE_DRIVER=cloudflare
CLOUDFLARE_ACCOUNT_ID=your-account-id
CLOUDFLARE_API_TOKEN=your-api-token
```

To get your credentials:

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com)
2. Your Account ID is in the dashboard URL
3. Create an API token under Manage account &gt; Account API tokens

Create a custom driver
--------------------------------------------------------------------------------------------------------------------------

You can create your own driver by implementing the `MarkdownDriver` interface:

```
namespace App\Drivers;

use Spatie\MarkdownResponse\Drivers\MarkdownDriver;

class PandocDriver implements MarkdownDriver
{
    public function convert(string $html): string
    {
        // Your conversion logic here
    }
}
```

Then bind it to the `MarkdownDriver` interface in a service provider:

```
use App\Drivers\PandocDriver;
use Spatie\MarkdownResponse\Drivers\MarkdownDriver;

$this->app->singleton(MarkdownDriver::class, PandocDriver::class);
```

Switch drivers at runtime
-----------------------------------------------------------------------------------------------------------------------------------

You can switch drivers on a per-conversion basis using the `using()` method:

```
use Spatie\MarkdownResponse\Facades\Markdown;

$markdown = Markdown::using('cloudflare')->convert($html);
```
