一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot使用Editor.md構建Markdown富文本編輯器示例

SpringBoot使用Editor.md構建Markdown富文本編輯器示例

2021-04-15 11:25Blog_龍飛 Java教程

這篇文章主要介紹了SpringBoot使用Editor.md構建Markdown富文本編輯器示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

markdown是一種可以使用普通文本編輯器編寫的標記語言,通過簡單的標記語法,它可以使普通文本內容具有一定的格式。

前言

editor.md 是一款開源的、可嵌入的 markdown 在線編輯器(組件),基于 codemirror、jquery 和 marked 構建。本章將使用springboot整合editor.md構建markdown編輯器。

下載插件

項目地址:editor.md

解壓目錄結構:

SpringBoot使用Editor.md構建Markdown富文本編輯器示例

配置editor.md

將exapmles文件夾中的simple.html放置到項目中,并配置對應的css和js文件

配置編輯器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
......
 <script src="${re.contextpath}/jquery.min.js"></script>
  <script src="${re.contextpath}/editor/editormd.min.js"></script>
  <link rel="stylesheet" href="${re.contextpath}/editor/css/style.css" rel="external nofollow" />
  <link rel="stylesheet" href="${re.contextpath}/editor/css/editormd.css" rel="external nofollow" rel="external nofollow" />
  <link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" rel="external nofollow" type="image/x-icon"/>
......
<!-- 存放源文件用于編輯 -->
 <textarea style="display:none;" id="textcontent" name="textcontent">
</textarea>
    <!-- 第二個隱藏文本域,用來構造生成的html代碼,方便表單post提交,這里的name可以任意取,后臺接受時以這個name鍵為準 -->
    <textarea id="text" class="editormd-html-textarea" name="text"></textarea>
  </div>

初始化編輯器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var testeditor;
 
  $(function () {
    testeditor = editormd("test-editormd", {
      width: "90%",
      height: 640,
      syncscrolling: "single",
      path: "${re.contextpath}/editor/lib/",
      imageupload: true,
      imageformats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
      imageuploadurl: "/file",
      //這個配置在simple.html中并沒有,但是為了能夠提交表單,使用這個配置可以讓構造出來的html代碼直接在第二個隱藏的textarea域中,方便post提交表單。
      savehtmltotextarea: true
      // previewtheme : "dark"
    });
  });

這樣就實現了最簡單的editor.md編輯器,效果如下:

SpringBoot使用Editor.md構建Markdown富文本編輯器示例

訪問地址:http://localhost:8080/

圖片上傳

由于在初始化編輯器中配置的圖片上傳地址為imageuploadurl: "/file",,與之對應,我們在/file處理文件上傳即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@restcontroller
@requestmapping("/file")
@slf4j
public class filecontroller {
 
//  @value("")
//  string folder = system.getproperty("user.dir")+file.separator+"upload"+file.separator;
  /**
   * 在配置文件中配置的文件保存路徑
   */
  @value("${img.location}")
  private string folder;
 
  @postmapping
  public fileinfo upload(httpservletrequest request, @requestparam(value = "editormd-image-file", required = false) multipartfile file) throws exception {
    log.info("【filecontroller】 filename={},fileorginnmae={},filesize={}", file.getname(), file.getoriginalfilename(), file.getsize());
    log.info(request.getcontextpath());
    string filename = file.getoriginalfilename();
    string suffix = filename.substring(filename.lastindexof(".") + 1);
    string newfilename = new date().gettime() + "." + suffix;
 
    file localfile = new file(folder, newfilename);
    file.transferto(localfile);
    log.info(localfile.getabsolutepath());
    return new fileinfo(1, "上傳成功", request.getrequesturl().substring(0,request.getrequesturl().lastindexof("/"))+"/upload/"+newfilename);
  }
 
  @getmapping("/{id}")
  public void download(@pathvariable string id, httpservletrequest request, httpservletresponse response) {
    try (inputstream inputstream = new fileinputstream(new file(folder, id + ".txt"));
       outputstream outputstream = response.getoutputstream();) {
      response.setcontenttype("application/x-download");
      response.setheader("content-disposition", "attachment;filename=test.txt");
 
      ioutils.copy(inputstream, outputstream);
      outputstream.flush();
    } catch (exception e) {
 
    }
  }
}

文件預覽

表單post提交時,editor.md將我們的markdown語法文檔翻譯成了html語言,并將html字符串提交給了我們的后臺,后臺將這些html字符串持久化到數據庫中。具體在頁面顯示做法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8"/>
  <title>editor.md examples</title>
  <link rel="stylesheet" href="${re.contextpath}/editor/css/editormd.preview.min.css" rel="external nofollow" />
  <link rel="stylesheet" href="${re.contextpath}/editor/css/editormd.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<!-- 因為我們使用了dark主題,所以在容器div上加上dark的主題類,實現我們自定義的代碼樣式 -->
<div class="content editormd-preview-theme" id="content">${editor.content!''}</div>
<script src="${re.contextpath}/jquery.min.js"></script>
<script src="${re.contextpath}/editor/lib/marked.min.js"></script>
<script src="${re.contextpath}/editor/lib/prettify.min.js"></script>
<script src="${re.contextpath}/editor/editormd.min.js"></script>
<script type="text/javascript">
  editormd.markdowntohtml("content");
</script>
</body>
</html>

預覽地址:http://localhost:8080/editorweb/preview/{id}

編輯地址:http://localhost:8080/editorweb/edit/{id}

代碼下載

從我的 github 中下載,https://github.com/longfeizheng/editor-markdown

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://longfeizheng.github.io/2018/03/15/SpringBoot使用Editor.md構建Markdown富文本編輯器

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四缺一写的小说 | 四虎影视网址 | 污污在线免费观看 | 脱了白丝校花的内裤猛烈进入 | aaa一级最新毛片 | 天天做天天爱天天爽综合网 | 美女靠逼免费视频 | 精品国产在天天线在线麻豆 | 国产精品麻豆99久久 | 精品国产品香蕉在线观看 | 911亚洲精品国内自产 | 国产高清不卡码一区二区三区 | 国产乱子伦一区二区三区 | 免费国产成人高清视频网站 | 免费的网址 | 特黄a级三级三级野战 | 成人性用品 | jiuse在线| 欧美日韩亚毛片免费观看 | 午夜性爽视频男人的天堂在线 | 第一次做m被调教经历 | 欧美影院天天5g天天爽 | 色综合天天综合网看在线影院 | 99视频网址 | 精品国产自在在线在线观看 | 国产区香蕉精品系列在线观看不卡 | 国产精品成人在线播放 | 精品欧美一区二区三区在线观看 | 男人肌肌捅女人 | 欧美专区在线播放 | 金牛网155755水心论坛黄大父母 | www.一区二区三区.com | 精品久久久久久久国产潘金莲 | 国产精品aⅴ| 韩剧消失的眼角膜免费完整版 | 娇妻中日久久持久久 | 日本免费在线播放 | 丰满在线观看 | 九九热这里只有精品2 | 五月天色综合 | 99国产精品免费视频 |