This article will detail how to use Markdown in ThinkPHP6, helping developers easily integrate Markdown editing and parsing functionality into the framework.
In this article, we will use Editor.md as the Markdown editor library, and install it via Composer.
composer require houzhongjian/editor.md
Place Editor.md in the /public/editor.md directory and include the necessary CSS and JS files in your frontend page.
<link rel="stylesheet" href="/editor.md/css/editormd.min.css">
<script src="/editor.md/editormd.min.js"></script>
First, include the Editor.md library namespace in the Controller file.
use EditorMd\EditorMd;
Next, create an EditorMd instance in the Controller method where you want to use it.
$editor = new EditorMd();
Then, call the relevant Editor.md methods, such as the getHtml method to convert the Markdown text in the editor to HTML.
$html = $editor->getHtml($markdownText);
In addition to using third-party libraries, ThinkPHP6 also has a built-in Markdown parser that can be used to parse Markdown text directly.
use think\helper\Markdown;
$html = Markdown::parse($markdownText);
This article covered how to integrate the Editor.md editor library and use the built-in Markdown parser in ThinkPHP6 to implement Markdown editing and parsing functionality. Developers can choose the appropriate solution based on their specific needs.