搜索

查看: 3156|回复: 11

[JavaScript] 一款功能强大的markdown编辑器tui.editor使用示例详解

[复制链接]
发表于 2023-5-4 11:49:57 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:49:57 3156 11 看全部
目录
  • 简介
  • 安装使用
  • 安装
  • 初始化
  • 官方插件
  • 功能拓展
  • 实现源码
    简介
    最近在捯饬自己的个人网站,想找一款类似于掘金的markdown编辑器,主要诉求包含实时预览、语法高亮、自动生成目录索引。对比了市面上主流的几款编辑器,最后采用了@toast-ui/editor。选择的主要原因就是开箱即用,内置一些实用的插件,如表格并且支持合并单元格、语法高亮、图形展示、uml绘制等;支持自定义插件扩展,因为这款编辑器是基于prosemirror,前身即codemirror,编辑器本身是偏底层的,提供了丰富的api供我们自定义开发,这也大大增强了编辑器的灵活性,如果想加一个目录索引,我们完全可以自定义开发一个插件使用。
    在初次使用过程中,也遇到一些注意点,本文以vue3为例,简单介绍@toast-ui/editor的使用过程。

    安装使用
    安装
    npm install @toast-ui/editor -S

    初始化
    import Editor from '@toast-ui/editor'
    import '@toast-ui/editor/dist/toastui-editor.css'
    import '@toast-ui/editor/dist/i18n/zh-cn';
    export default {
        mounted () {
          const editor = new Editor({
            el: this.$refs.editor,
            language: 'zh-CN',
            initialEditType: 'markdown',
            previewStyle: 'vertical',
          });
        }
      }
    通过以上两步,我们就能得到一个简易的编辑器了,如下图所示:

    20230220083232020.png

    20230220083232020.png


    显然我们的目的不仅如此,markdown编辑器还缺少语法高亮、目录栏,接下来我们看下如何扩展tui

    官方插件
    官方内置了以下插件:
    插件名称用途
    @toast-ui/editor-plugin-chart图形渲染
    @toast-ui/editor-plugin-code-syntax-highlight语法高亮
    @toast-ui/editor-plugin-color-syntax文本添加颜色
    @toast-ui/editor-plugin-table-merged-cell合并单元格
    @toast-ui/editor-plugin-uml渲染UML

    接下来我们配置代码语法高亮。
  • 安装插件
    npm install @toast-ui/editor-plugin-code-syntax-highlight
  • 使用
    import 'prismjs/themes/prism.css';
    import '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight.css';
    import Editor from '@toast-ui/editor';
    // 支持所有语言语法高亮
    import codeSyntaxHighlight from '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js';
    const editor = new Editor({
      // ...
      plugins: [codeSyntaxHighlight]
    });

    功能拓展
    目前编辑器包含了语法高亮,如果需要添加目录索引,可以监听文档编辑的change事件,获取markdown文档内容,通过正则表达式解析即可。具体实现如下:
    const editor = new Editor({
      // ...
      events: {
        change: this.handleContentChange.bind(this)
      },
    });
    methods: {
      handleContentChange () {
        const mdText = this.editor.mdEditor.getMarkdown()
        this.parseMdTitle(mdText)
      },
      parseMdTitle (mdText) { // 解析markdown title
        const pattern = /^(#+)\s+(.+)/mg
        let result = mdText.match(pattern)
        if (!result) return
        const catalogList = result.map((vv, index) => {
          const levelText = vv.match(/^(#+)/)
          return {
            level: levelText[0].length, // 目录级别
            index,
            cls: `heading-${levelText[0].length}`,
            content: vv.slice(levelText[0].length).trim(), // 内容
          }
        })
        this.catalogList = catalogList
      }
    }
    以上仅仅是一些基础的使用。markdown基础语法无法满足我们需要时、需要手动修改渲染样式等需求,tui.editor也提供相应的能力。如需要修改标题的默认渲染样式,我们可以使用customHTMLRenderer,这一块官方文档较少,可以从源码看出默认书写规则,内置schema位置详见源码libs\toastmark\src\html\baseConvertors.ts。
    new Editor({
      // ...
      customHTMLRenderer: {
        heading (node, { entering }) {
          const spec = {
            type: entering ? 'openTag' : 'closeTag',
            tagName: `h${node.level}`,
            outerNewLine: true,
          };
          // 给每个header添加class
          if (entering) spec.attributes = {
            'class': `heading${node.level}`
          }
          return spec
        }
      }
    })
    最新3.0版本的编辑器是基于Prosemirror,有兴趣的小伙伴可以去看下,功能十分强大,也是level1级富文本编辑器的典型代表。
    编辑器最终效果图如下:

    20230220083232021.png

    20230220083232021.png


    实现源码
      
       
         0">
          目录
          
            
              [url='#heading' + (index + 1)]{{item.content}}[/url]
            
          
       
      

      .full {
        position: relative
      }
      .catalog-container {
        box-sizing: border-box;
        position: absolute;
        right: 0;
        bottom: 32px;
        width: 200px;
        height: 300px;
        padding: 16px 0;
        background-color: rgba(255, 255, 255, .65);
        border: 1px solid #ccc;
        border-radius: 4px;
      }
      .catalog-title {
        text-align: center;
        padding-bottom: 12px;
      }
      .catalog-item {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding: 4px 8px;
        font-size: 14px;
        user-select: none;
      }
      .catalog-item a {
        color: rgba(0, 0, 0, .65);
        text-decoration: none;
      }
      .heading-2 {
        padding-left: 24px;
      }
      .heading-3 {
        padding-left: 48px;
      }
      .catalog-item a:hover {
        color: cadetblue;
      }
      .markdown-editor {
        height: 100% !important;
        background: #fff;
        border-radius: 4px;
      }

    参考资料
  • toastui/editor
  • tui.editor
    以上就是一款功能强大的markdown编辑器tui.editor使用示例详解的详细内容,更多关于markdown编辑器tui.editor的资料请关注知鸟论坛其它相关文章!
  • 回复

    使用道具 举报

    发表于 2023-6-28 16:50:28 | 显示全部楼层
    麻辣鸡翅 2023-6-28 16:50:28 看全部
    我看不错噢 谢谢楼主!知鸟论坛越来越好!
    回复

    使用道具 举报

    发表于 2023-6-28 21:00:32 | 显示全部楼层
    落败的青春阳落s 2023-6-28 21:00:32 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-29 14:30:51 | 显示全部楼层
    dxf17 2023-6-29 14:30:51 看全部
    楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
    回复

    使用道具 举报

    发表于 2023-6-29 23:05:32 | 显示全部楼层
    音乐之家1 2023-6-29 23:05:32 看全部
    既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
    回复

    使用道具 举报

    发表于 2023-6-30 03:24:31 | 显示全部楼层
    123456848 2023-6-30 03:24:31 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-6-30 10:18:29 | 显示全部楼层
    十二音阶囤 2023-6-30 10:18:29 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-7-4 01:41:01 | 显示全部楼层
    462710480 2023-7-4 01:41:01 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    发表于 2023-7-4 13:57:05 | 显示全部楼层
    123456819 2023-7-4 13:57:05 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-7-6 01:45:59 | 显示全部楼层
    123456823 2023-7-6 01:45:59 看全部
    楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
    回复

    使用道具 举报

    • 您可能感兴趣
    点击右侧快捷回复 【请勿灌水】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则 返回列表

    RSS订阅| SiteMap| 小黑屋| 知鸟论坛
    联系邮箱E-mail:zniao@foxmail.com
    快速回复 返回顶部 返回列表