SimpleMDE.JS 輕鬆打造一個類似簡書的純前端MarkDown語法編輯器

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>

效果圖

效果圖


進階用法

<!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>

效果圖

image

主要用到的配置

  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...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章