1. 程式人生 > >javaweb專案整合editor.md編輯器---markdown編輯器

javaweb專案整合editor.md編輯器---markdown編輯器

最近寫一個部落格系統,需要整合編輯器,所以我就打算整合一個markdown編輯器用來編輯文章。在網上搜,好像editor.md這個國產開源的markdown編輯器比較好。不過想吐槽是網上的教程真的。。。哈哈哈,開始我們的教程:
1.首先在editor.md官網(http://pandao.github.io/editor.md/)下載這一個編輯器。
這是下載好後的檔案目錄

這裡寫圖片描述
2.將目錄下的css目錄,images目錄,lib目錄,plugins目錄,src目錄以及example目錄中的css目錄,js目錄和整合一下
這裡寫圖片描述
3.將提取後的檔案放在你專案的靜態資料夾中,我用的idea,我放在web目錄下的static資料夾中,
寫一個html如下

<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
        <title>Simple example - Editor.md examples</title>
        <link rel="stylesheet" href="/static/Admin/editor/css/style.css" />
        <link rel="stylesheet" href="/static/Admin/editor/css/editormd.css"
/>
<link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" href="/static/Public/css/bootstrap_button.css">\ <style> #submit_button{ display:block; margin-left
: 42%
; margin-top: 2em; margin-bottom: 2em; }
.input{ display: block; margin-left: 20%; }
</style> </head> <body> <div id="layout"> <div id="my-editormd" > <textarea id="my-editormd-markdown-doc" name="my-editormd-markdown-doc" style="display:none;"></textarea> <!-- 注意:name屬性的值--> <textarea id="my-editormd-html-code" name="my-editormd-html-code" style="display:none;"></textarea> </div> <button class="button button-block button-rounded button-action button-large" id="submit_button">確認提交</button> </div> <script src="/static/Public/jquery.js"></script> <script src="/static/Admin/editor/js/editormd.js"></script> <script type="text/javascript"> <!--為了讓editor.md編輯器完整的顯示出來--> var testEditor; $(function() { testEditor = editormd("my-editormd", { width : "90%", height : 640, syncScrolling : "single", path : "/static/Admin/editor/lib/", saveHTMLToTextarea : true }); }); <!--然後用ajax將編輯器中的md格式的資料提交到後臺,儲存在資料庫中,有的人可能儲存的是html格式的,也行,這個看你自己的選擇了,不過儲存.md格式還是要好一點--> $("#submit_button").click(function () { $.ajax({ type: "post", url: "", data: { content:$("#my-editormd-html-code").val(), }, contentType: "application/x-www-form-urlencoded; charset=utf-8", dataType: "json", success: function (data) { }, error:function () { } }); }); </script> </body> </html>

4.第三步是為了把資料儲存到資料庫中,這一步那就是將資料庫儲存的資料在前端顯示。
同樣的寫個jsp檔案如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
    <meta charset="utf-8" />
    <title>HTML Preview(markdown to html) - Editor.md examples</title>
    <link rel="stylesheet" href="/static/Admin/editor/css/style.css" />
    <link rel="stylesheet" href="/static/Admin/editor/css/editormd.preview.css" />
    <link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
    <style>
        .editormd-html-preview {
            width: 90%;
            margin: 0 auto;
            padding-top: 9em;
        }
    </style>
</head>
<body>
<div id="layout">
    <div id="test-editormd-view">
        <textarea style="display:none;" name="test-editormd-markdown-doc"></textarea>
    </div>
    <div id="test-editormd-view2">
                <%--存放md資料--%>
                <textarea id="append-test" style="display:none;">
                    ${articleContent}
                </textarea>
    </div>
</div>
<script src="/static/Public/jquery.js"></script>
<script src="/static/Admin/editor/lib/marked.min.js"></script>
<script src="/static/Admin/editor/lib/prettify.min.js"></script>
<script src="/static/Admin/editor/lib/raphael.min.js"></script>
<script src="/static/Admin/editor/lib/underscore.min.js"></script>
<script src="/static/Admin/editor/lib/sequence-diagram.min.js"></script>
<script src="/static/Admin/editor/lib/flowchart.min.js"></script>
<script src="/static/Admin/editor/lib/jquery.flowchart.min.js"></script>
<script src="/static/Admin/editor/js/editormd.js"></script>
<script type="text/javascript">
    $(function() {
        var testEditormdView, testEditormdView2;
        $.get("test.md", function(markdown) {
            testEditormdView = editormd.markdownToHTML("test-editormd-view", {
                markdown        : markdown ,//+ "\r\n" + $("#append-test").text(),
                htmlDecode      : "style,script,iframe",  // you can filter tags decode
                tocm            : true,    // Using [TOCM]
//                 markdownSourceCode : true, // 是否保留 Markdown 原始碼,即是否刪除儲存原始碼的 Textarea 標籤
                emoji           : true,
                taskList        : true,
                tex             : true,  // 預設不解析
                flowChart       : true,  // 預設不解析
                sequenceDiagram : true,  // 預設不解析
            });
            // 獲取Markdown原始碼
            //console.log(testEditormdView.getMarkdown());
            //alert(testEditormdView.getMarkdown());
        });
        testEditormdView2 = editormd.markdownToHTML("test-editormd-view2", {
            htmlDecode      : "style,script,iframe",  // you can filter tags decode
            emoji           : true,
            taskList        : true,
            tex             : true,  // 預設不解析
            flowChart       : true,  // 預設不解析
            sequenceDiagram : true,  // 預設不解析
        });
    });
</script>
</body>
</html>

上面幾步都是我從我的專案裡抽出來的程式碼,真實可用。
5.關於編輯器拓展功能的實現我就不多說了,這方面度娘上的文章很多了。

以上只是用原始碼說明一下,怎樣在Javaweb專案中整合editor.md編輯器,知識很淺,只是希望有些童鞋能少走些彎路。