App下載

怎么實(shí)現(xiàn)markdown編輯器效果?通過html+js 實(shí)現(xiàn)該效果案例分享!

你是我的所有夢(mèng) 2021-08-09 15:09:31 瀏覽數(shù) (2212)
反饋

今天和大家分享一個(gè)比較常見而且使用的問題和解決方法分享!那么今天的這個(gè)問題就是“怎么實(shí)現(xiàn)markdown編輯器效果?”對(duì)于這個(gè)問題下面是小編整理分相關(guān)內(nèi)容分享!

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 = InterventionImageImageManagerStatic::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編輯器中上傳本地圖片,那么以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,有喜歡前端的小伙伴們都可以在W3Cschool搜索到自己要學(xué)習(xí)的內(nèi)容。


0 人點(diǎn)贊