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:
'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('/<div\b[^>]*class="[^"]*\bad\b[^"]*"[^>]*>.*?<\/div>/is', '', $html);
}
}
Then register it in the config file:
'preprocessors' => [
Spatie\MarkdownResponse\Preprocessors\RemoveScriptsAndStylesPreprocessor::class,
App\Preprocessors\RemoveAds::class,
],