当前位置:网站首页>Adding a markdown editor to lavel
Adding a markdown editor to lavel
2022-06-28 22:05:00 【IT1995】
Be careful
This blog post corresponds to Laravel Version is 8,5 Version is not available , Many functions do not .
Start
Use Laravel Create basic blog,css For beautification Tailwind CSS, establish Post The interface is like this .

The content here uses html Medium textarea keyword , It is very painful to write an article like this , So put this textarea upgrade , Switch to MarkDown.
install and configure
MarkDown Editor for Toast UI Editor, Use the following command to install :
npm install --save @toast-ui/editorUse div Instead of textarea Area , Set it up id, Later use JS Middle configuration MarkDown Editor :
<!-- resources/views/posts/create.blade.php -->
<div class="flex flex-col space-y-2">
<label for="editor" class="text-gray-600 font-semibold">Content</label>
<div id="editor" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm"><div>
</div>The following is JS in , take MarkDown The editor is instantiated .
// resources/js/app.js
import Editor from '@toast-ui/editor'
import 'codemirror/lib/codemirror.css';
import '@toast-ui/editor/dist/toastui-editor.css';
const editor = new Editor({
el: document.querySelector('#editor'),
height: '400px'
initialEditType: 'markdown',
placeholder: 'Write something cool!',
})Compile the :
npm run dev # or 'npm run watch' to listen for file changes and compile automaticallysuch MarkDown The editor comes out :

If the above functions are not very good , You can check file Configure the editor .
Set up MarkDown Content editing
The editor is rendered to div in , It's not inpute Framed , either name This attribute , such , It cannot be sent to the server for storage .
So use JS obtain MarkDown Content in the editor , And add hidden forms , hold MarkDown Set the content of to express Input in .
Corresponding html The code is as follows :
<!-- resources/views/posts/create.blade.php -->
<input type="hidden" name="content" id="content">Corresponding JS The code is as follows :
// resources/js/app.js
document.querySelector('#createPostForm').addEventListener('submit', e => {
e.preventDefault();
document.querySelector('#content').value = editor.getMarkdown();
e.target.submit();
});Then compile the front-end code :
npm run devIn this way, after the back-end receives , It can be stored .

MarkDown Turn into HTML
The article uses MarkDown After the format of is stored , It needs to be displayed at the front end , as follows :

2021 year 1 month ,Laravel Is integrated with MarkDown turn HTML The function of , Simple to use :
// resources/views/posts/show.blade.php
{!! \Illuminate\Support\Str::markdown($post->content) !!}So the display is normal :

Source code download address :
https://github.com/stevie-c91/laravel-markdown-editor
边栏推荐
- [golang] leetcode intermediate subset & Word Search
- LeetCode188. The best time to buy and sell stocks IV
- Webrtc audio and video development - experience
- LeetCode122. The best time to buy and sell stocks II
- Openfire 3.8.2 cluster configuration
- Lumiprobe protein labeling research scheme
- Leetcode: expand a binary tree into a linked list_ one hundred and fourteen
- Real time transformer: meituan's research on single image depth estimation
- LeetCode56. 合并区间
- LeetCode121. The best time to buy and sell stocks
猜你喜欢

【电子实验2】简单电子门铃

Lumiprobe protein labeling research scheme

【激活函数】

AI deep dive of Huawei cloud

Recommend two high-quality Wallpaper software

Lumiprobe proteorange protein gel dye instructions

rosdep update 使用小鱼fishros解决ros1/ros2问题 2022

The rogue downloader named by 315 is back
![[width first search note] BFS output shortest path](/img/b5/553ca241ae571daecabd282f33f20a.jpg)
[width first search note] BFS output shortest path

什么是接口?什么是接口测试?
随机推荐
2022年股票在手机上开户安全吗?找谁可以办理?
Openfire 3.8.2 cluster configuration
PAT 1021. Traversal of the deep root (25 points) graph, DFS, calculating the number of connected components
Akamai acquires linode
How to open a safe and reliable securities account in the financial management class of qiniu school?
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
LeetCode116. Populate the next right node pointer for each node
LeetCode560. 和为K的子数组
17 `bs object Node name h3 Parent ` parents get parent node ancestor node
Workplace tips | understanding the advantages of the position "knowing people"
【激活函数】
LeetCode123. The best time to buy and sell stocks III
PHP login problem
Openfire user and group relationship migration
直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!
Interface use case design
Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list
華為雲的AI深潜之旅
ROS 2 Humble Hawksbill 之 f1tenth gym