当前位置:网站首页>Laravel文档阅读笔记-Adding a Markdown editor to Laravel
Laravel文档阅读笔记-Adding a Markdown editor to Laravel
2022-06-28 21:35:00 【IT1995】
注意
此博文对应的Laravel版本为8,5版本是不能用的,很多函数都没有。
开始
使用Laravel创建好基本的blog,css美化用Tailwind CSS,创建Post界面是这样的。

这里内容使用的是html中的textarea关键字,这样写文章内容非常痛苦,所以将这个textarea升级,换成MarkDown。
安装和配置
MarkDown的编辑器使用Toast UI Editor,使用如下命令进行安装:
npm install --save @toast-ui/editor使用div代替上textarea区域,设置好id,后面用JS中配置MarkDown编辑器:
<!-- 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>下面在JS中,将MarkDown编辑器实例化出来。
// 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!',
})编译一下:
npm run dev # or 'npm run watch' to listen for file changes and compile automatically这样MarkDown的编辑器就出来了:

如果上面的功能不太行,可以查文档进行编辑器配置。
设置MarkDown内容的编辑
编辑器被渲染到div中,是没有inpute框的,也没有name这个属性,这样,不能发送到服务端进行存储。
所以要使用JS获取MarkDown编辑器中的内容,并且增加隐藏的表单,把MarkDown的内容设置到表达Input中。
对应的html代码如下:
<!-- resources/views/posts/create.blade.php -->
<input type="hidden" name="content" id="content">对应的JS代码如下:
// resources/js/app.js
document.querySelector('#createPostForm').addEventListener('submit', e => {
e.preventDefault();
document.querySelector('#content').value = editor.getMarkdown();
e.target.submit();
});然后编译下前端代码:
npm run dev这样后端接收到后,就能进行存储了。

MarkDown转成HTML
文章以MarkDown的格式存储后,需要再前端进行展示,如下:

2021年1月,Laravel就集成了MarkDown转HTML的功能,用法简单:
// resources/views/posts/show.blade.php
{!! \Illuminate\Support\Str::markdown($post->content) !!}这样显示就正常了:

源码下载地址:
https://github.com/stevie-c91/laravel-markdown-editor
边栏推荐
- [webapi] return dynamic list dynamic
- Definition and precautions of genuine St link/v2 j-link jtag/swd pin
- Manual backup and restore of DHCP server
- LeetCode122. The best time to buy and sell stocks II
- PE file-
- PHP uses stack to solve maze problem
- be careful! The PMP emergency postponement exam is due today!
- Application of the purple book p113map of ananagrams (uva156)
- If you are a C developer, look at these three explicit programming techniques
- Zero foundation self-study SQL course | complete collection of date functions in SQL
猜你喜欢

视觉弱监督学习研究进展

Biovendor free light chain( κ and λ) Test steps of ELISA Kit

16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants

華為雲的AI深潜之旅

Multinomial distribution (a discrete distribution)

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

The rogue downloader named by 315 is back
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)

Query rewriting for opengauss kernel analysis

安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
随机推荐
Query rewriting for opengauss kernel analysis
Is it safe to open a stock trading account? Is it reliable?
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.
LeetCode226. 翻转二叉树
华为云的AI深潜之旅
Constructing the three-dimensional anti penetration of the actual combat defense system
User network model and QoE
LeetCode560. 和为K的子数组
If you are a C developer, look at these three explicit programming techniques
CVPR 2022 𞓜 a creative and aesthetic text generation method! Support any input
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
二叉树类题目 力扣
How can the sports app keep the end-to-side background alive to make the sports record more complete?
Lumiprobe protein labeling research scheme
pyechart绘制多条y轴折线图
安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
The rogue downloader named by 315 is back
Workplace tips | understanding the advantages of the position "knowing people"
LeetCode986. 区间列表的交集