TOAST UI Editor

repository·master·Indexed 12 days ago

https://github.com/nhn/tui.editor

A highly extensible Markdown and WYSIWYG editor supporting GFM and CommonMark specifications. It features a rich plugin ecosystem, including a Chart plugin, and provides official framework wrappers for React (@toast-ui/react-editor) and Vue (@toast-ui/vue-editor). The editor includes a read-only Viewer component and utilizes ToastMark, an incremental markdown parser extended from commonmark.js.

Tokens
40.3K
Snippets
144
Records
172
Agent score
93%

What's inside TOAST UI Editor

  1. Overview of TOAST UI Editor packages

    master

    TOAST UI Editor is a GFM (GitHub Flavored Markdown) and WYSIWYG editor that supports both Markdown and visual editing modes. It is available as a plain JavaScript component, framework-specific wrappers, and a variety of functional plugins.

    Core Package

    • @toast-ui/editor: The primary plain JavaScript component.

    Framework Wrappers

    • @toast-ui/react-editor: A wrapper for React applications.
    • @toast-ui/vue-editor: A wrapper for Vue.js applications.

    Available Plugins

    • @toast-ui/editor-plugin-chart: Renders charts via TOAST UI Chart.
    • @toast-ui/editor-plugin-code-syntax-highlight: Provides syntax highlighting using Prism.js.
    • @toast-ui/editor-plugin-color-syntax: Adds a GUI color picker to change text color.
    • @toast-ui/editor-plugin-table-merged-cell: Enables merging table columns in headers and bodies.
    • @toast-ui/editor-plugin-uml: Renders UML diagrams via PlantUML.
  2. Overview of ToastMark

    master
    ToastMark is a markdown parser extended from commonmark.js designed specifically for use within the TOAST UI Editor. It provides advanced features for markdown editing that go beyond the standard CommonMark specification, including support for GitHub Flavored Markdown (GFM), detailed source position information, and incremental parsing to improve performance during editing.
  3. What is the TOAST UI Editor Viewer?

    master
    The Viewer is a lightweight component provided by TOAST UI Editor designed specifically for displaying Markdown content. Use the Viewer instead of the full Editor when you only need to render Markdown without the editing interface, as it is significantly lighter in terms of resource usage.
  4. Define Plugins with Commands and Renderers

    master

    Plugins in v3.0 are defined by injecting specific option objects. Key capabilities include:

    Registering Commands

    Use markdownCommands and wysiwygCommands to register new commands. Each command receives (payload, state, dispatch).

    return {
      markdownCommands: {
        myCommand: (payload, state, dispatch) => {
          // ...
        },
      },
      wysiwygCommands: {
        myCommand: (payload, state, dispatch) => {
          // ...
        },
      },
    };

    Customizing Converters

    Use toHTMLRenderers (Markdown $\rightarrow$ WYSIWYG/Preview) and toMarkdownRenderers (WYSIWYG $\rightarrow$ Markdown) to intercept and modify how specific nodes are rendered during conversion.

    return {
      toHTMLRenderers: {
        tableCell(node, { entering, origin }) {
          const result = origin!();
          // ...
          return result;
        },
      },
      toMarkdownRenderers: {
        tableHead(nodeInfo) {
          // ...
          return { delim };
        },
      },
    };

    Registering Toolbar Items

    Add items to the toolbar by specifying their target group and position using groupIndex and itemIndex.

    return {
      toolbarItems: [
        {
          groupIndex: 0,
          itemIndex: 3,
          item: toolbarItem,
        },
      ],
    };
  5. What are TOAST UI Editor plugins?

    master

    Plugins are extensions that add specific functionality to the TOAST UI Editor. The editor provides five official plugins:

    • chart (@toast-ui/editor-plugin-chart): Renders charts.
    • code-syntax-highlight (@toast-ui/editor-plugin-code-syntax-highlight): Highlights code syntax.
    • color-syntax (@toast-ui/editor-plugin-color-syntax): Adds color editing capabilities to text.
    • table-merged-cell (@toast-ui/editor-plugin-table-merged-cell): Enables merging table cells.
    • uml (@toast-ui/editor-plugin-uml): Renders UML diagrams.
  6. How Popup Widgets and Inline Widget Nodes differ

    master

    The TOAST UI Editor provides two ways to display widgets, depending on whether they should be part of the document content:

    1. Popup Widgets: Created via addWidget(). These are floating DOM nodes that are not part of the editor's content. They are temporary and disappear when the user interacts with the editor (typing or focus change). Use these for suggestion menus or tooltips.

    2. Inline Widget Nodes: Created via the widgetRules configuration. These are inserted into the editor as actual content. They affect the position of other nodes and are rendered when text matches a specific pattern. Use these for features like 'mentions' or custom link formats that should persist in the document.

  7. Customize Toolbar Popups via options

    master

    In v3.0, you can customize toolbar popups (like a color picker) by providing a simple configuration object instead of manually managing UI instances and event listeners. You define the className, style, and body properties within a popup object.

    const popup = {
      name: 'color',
      tooltip: 'Text color',
      className: 'toastui-editor-toolbar-icons color',
      popup: {
        className: 'toastui-editor-popup-color',
        body: colorPickerContainer,
        style: { width: 'auto' },
      },
    };
    const popup = {
      name: 'color',
      tooltip: 'Text color',
      className: 'toastui-editor-toolbar-icons color',
      popup: {
        className: 'toastui-editor-popup-color',
        body: colorPickerContainer,
        style: { width: 'auto' },
      },
    };
  8. Key features of ToastMark

    master

    ToastMark differs from commonmark.js in several ways to support high-performance editing:

    • GitHub Flavored Markdown (GFM) Support: Includes a custom implementation for GFM syntax.
    • Source Position Information: Unlike commonmark.js which limits position data to block-level elements, ToastMark provides source position information for both block-level and inline-level elements in the Abstract Syntax Tree (AST).
    • Incremental Parsing: Instead of re-parsing the entire document on every change, ToastMark parses only the modified parts and updates the existing AST. It returns information about removed and inserted nodes, enabling incremental updates for syntax highlighting and preview rendering.
    • AST Searching and Editing: Provides methods to interact with the AST, such as findNodeAtPosition and findNodeById, which are useful for synchronizing scroll positions between the editor and preview or updating toolbar states based on cursor position.
  9. Access the plugin 'context' parameter

    master

    Plugin functions receive a context parameter that provides access to the editor's core modules and utilities:

    • eventEmitter: Used to communicate with the editor.
    • usageStatistics: Controls whether plugin usage is collected for GA.
    • i18n: Instance for internationalization.
    • pmState: Access to prosemirror-state modules.
    • pmView: Access to prosemirror-view modules.
    • pmModel: Access to prosemirror-model modules.
  10. How Markdown and WYSIWYG modes work

    master

    TOAST UI Editor provides two distinct modes that can be switched at any time:

    Markdown Mode

    Follows both CommonMark and GFM (GitHub Flavored Markdown) specifications. Key features include:

    • Live Preview: Real-time HTML rendering as you edit Markdown.
    • Scroll Sync: Synchronous scrolling between the Markdown editor and the Preview pane.
    • Syntax Highlight: Immediate feedback on Markdown syntax validity.

    WYSIWYG Mode

    A visual editing experience with features like:

    • Table Editing: Context menus to add/delete rows/columns and arrange text.
    • Custom Block Editor: Ability to edit custom block areas through an internal editor.
    • Rich Paste: Supports pasting content from browsers, screenshots, Excel, and PowerPoint.
  11. Handle node traversal with context.entering and context.origin()

    master

    When the Editor traverses the AST, it visits non-leaf nodes twice: once when entering the node and once after visiting all its children. You can use the context object to manage this lifecycle:

    context.entering

    Use this boolean to distinguish between the start and end of a node. This is essential for returning an openTag when entering is true and a closeTag when false.

    context.origin()

    If you want to extend the default behavior rather than completely replacing it, call context.origin(). This function returns the token object produced by the Editor's original converter for that node type, allowing you to modify its properties (like adding an attribute) before returning it.

    // Using entering to toggle tags
    heading({ level }, { entering }) {
      return {
        type: entering ? 'openTag' : 'closeTag',
        tagName: `h${level}`
      };
    }
    
    // Using origin() to extend default behavior
    link(node, context) {
      const { origin, entering } = context;
      const result = origin(); // Get default token
      if (entering) {
        result.attributes.target = '_blank'; // Add custom attribute
      }
      return result;
    }
  12. Core features of TOAST UI Editor

    master

    Beyond standard editing, the editor includes several advanced features:

    • Viewer: A mode to display rendered Markdown data without an editing interface.
    • Internationalization (i18n): Supports a wide range of languages (English, Korean, Japanese, Chinese, Spanish, etc.) and is extensible.
    • Widget: Allows replacing specific string matches (via RegExp) with custom widget nodes.
    • Custom Block: Enables defining nodes not natively supported by Markdown by providing custom parsing logic.