markdown编辑器示例

た 入场券 2023-07-20 05:51 121阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>markdown编辑器示例</title>
  6. <link rel="stylesheet" type="text/css" href="css/editormd.min.css"/>
  7. <script type="text/javascript" src="jquery.min.js"></script>
  8. <script type="text/javascript" src="editormd.js"></script>
  9. </head>
  10. <body>
  11. <div class="editormd" id="content" style="border: none">
  12. <textarea class="editormd-markdown-textarea" name="markdown" id="markdown"></textarea>
  13. <textarea class="editormd-html-textarea" name="html" id="html"></textarea>
  14. </div>
  15. <script type="text/javascript">
  16. $(function(){
  17. var markdownEdit;
  18. markdownEdit = editormd('content',{
  19. width:"100%",
  20. height:740,
  21. syncScrolling:'single',
  22. path:'lib/',// editormd/lib/
  23. codeFold:true,
  24. emoji:true,
  25. taskList:true,
  26. tocm:true,
  27. tex:true,
  28. watch:true,
  29. flowChart:true,
  30. sequenceDiagram:true,
  31. imageUpload:true,
  32. saveHTMLToTextarea:true,
  33. htmlDecode:'style,script,iframe|on*',
  34. imageFormats:['jpg','jpeg','gif','png','bmp','webp'],
  35. imageUploadURL:'<%=path%>/admin/upload/editormd',
  36. toolbarIcons:function(){
  37. return ["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","html-entities","preview"];
  38. }
  39. });
  40. });
  41. </script>
  42. </body>
  43. </html>

上传图片控制器

  1. package com.ramostear.unaboot.web.admin;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ramostear.unaboot.common.UnaBootConst;
  4. import com.ramostear.unaboot.service.UploadService;
  5. import com.ramostear.unaboot.web.UnaBootController;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.shiro.authz.annotation.RequiresRoles;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import java.util.stream.Collectors;
  17. /**
  18. * @ClassName UnloadController
  19. * @Description 文件上传控制器
  20. * @Author 树下魅狐
  21. * @Date 2020/2/23 0023 8:00
  22. * @Version since UnaBoot-1.0
  23. **/
  24. @RequiresRoles(value = UnaBootConst.ROLE_ADMIN)
  25. @RestController
  26. @RequestMapping("/admin/upload")
  27. public class UnloadController extends UnaBootController {
  28. @Autowired
  29. private UploadService uploadService;
  30. @PostMapping("/editormd")
  31. public JSONObject upload(@RequestParam(name="editormd-image-file")MultipartFile file){
  32. JSONObject json = new JSONObject();
  33. if(file == null || file.isEmpty()){
  34. return convert(json,0,"上传文件不能为空","");
  35. }
  36. if(StringUtils.isBlank(file.getOriginalFilename()) || !allow(file.getOriginalFilename())){
  37. return convert(json,0,"上传文件格式不正确","");
  38. }
  39. String url = uploadService.upload(file);
  40. if(StringUtils.isBlank(url)){
  41. return convert(json,0,"文件上传失败","");
  42. }else{
  43. return convert(json,1,"文件上传成功",url);
  44. }
  45. }
  46. private JSONObject convert(JSONObject json,int status,String message,String url){
  47. json.put("success",status);
  48. json.put("message",message);
  49. json.put("url",url);
  50. return json;
  51. }
  52. private boolean allow(String fileName){
  53. String[] allowFiles = {".gif",".png",".jpg",".jpeg",".bpm"};
  54. String suffix = fileName.substring(fileName.lastIndexOf("."));
  55. List<String> suffixList = Arrays.stream(allowFiles).collect(Collectors.toList());
  56. return suffixList.contains(suffix);
  57. }
  58. }

控制器接收editormd-image-file参数,js中的content对应id=“content”
示例效果
在这里插入图片描述
在这里插入图片描述

附一篇相关文章https://blog.csdn.net/jabinjava/article/details/88375511

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Markdown编辑器总结

    Markdown 是一种简单的、轻量级的标记语法。用户可以使用诸如 \ \ 等简单的标记符号以最小的输入代价生成极富表现力的文档。   Markdown具有很多优点:

    相关 MarkDown编辑器学习

    前面的话 > 作为初入门的程序员小鸟,学会写博文笔记应该是成长的最好方法之一,但是 工欲善其事,必先利其器。在学写博文之前,必须先学会使用MarkDown编辑器。 M