当前位置:网站首页>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
边栏推荐
- Is the VIP securities account of qiniu school really safe and regular? How do I say this?
- ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
- Is it safe to open a dig money account? Is it reliable?
- What is an interface? What is interface testing?
- CORBA Architecture Guide (Common Object Request Broker Architecture)
- QStringLiteral(str)
- Web自动化工具选择
- An artifact extracted from a well-known software and paid by a group of people
- QT how the coordinates of one control are relatively fixed and displayed on another control (coordinate system)
- Application of the purple book p113map of ananagrams (uva156)
猜你喜欢

Binomial distribution (a discrete distribution)

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点

零基础自学SQL课程 | SQL中的日期函数大全

【激活函数】

Lumiprobe proteorange protein gel dye instructions

E-commerce is popular, how to improve the store conversion rate?

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入

Interface use case design

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
![Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end](/img/d6/824985b74b27a1bee456c2cebbac26.jpg)
Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end
随机推荐
LeetCode226. 翻转二叉树
Application of the purple book p113map of ananagrams (uva156)
Web automation tool selection
Lumiprobe proteorange protein gel dye instructions
SqlTransaction
Ehcache configuration data, convenient for self checking
Go cryptobin common encryption and decryption Libraries
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
Leetcode: merge K ascending linked lists_ twenty-three
关于不完全类型的认识
LeetCode117. 填充每个节点的下一个右侧节点指针_II
LeetCode213. 打家劫舍II
An artifact extracted from a well-known software and paid by a group of people
Comprehensive evaluation of easy-to-use and powerful PDF reading software: PDF expert, marginnote, liquidtext, notability, goodnotes, Zotero
零基础自学SQL课程 | SQL中的日期函数大全
Lumiprobe non fluorescent alkyne research - dbco NHS ester
LeetCode123. 买卖股票的最佳时机III
Query rewriting for opengauss kernel analysis
Which software is safer to open an account on and what is the account opening process?