一、富文本编辑器
1.1、编辑器页面源码
<!-- md编辑器例子-->
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>Simple example - Editor.md examples</title>
<link rel="stylesheet" href="../css/editormd.css" />
<link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="layout">
<header>
<h1>Simple example</h1>
</header>
<div id="test-editormd">
<textarea style="display:none;">111111111111111111</textarea>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="../js/editormd.js"></script>
<script type="text/javascript">
var testEditor;
$(function() {
testEditor = editormd("test-editormd", {
width : "90%",
height : 640,
syncScrolling : "single",
path : "../lib/"
});
});
$('#test-editormd').bind('input propertychange', function() {
var content = $('.editormd-markdown-textarea').val()
console.log(content)
});
</script>
</body>
</html>
1.2 关键源码解释
1.2.1 html
添加一个textarea作为文本编辑器基本元素
<div id="test-editormd">
<textarea style="display:none;">111111111111111111</textarea>
</div>
1.2.2 css
引入editormd.css即可
<link rel="stylesheet" href="../css/editormd.css" />
<link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
1.2.3 js
引入 依赖js
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="../js/editormd.js"></script>
编写markdown内容保存逻辑
<script type="text/javascript">
var testEditor;
$(function() {
testEditor = editormd("test-editormd", {
width : "90%",
height : 640,
syncScrolling : "single",
path : "../lib/"
});
});
$('#test-editormd').bind('input propertychange', function() {
var content = $('.editormd-markdown-textarea').val()
console.log(content)
});
</script>
通过获取类editormd-markdown-textarea的值的方式获取输入内容。拿到内容后存入数据库即可。
1.3 注意
- jquery.min.js必须最先引入
- editormd.js依赖于其他的js文件。所以要将markdown.md文件夹下的/lib 路径下的内容放入静态资源文件夹下。
二、显示数据库中取出的md文本内容
2.1 html源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="../css/editormd.preview.css" />
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="../lib/marked.min.js"></script>
<script src="../lib/prettify.min.js"></script>
<script src="../lib/raphael.min.js"></script>
<script src="../lib/underscore.min.js"></script>
<script src="../lib/sequence-diagram.min.js"></script>
<script src="../lib/flowchart.min.js"></script>
<script src="../lib/jquery.flowchart.min.js"></script>
<script src="../js/editormd.js"></script>
</head>
<body>
<div id="doc-content">
<textarea style="display:none;" placeholder="markdown语言">#${md.content }</textarea>
</div>
<script type="text/javascript">
var testEditor;
$(function () {
testEditor = editormd.markdownToHTML("doc-content", {//注意:这里是上面DIV的id
htmlDecode: "style,script,iframe",
emoji: true,
taskList: true,
tocm: true,
tex: true, // 默认不解析
flowChart: true, // 默认不解析
sequenceDiagram: true, // 默认不解析
codeFold: true
});});
</script>
</body>
</html>
2.2 关键源码解释
2.2.1 引入的css和js
直接引入css 和 js即可
<link rel="stylesheet" href="../css/editormd.preview.css" />
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="../lib/marked.min.js"></script>
<script src="../lib/prettify.min.js"></script>
<script src="../lib/raphael.min.js"></script>
<script src="../lib/underscore.min.js"></script>
<script src="../lib/sequence-diagram.min.js"></script>
<script src="../lib/flowchart.min.js"></script>
<script src="../lib/jquery.flowchart.min.js"></script>
<script src="../js/editormd.js"></script>
2.2.2 回显逻辑
<script type="text/javascript">
var testEditor;
$(function () {
testEditor = editormd.markdownToHTML("doc-content", {//注意:这里是上面DIV的id
htmlDecode: "style,script,iframe",
emoji: true,
taskList: true,
tocm: true,
tex: true, // 默认不解析
flowChart: true, // 默认不解析
sequenceDiagram: true, // 默认不解析
codeFold: true
});});
</script>
直接按照模板代码写即可。
<textarea style="display:none;" placeholder="markdown语言">#${md.content }</textarea>
将从数据库拿出的md文本内容赋值给textarea的val即可显示文本内容。