Converting Markdown to HTML in Laravel - TechvBlogs

Converting Markdown to HTML in Laravel

Learn how to easily convert Markdown to HTML in Laravel using the built-in \Illuminate\Support\Str::markdown() method. Follow our step-by-step guide and start converting your Markdown content to HTML in no time.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 year ago

TechvBlogs - Google News

Markdown is a popular syntax for writing formatted text that is easily converted to HTML. Laravel provides a simple and efficient way to convert Markdown to HTML using the \Illuminate\Support\Str::markdown() method, which is a part of Laravel's String Helpers.

In this article, we will discuss how to use the \Illuminate\Support\Str::markdown() method in Laravel to convert Markdown to HTML.

Step 1: Install Laravel

Before we begin, make sure you have Laravel installed on your system. You can install Laravel by following the instructions provided in the official Laravel documentation.

Step 2: Create a Markdown File

The first step is to create a Markdown file that you want to convert to HTML. You can create a Markdown file using any text editor such as Sublime Text, Visual Studio Code, or Notepad++. In this example, we will create a file named "example.md" with the following content:

# Example Heading

This is an example paragraph.

- List item 1
- List item 2
- List item 3

Save the file in the "resources" directory of your Laravel application.

Step 3: Convert Markdown to HTML

Now that we have installed Laravel and created a Markdown file, we can use the \Illuminate\Support\Str::markdown() method to convert the Markdown content to HTML.

We can do this by using the following code in our controller:

use Illuminate\Support\Str;

$html = Str::markdown(file_get_contents(resource_path('example.md')));

In this code, we are first importing the Illuminate\Support\Str class, which contains the markdown() method. Next, we are using the file_get_contents() function to read the contents of the "example.md" file and passing it to the markdown() method, which will convert it to HTML and store the result in the $html variable.

Step 4: Display the HTML

Finally, we need to display the HTML on our web page. We can do this by passing the $html variable to our view file and rendering it using Laravel's blade templating engine.

We can pass the $html variable to our view file using the following code in our controller:

return view('example', ['html' => $html]);

This code will pass the $html variable to the "example" view file.

In our view file, we can display the HTML content using the following code:

{!! $html !!}

This code will display the HTML content on our web page.

Conclusion

In this article, we have discussed how to use the \Illuminate\Support\Str::markdown() method in Laravel to convert Markdown to HTML. By following the steps outlined in this article, you can easily convert Markdown content to HTML and display it on your web page using Laravel's blade templating engine.

Comments (0)

Comment


Note: All Input Fields are required.