Transforming comments | laravel-comments | Spatie

 SPATIE

laravel-comments
================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-comments](https://spatie.be/docs/laravel-comments/v2)  Basic-usage  Transforming comments

 Version   v2   v1

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

- [ Introduction ](https://spatie.be/docs/laravel-comments/v2/introduction)
- [ Getting a license ](https://spatie.be/docs/laravel-comments/v2/getting-a-license)
- [ Requirements ](https://spatie.be/docs/laravel-comments/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-comments/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-comments/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-comments/v2/changelog)
- [ Upgrade guide ](https://spatie.be/docs/laravel-comments/v2/upgrade)
- [ About us ](https://spatie.be/docs/laravel-comments/v2/about-us)

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

- [ Working with comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/working-with-comments)
- [ Working with reactions ](https://spatie.be/docs/laravel-comments/v2/basic-usage/working-with-reactions)
- [ Transforming comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/transforming-comments)
- [ Sending notifications ](https://spatie.be/docs/laravel-comments/v2/basic-usage/sending-notifications)
- [ Approving comments ](https://spatie.be/docs/laravel-comments/v2/basic-usage/approving-comments)
- [ Listing comments in Laravel Nova ](https://spatie.be/docs/laravel-comments/v2/basic-usage/listing-comments-in-laravel-nova)

Livewire components
-------------------

- [ Introduction ](https://spatie.be/docs/laravel-comments/v2/livewire-components/introduction)
- [ Installation ](https://spatie.be/docs/laravel-comments/v2/livewire-components/installation)
- [ Using the components ](https://spatie.be/docs/laravel-comments/v2/livewire-components/using-the-components)
- [ Taking care of authorization ](https://spatie.be/docs/laravel-comments/v2/livewire-components/taking-care-of-authorization)
- [ Customising the views ](https://spatie.be/docs/laravel-comments/v2/livewire-components/customising-the-views)
- [ Mentions ](https://spatie.be/docs/laravel-comments/v2/livewire-components/mentions)
- [ Miscellaneous options ](https://spatie.be/docs/laravel-comments/v2/livewire-components/miscellaneous-options)
- [ Using Markdown ](https://spatie.be/docs/laravel-comments/v2/livewire-components/using-markdown)

 Transforming comments
=====================

###  On this page

1. [ Transforming Markdown to HTML ](#content-transforming-markdown-to-html)
2. [ Creating your own transformer class ](#content-creating-your-own-transformer-class)

When a comments get created, the package can transform that comment for you. This can be handy when you're allowing the user to use markdown to render comments, but you want to display them as HTML.

A comment can be created like this:

```
$blogPost->comment('Everybody saw the sunshine');
```

The text passed to comment used as the value of the `original_text` attribute of the `Comment` model.

In the `comments` config, you can specify comment transformer classes that can transform the comment. The transformed comment will be put in the `text` attribute of the `Comment` model. If you're building a UI, you should use `text` to display the comment, and the `original_text` when the user is editing the comment.

Transforming Markdown to HTML
-----------------------------------------------------------------------------------------------------------------------------------------------

The package ships with a transformer `Spatie\Comments\CommentProcessors\MarkdownToHtmlProcessor` that can transform markdown to HTML. Any code snippets will be highlighted too.

To use this transformer you should install the `spatie/laravel-markdown` package.

```
composer require spatie/laravel-markdown
```

Make sure to install Shiki as well, this is mentioned in [the installation instruction of laravel-markdown](https://spatie.be/docs/laravel-markdown/v1/installation-setup).

Next, specify the `MarkdownToHtmlProcessor::class` in the `transformers` key of the `comments` config file.

```
// in `config/comments.php`

'comment_transformers' => [
    Spatie\Comments\CommentProcessors\MarkdownToHtmlProcessor::class,
],
```

After that, when you create a comment like this...

```
$blogPost->comment('## Title')
```

... will create a new `Comment` model with these attributes:

- `original_text`: '## Title'
- `text`: `My title`

Creating your own transformer class
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

You can create a transformer by letting any class implement the `Spatie\Comments\CommentTransformers\CommentTransformer` interface.

Here's how that interface looks like:

```
namespace Spatie\Comments\CommentTransformers;

use Spatie\Comments\Models\Comment;

interface CommentTransformer
{
    public function handle(Comment $comment): void;
}
```

Inside the `handle` method you should set the `text` property of the given `$comment`.

For an example, take a look at [the sourcecode of `MarkdownToHtmlProcessor`](https://github.com/spatie/laravel-comments/blob/af3dc7a415f3022fc0213b0eeff5f540d139fe89/src/CommentTransformers/MarkdownToHtmlTransformer.php).
