react 搭建博客---支持markdown的富文本編輯器

最近在學nodejs,準備配合react+mongodb搭個博客,找了不少富文本編輯器,都不是很適合react用,後來看到一篇vue+node搭建博客的文章,裏面使用的simplemde(github地址),徹底就符合個人想法啊,界面簡潔大方還有預覽功能。
附上官方demo
用法也至關簡單,官方介紹的是外鏈的引用方法,下面我說一下如何配合 makded 語法庫和 highlight.js 代碼高亮插件應用到react中css

首先安裝相關依賴

npm install simplemde marked highlight.js  --save

ps:simplemde官方的css也要引入到項目中,否則樣式會缺失vue

在組件中引入

import SimpleMDE from 'simplemde'
import marked from 'marked'
import highlight from 'highlight.js'

基本使用

在constructor中new一個SimpleMDE編輯器
render中要有對應的DOM  <textarea id="editor"></textarea>

this.smde = new SimpleMDE({
        element: document.getElementById('editor').childElementCount,  
        autofocus: true,
        autosave: true,
        previewRender: function(plainText) {
                return marked(plainText,{
                        renderer: new marked.Renderer(),
                        gfm: true,
                        pedantic: false,
                        sanitize: false,
                        tables: true,
                        breaks: true,
                        smartLists: true,
                        smartypants: true,
                        highlight: function (code) {
                                return highlight.highlightAuto(code).value;
                        }
                });
        },
})

獲取編輯器內容node

this.smde.value()
相關文章
相關標籤/搜索