SimpleMDE.JS 轻松打造一个相似简书的纯前端MarkDown语法编辑器

《SimpleMDE.JS 轻松打造一个相似简书的纯前端MarkDown语法编辑器》

一向想搞一个纯前端的markdown编辑器+解析器。
将个人博客zzzmh.cn 的文章语法用markdown完成。
既能够大幅节约写文章斲丧时候,也能通用到简书或思否。
试了多个纯前端markdown库今后,觉得simpleMDE的用法最简朴,结果也与简书等最为靠近。

终究结果演示: https://tczmh.gitee.io/markdown
终究源码下载: https://gitee.com/tczmh/markdown

入门款demo

只须要引入CSS、JS、一个textarea标签即可。无其他依靠。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://cdn.bootcss.com/simplemde/1.11.2/simplemde.min.css" rel="stylesheet">
</head>
<body>
<textarea></textarea>
<script src="https://cdn.bootcss.com/simplemde/1.11.2/simplemde.min.js"></script>
<script>
    var simplemde = new SimpleMDE();
</script>
</body>
</html>

结果图

《SimpleMDE.JS 轻松打造一个相似简书的纯前端MarkDown语法编辑器》

进阶用法

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://cdn.bootcss.com/simplemde/1.11.2/simplemde.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/highlight.js/9.13.0/styles/github-gist.min.css" rel="stylesheet">
</head>
<body>
<textarea></textarea>
<script src="https://cdn.bootcss.com/simplemde/1.11.2/simplemde.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/highlight.min.js"></script>
<script>
    var simplemde = new SimpleMDE({
        element: document.querySelector('textarea'),
        autoDownloadFontAwesome:false,//true从默许地点引入fontawesome依靠 false需自行引入(国内用bootcdn更快点)
        autofocus:true,
        autosave: {
            enabled: true,
            uniqueId: "SimpleMDE",
            delay: 1000,
        },
        blockStyles: {
            bold: "**",
            italic: "*",
            code: "```"
        },
        forceSync: true,
        hideIcons: false,
        indentWithTabs: true,
        lineWrapping: true,
        renderingConfig:{
            singleLineBreaks: false,
            codeSyntaxHighlighting: true // 须要highlight依靠
        },
        showIcons: true,
        spellChecker: true
    });
    // 默许开启预览形式
    simplemde.toggleSideBySide();
</script>
</body>
</html>

结果图

《SimpleMDE.JS 轻松打造一个相似简书的纯前端MarkDown语法编辑器》

重要用到的设置

  1. 启用预览形式
  2. 启用自动保留(每秒保留到localstorage,防革新后丧失)
  3. 启用代码高亮
  4. 启用自定义地点的font-awesome

更多内容能够参考作者在github写的申明文档
https://github.com/sparksuite…

轻微复制一些申明文档中重要的设置参数申明

(鄙人英语水平有限,翻译就免了,交给有道翻译吧)

autoDownloadFontAwesome: If set to true, force downloads Font Awesome (used for icons). If set to false, prevents downloading. Defaults to undefined, which will intelligently check whether Font Awesome has already been included, then download accordingly.
autofocus: If set to true, autofocuses the editor. Defaults to false.
autosave: Saves the text that’s being written and will load it back in the future. It will forget the text when the form it’s contained in is submitted.

  • enabled: If set to true, autosave the text. Defaults to false.
  • delay: Delay between saves, in milliseconds. Defaults to 10000 (10s).
  • uniqueId: You must set a unique string identifier so that SimpleMDE can autosave. Something that separates this from other instances of SimpleMDE elsewhere on your website.

blockStyles: Customize how certain buttons that style blocks of text behave.

  • bold Can be set to ** or __. Defaults to **.
  • code Can be set to ``` or ~~~. Defaults to ``` .
  • italic Can be set to * or _. Defaults to *.

element: The DOM element for the textarea to use. Defaults to the first textarea on the page.
forceSync: If set to true, force text changes made in SimpleMDE to be immediately stored in original textarea. Defaults to false.
hideIcons: An array of icon names to hide. Can be used to hide specific icons shown by default without completely customizing the toolbar.
indentWithTabs: If set to false, indent using spaces instead of tabs. Defaults to true.
initialValue: If set, will customize the initial value of the editor.
insertTexts: Customize how certain buttons that insert text behave. Takes an array with two elements. The first element will be the text inserted before the cursor or highlight, and the second element will be inserted after. For example, this is the default link value: ["[", "](http://)"].

  • horizontalRule
  • image
  • link
  • table

lineWrapping: If set to false, disable line wrapping. Defaults to true.
parsingConfig: Adjust settings for parsing the Markdown during editing (not previewing).

  • allowAtxHeaderWithoutSpace: If set to true, will render headers without a space after the #. Defaults to false.
  • strikethrough: If set to false, will not process GFM strikethrough syntax. Defaults to true.
  • underscoresBreakWords: If set to true, let underscores be a delimiter for separating words. Defaults to false.

placeholder: Custom placeholder that should be displayed
previewRender: Custom function for parsing the plaintext Markdown and returning HTML. Used when user previews.
promptURLs: If set to true, a JS alert window appears asking for the link or image URL. Defaults to false.
renderingConfig: Adjust settings for parsing the Markdown during previewing (not editing).

  • singleLineBreaks: If set to false, disable parsing GFM single line breaks. Defaults to true.
  • codeSyntaxHighlighting: If set to true, will highlight using highlight.js. Defaults to false. To use this feature you must include highlight.js on your page. For example, include the script and the CSS files like:
    <script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">

shortcuts: Keyboard shortcuts associated with this instance. Defaults to the array of shortcuts.
showIcons: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar.
spellChecker: If set to false, disable the spell checker. Defaults to true.
status: If set to false, hide the status bar. Defaults to the array of built-in status bar items.

  • Optionally, you can set an array of status bar items to include, and in what order. You can even define your own custom status bar items.

styleSelectedText: If set to false, remove the CodeMirror-selectedtext class from selected lines. Defaults to true.
tabSize: If set, customize the tab size. Defaults to 2.
toolbar: If set to false, hide the toolbar. Defaults to the array of icons.
toolbarTips: If set to false, disable toolbar button tips. Defaults to true.

趁便说下这个东西有什么用哈

平常个人建博客站,如果是不必hexo、wp的话,就须要本身写个背景新增编辑文章,这样一来能够每篇文章就都须要用html语法来写大批代码,斲丧不少时候,还不能及时检察结果。用本东西既能够用在背景的文章编辑,也能够把编辑器设置成页面显现,直接衬着出html代码。从而大幅缩减开辟时候和编辑文章的时候。写出来的内容语法和简书、思否等一致都是markdown,复制过来也圆满兼容

END

本文也会宣布在我的个人博客,并会附上在线演示,迎接检察
https://zzzmh.cn/single?id=60
终究结果演示
https://tczmh.gitee.io/markdown
终究源码下载
https://gitee.com/tczmh/markdown
参考
https://yq.aliyun.com/article…
simplemde官网
https://simplemde.com/
Github
https://github.com/sparksuite…

    原文作者:zzzmh
    原文地址: https://segmentfault.com/a/1190000018021238
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞