这篇文章主要讲解了“Thinkphp5框架中引入Markdown编辑器如何操作”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Thinkphp5框架中引入Markdown编辑器如何操作”吧!
1.把下载的项目放在public目录下
2.页面中引入jquery.js,editormd.js,editormd.css
demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>markdown测试</title> <link rel="stylesheet" href="/public/markdown/css/editormd.css" rel="external nofollow" /> <script src="__JS__/jquery.min.js"></script> <script src="/public/markdown/editormd.js"></script> </head> <body> <form action="{:url('test')}" enctype="multipart/form-data" method='post'> <div id="content-editormd" class="form-group"> <textarea class="form-control" id="content-editormd-markdown-doc" name="content-editormd-markdown-doc"></textarea> </div> <button>提交</button> </form> <script type="text/javascript"> $(function() { editormd("content-editormd", { placeholder : '编辑你的内容...', width : "100%", height : 1000, syncScrolling : "single", path : "/public/markdown/lib/", watch : true, previewTheme : "white",//预览 theme : 'white',//工具栏 saveHTMLToTextarea : true, // 保存HTML到Textarea // 图片上传 imageUpload : true, imageFormats: ["jpg","jpeg","gif","png","bmp","webp"], imageUploadURL: "/api/Upload/markdownUpload", toolbarIcons : function() { //自定义工具栏,后面有详细介绍 return editormd.toolbarModes['full']; // full, simple, mini }, }); }); //上传 /* { success : 0 | 1, // 0 表示上传失败,1 表示上传成功 message : "提示的信息,上传成功或上传失败及错误信息等。", url : "图片地址" // 上传成功时才返回 } */ </script> </body> </html>
上传图片
public function markdownUpload(){ $config = [ 'size' => 2097152, 'ext' => 'jpg,gif,png,bmp' ]; $file = $this->request->file('editormd-image-file'); $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads'); $save_path = '/uploads/'; $info = $file->validate($config)->move($upload_path); if ($info) { $result = [ 'success' => 1, 'message' => '上传成功', 'url' => str_replace('\\', '/', '/public/'.$save_path . $info->getSaveName()) ]; } else { $result = [ 'success' => 0, 'message' => $file->getError(), 'url' => str_replace('\\', '/', '/public/'.$save_path . $info->getSaveName()) ]; } return json($result); }
3.页面加载markdown格式内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>页面加载markdown格式内容</title> <link href="/public/markdown/css/editormd.min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" /> <script src="__JS__/jquery.min.js"></script> <script src="/public/markdown/lib/marked.min.js"></script> <script src="/public/markdown/lib/prettify.min.js"></script> <script src="/public/markdown/lib/raphael.min.js"></script> <script src="/public/markdown/lib/underscore.min.js"></script> <script src="/public/markdown/lib/sequence-diagram.min.js"></script> <script src="/public/markdown/lib/flowchart.min.js"></script> <script src="/public/markdown/lib/jquery.flowchart.min.js"></script> <script src="/public/markdown/editormd.js"></script> </head> <body> <div id="doc-content"> <textarea > ```php <?php echo 1; ?> ``` </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>
4.直接展示html格式的内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>前端显示</title> <link href="/public/markdown/css/editormd.min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" /> <script src="__JS__/jquery.min.js"></script> <script src="/public/markdown/lib/marked.min.js"></script> <script src="/public/markdown/lib/prettify.min.js"></script> <script src="/public/markdown/editormd.min.js"></script> </head> <body> <div id="doc-content"> {:htmlspecialchars_decode($data)} </div> <script type="text/javascript"> $(function () { editormd.markdownToHTML("doc-content"); }) </script> </body> </html>
感谢各位的阅读,以上就是“Thinkphp5框架中引入Markdown编辑器如何操作”的内容了,经过本文的学习后,相信大家对Thinkphp5框架中引入Markdown编辑器如何操作这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。