"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Using Helper Functions to Convert Markdown to HTML in Laravel 11

Using Helper Functions to Convert Markdown to HTML in Laravel 11

Published on 2024-08-01
Browse:177

Using Helper Functions to Convert Markdown to HTML in Laravel 11

In this tutorial, we'll create a helper function in a Laravel application to convert Markdown content to HTML using the league/commonmark library. We'll cover the steps to create a helper file, include it in our Laravel project, and use it in a Blade template. This mechanism is used in Laravel 11 to make functions globally available.

Optional: Handling Markdown Conversion in Controller

The alternative is to handle the Markdown conversion in controller before passing the data to the view, so we no longer need to create a helper file:

use League\CommonMark\CommonMarkConverter;

public function show($id)
{
    $course = Course::find($id);
    $converter = new CommonMarkConverter();

    $post->description = $converter->convertToHtml($post->description);

    return view('post.show', compact('post'));
}

Creating a Helper File to Make the Conversion Function Available in the Blade Template

Step 1: Install league/commonmark

First, we install league/commonmark library via Composer:

composer require league/commonmark

Step 2: Create the helpers.php File

Next, we'll create a helpers.php file to define our helper function. This file can be placed in the app directory or any other preferred location.

touch app/helpers.php

Open app/helpers.php and add the following content:

convertToHtml($markdown);
    }
}

Step 3: Including the Helper File in Composer

To ensure Laravel automatically loads the helpers.php file, we need to modify the composer.json file to add the path to helpers.php under the autoload section:

"autoload": {
    "files": [
        "app/helpers.php"
    ]
}

Step 4: Regenerate Composer Autoload Files

After modifying composer.json, regenerate the Composer autoload files by running:

composer dump-autoload

Step 5: Use the Helper Function in Blade Templates

With the helper function defined and loaded, we can now use it in the Blade templates to convert Markdown to HTML:

{!! markdownToHtml($post->description) !!}

This tutorial is provided to show how we can make custom functions available in blade templates.

Release Statement This article is reproduced at: https://dev.to/websilvercraft/using-helper-functions-to-convert-markdown-to-html-in-laravel-11-30a?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3