资讯专栏INFORMATION COLUMN

SimpleMDE.JS 轻松打造一个类似简书的纯前端MarkDown语法编辑器

netScorpion / 3463人阅读

摘要:一直想搞一个纯前端的编辑器解析器。将个人博客的文章语法用实现。试了多个纯前端库以后,感觉的用法最简单,效果也与简书等最为接近。最终效果演示最终源码下载入门款只需要引入一个标签即可。从而大幅缩减开发时间和编辑文章的时间。

一直想搞一个纯前端的markdown编辑器+解析器。
将个人博客zzzmh.cn 的文章语法用markdown实现。
既可以大幅节省写文章耗费时间,也能通用到简书或思否。
试了多个纯前端markdown库以后,感觉simpleMDE的用法最简单,效果也与简书等最为接近。

最终效果演示: https://tczmh.gitee.io/markdown
最终源码下载: https://gitee.com/tczmh/markdown

入门款demo

只需要引入CSS、JS、一个textarea标签即可。无其他依赖。




    
    Title
    






效果图

进阶用法



    
    Title
    
    
    







效果图

主要用到的配置

启用预览模式

启用自动保存(每秒保存到localstorage,防刷新后丢失)

启用代码高亮

启用自定义地址的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:

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101430.html

相关文章

  • 靡不有初,鲜克有终——写在VNote半周岁

    摘要:舒适的编辑体验通过语法高亮,最大地消除与生俱来的编辑和阅读的割裂感。所以,是不是又少了一个回到阅读模式的借口代码块语法高亮通过插件可以支持代码块里面的代码语法高亮,其他的编辑器好像没有支持。 首发于简书. showImg(https://segmentfault.com/img/remote/1460000009164987); 从去年的十一开始到今天,VNote已经半周岁了,也迭代到...

    roland_reed 评论0 收藏0
  • SimpleMDE辑器 + 提取HTML + 美化输出

    摘要:不过的缩进,是个无情的痛,会呼吸的痛。使用方法在输出的地方加上的类即可下面的代码是给编辑器预览输出的容器加的。 这几天在帮公司内部弄编辑器,之前是用wangEditor富文本编辑器的,也不能说别人插件不好什么的,毕竟我是写不出来的。不过wangEditor的缩进,是个无情的痛,会呼吸的痛。因此我找到了一款好用的markown编辑器——simpleMDE 可能你会跟我说,哟?那Edit...

    elliott_hu 评论0 收藏0
  • 超好用的谷歌浏览器、Sublime Text、Phpstorm、油猴插件合集

    摘要:分享一些超好用插件,打造一个不一样的浏览器编辑器。一谷歌浏览器插件谷歌访问助手强烈推荐一键安装,无需其他配置,即可访问谷歌。谷歌浏览器是很耗内存的,该插件会自动挂起长时间未使用的网页,来释放系统资源。 showImg(https://segmentfault.com/img/remote/1460000014011338); 分享一些超好用插件,打造一个不一样的 GitHub、浏览器、...

    Rango 评论0 收藏0
  • GitHub 值得收藏的前端项目[每月更新...]

    摘要:也是一款优秀的响应式框架站点所使用的一套框架为微信服务量身设计的一套框架一组很小的,响应式的组件,你可以在网页的项目上到处使用一个可定制的文件,使浏览器呈现的所有元素,更一致和符合现代标准。 GitHub 值得收藏的前端项目 整理与收集的一些比较优秀github项目,方便自己阅读,顺便分享出来,大家一起学习,本篇文章会持续更新,版权归原作者所有。欢迎github star与fork 预...

    maxmin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<