让前端飞Markdown

SimpleMDE.JS 轻松打造一个类似简书的纯前端MarkD

2019-01-25  本文已影响5人  zzzmh
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/simplemde-markdown-editor


稍微复制一些说明文档中主要的配置参数说明

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

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.


顺便说下这个工具有什么用哈

一般个人建博客站,如果是不用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/articles/613408
simplemde官网
https://simplemde.com/
Github
https://github.com/sparksuite/simplemde-markdown-editor

上一篇下一篇

猜你喜欢

热点阅读