最近项目中遇到了写接口文档,做OpenAPI的的记录以便别人查看文档,在网上看到Editor.md这个工具,在这里记录一下,就当做笔记吧,如果有需要可以参考一下。
官网示例了解下:http://pandao.github.io/editor.md/examples/index.html
一、效果图
先看效果图了解下,是不是你想要的,如果适合就往下看吧,哈哈。
图片
二、下载插件
项目地址:http://pandao.github.io/editor.md/
下载安装包后放在相应的目录中并引用
//依赖jquery
//需要这三个文件,自己对应好目录哦
三、在自己的页面中加上对应的id
四、js代码
主要就是说这个就是代码,记得make一下
var testEditor;
testEditor = editormd("test-editormd", {
placeholder:'本编辑器支持Markdown编辑,左边编写,右边预览', //默认显示的文字,这里就不解释了
width: "90%",
height: 640,
syncScrolling: "single",
path: "lib/js/editor.md-master/lib/", //你的path路径(原资源文件中lib包在我们项目中所放的位置)
theme: "dark",//工具栏主题
previewTheme: "dark",//预览主题
editorTheme: "pastel-on-dark",//编辑主题
saveHTMLToTextarea: true,
emoji: false,
taskList: true,
tocm: true, // Using [TOCM]
tex: true, // 开启科学公式TeX语言支持,默认关闭
flowChart: true, // 开启流程图支持,默认关闭
sequenceDiagram: true, // 开启时序/序列图支持,默认关闭,
toolbarIcons : function() { //自定义工具栏,后面有详细介绍
return editormd.toolbarModes['simple']; // full, simple, mini
},
});
//上面的挑有用的写上去就行
综上所述一个编辑器就诞生了,下面有个小知识点。
testEditor.getMarkdown();
// 在js中调用getMarkdown这个方法可以获得Markdown格式的文本。
五、页面展示Markdown文档
后台给我们的文档我们要展示成转换后的样子不能一大堆符号摆在页面上是吧,也不好看呀,所以下面放上展示代码需要的东东。
- 首先引入必要JS(下面不是所有的都必要)
//具体目录在你下载的文件里都能找到,对号入座
- 页面的div
- js代码
testEditor = editormd.markdownToHTML("test-editormd", {
markdown:$scope.apidetails.content,
htmlDecode : "style,script,iframe", // you can filter tags decode
emoji : true,
taskList : true,
tex : true, // 默认不解析
flowChart : true, // 默认不解析
sequenceDiagram : true, // 默认不解析
});
六、自定义工具栏
工具栏分为三组,full, simple, mini这三个,可以选择,如果想更加自由选你所需,就可以用下面的代码,也可以看看官网的示例
toolbarIcons : function() {
// Or return editormd.toolbarModes[name]; // full, simple, mini
// Using "||" set icons align right.
return ["undo", "redo", "|", "bold", "hr"]
},
具体没一个标签代表的什么含义可以对照整个工具栏自己对一下,下面是他的源码
t.toolbarModes={
full:["undo","redo","|","bold","del","italic","quote","ucwords","uppercase","lowercase","|","h1","h2","h3","h4","h5","h6","|","list-ul","list-ol","hr","|","link","reference-link","image","code","preformatted-text","code-block","table","datetime","emoji","html-entities","pagebreak","|","goto-line","watch","preview","fullscreen","clear","search","|","help","info"],
simple:["undo","redo","|","bold","del","italic","quote","uppercase","lowercase","|","h1","h2","h3","h4","h5","h6","|","list-ul","list-ol","hr","|","watch","preview","fullscreen","|","help","info"],
mini:["undo","redo","|","watch","preview","|","help","info"]
}