主頁 > 知識(shí)庫 > html+js 實(shí)現(xiàn)markdown編輯器效果

html+js 實(shí)現(xiàn)markdown編輯器效果

熱門標(biāo)簽:百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 使用U盤裝系統(tǒng) 檢查注冊(cè)表項(xiàng) 阿里云 網(wǎng)站建設(shè) 智能手機(jī) 美圖手機(jī) 硅谷的囚徒呼叫中心

markdown的Editor.md插件官方網(wǎng)站:https://pandao.github.io/editor.md/

一:下載Editor.md

1:進(jìn)入官網(wǎng)直接進(jìn)行下載

github下載地址:https://github.com/pandao/editor.md

2:使用npm進(jìn)行下載

npm install editor.md

下載下來的文件結(jié)構(gòu)如下:

二:Editor.md的簡(jiǎn)單使用

1:前提:

引入css

<link rel="stylesheet" href="editormd/css/editormd.css" />

引入js

<script src="js/jquery.min.js"></script>
<script src="editormd/editormd.min.js"></script>

2:html+js實(shí)現(xiàn)markdown效果

<link rel="stylesheet" href="editormd/css/editormd.css" />
<div id="test-editor">
<textarea style="display:none;" class="content-markdown" name="content-markdown"></textarea>
</div>
<script src="js/jquery.min.js"></script>
<script src="editormd/editormd.min.js"></script>
<script type="text/javascript">
    $(function() {
      var editor = editormd("test-editor", {
        width  : "100%",            //寬度,不填為100%
        height : "500px",           //高度,不填為100%
        theme : "dark",             //主題,不填為默認(rèn)主題
        path   : "editormd/lib/",   //editor.md插件的lib目錄地址
        saveHTMLToTextarea : true, // 保存 HTML 到 Textarea
        toolbarAutoFixed:true,      //工具欄自動(dòng)固定定位的開啟與禁用
      });
    });
</script>

根據(jù)如上代碼就可以實(shí)現(xiàn)markdown編輯器效果

但是如上代碼沒有本地上傳圖片功能,如果你需要本地上傳圖片功能,js代碼修改如下:

$(function() {
    var editor = editormd("test-editor", {
        width  : "100%",            //寬度,不填為100%
        height : "500px",           //高度,不填為100%
        theme : "dark",             //主題,不填為默認(rèn)主題
        path   : "editormd/lib/",   //editor.md插件的lib目錄地址
        saveHTMLToTextarea : true, // 保存 HTML 到 Textarea
        toolbarAutoFixed:true,      //工具欄自動(dòng)固定定位的開啟與禁用
        imageUpload : true,         //運(yùn)行本地上傳
        imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],    //允許上傳的文件格式
        imageUploadURL : "/index.php?r=markdown/upload"                 //上傳的后臺(tái)服務(wù)器路徑
    });
});

后端上傳的簡(jiǎn)單實(shí)現(xiàn)如下(這里上傳我使用了Yii框架的intervention/image插件)

Yii::$app->response->format = Response::FORMAT_JSON;
$upload = \Intervention\Image\ImageManagerStatic::make($_FILES['editormd-image-file']['tmp_name'])->save('upload/upload.jpg');//file為上傳表單的name名
if ($upload) {
    return [
        'success' => 1,
        'message' => '上傳成功',
        'url' => 'upload/upload.jpg'
    ];
} else {
    return [
        'success' => 0,
        'message' => '上傳失敗',
    ];
}

根據(jù)如上代碼就可以實(shí)現(xiàn)在markdown編輯器中上傳本地圖片

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:煙臺(tái) 湖北 懷化 山南 湘潭 黃山 賀州 通遼

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html+js 實(shí)現(xiàn)markdown編輯器效果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266