ByteMD Documentation

repository·main·Indexed 23 days ago

https://github.com/pd4d10/bytemd

ByteMD is a lightweight, framework-agnostic Markdown editor and viewer component built with Svelte. It features a robust plugin system for extending Markdown syntax and is designed for integration into React, Vue, and Angular applications. Available plugins include @bytemd/plugin-gfm for GitHub Flavored Markdown, @bytemd/plugin-highlight and @bytemd/plugin-highlight-ssr for code syntax highlighting, @bytemd/plugin-math-ssr for math formulas, as well as support for frontmatter, gemoji, and line breaks.

Tokens
21.5K
Snippets
49
Records
95
Agent score
80%

What's inside ByteMD

  1. Supported Markdown Syntax and Extensions

    main

    ByteMD supports standard Markdown syntax and various extended features through plugins. Supported syntax includes:

    • Basic Syntax: Bold (**text**), Italics (_text_), Links ([text](url)), Ordered/Unordered Lists, and Blockquotes.
    • GFM (GitHub Flavored Markdown): Automatic URL linking, Strikethrough (~~text~~), Task lists (- [x]), and Tables.
    • Footnotes: Using [^1] syntax for references and [^1]: content for definitions.
    • Gemoji: Emoji shortcodes like :+1: or :family_man_man_boy_boy:.
    • Math Equations: Inline math using $a+b$ and block math using $$ delimiters.
    • Mermaid Diagrams: Rendering diagrams using mermaid code blocks.
  2. How the ByteMD plugin system works

    main

    ByteMD leverages the remark and rehype ecosystems to process Markdown through a multi-step pipeline. Customization is primarily achieved through three specific steps in this lifecycle:

    1. Markdown Parsing: Text is parsed into a Markdown AST (mdast).
    2. Remark Manipulation (Step 2): The Markdown AST is modified using remark plugins. (Customizable)
    3. HTML Transformation: The Markdown AST is converted to an HTML AST.
    4. Sanitization: The HTML AST is sanitized for security.
    5. Rehype Manipulation (Step 5): The HTML AST is modified using rehype plugins. (Customizable)
    6. Stringification: The HTML AST is converted to an HTML string.
    7. DOM Manipulation (Step 7): Extra DOM manipulation occurs after rendering. (Customizable)

    Developers can hook into steps 2, 5, and 7 to implement custom logic.

  3. How Editor and Viewer work together

    main

    ByteMD consists of two primary components:

    1. Editor: A full Markdown editor component that allows users to write and edit Markdown content. It includes a preview mode.
    2. Viewer: A component used to display rendered Markdown results without editing capabilities.

    Important: Before using either component, you must import the ByteMD CSS file to ensure correct styling:

    import 'bytemd/dist/index.css'
  4. Understand the Nuxt.js directory structure in ByteMD examples

    main

    The ByteMD Nuxt.js example follows the standard Nuxt.js directory structure. While only the pages directory is strictly required, the following directories provide specific functionalities:

    • assets: Contains uncompiled assets like Stylus, Sass, images, or fonts.
    • components: Contains reusable Vue.js components.
    • layouts: Used for defining different page structures (e.g., sidebars or mobile/desktop views).
    • pages: Contains application views and routes. Nuxt automatically configures Vue Router based on *.vue files here.
    • plugins: For JavaScript plugins that run before the root Vue.js application is instantiated. Use this for Vue.use() calls and register the file paths in nuxt.config.js.
    • static: Contains static files mapped to the root /. For example, /static/robots.txt is accessible at /robots.txt.
    • store: Contains Vuex store files. Creating files here automatically activates Vuex.
  5. Use @bytemd/plugin-math-ssr for math formula support

    main

    The @bytemd/plugin-math-ssr plugin enables math formula support in ByteMD while remaining compatible with Server-Side Rendering (SSR). To use it, you must import the plugin, include it in the plugins array of your Editor configuration, and ensure that the KaTeX CSS is imported into your project to render the formulas correctly.

    Note: You must import katex/dist/katex.css for the math formulas to display with proper styling.

    import math from '@bytemd/plugin-math-ssr'
    import { Editor } from 'bytemd'
    import 'katex/dist/katex.css'
    
    new Editor({
      target: document.body,
      props: {
        plugins: [
          math(),
          // ... other plugins
        ],
      },
    })
  6. Use @bytemd/plugin-math to support math formulas

    main

    The @bytemd/plugin-math plugin enables math formula support within the ByteMD editor. To use it, you must import the math plugin, register it in the plugins array of the Editor configuration, and importantly, import the KaTeX CSS to ensure formulas are rendered correctly.

    Requirements:

    • bytemd
    • @bytemd/plugin-math
    • katex (for the CSS)
    import math from '@bytemd/plugin-math'
    import { Editor } from 'bytemd'
    import 'katex/dist/katex.css'
    
    new Editor({
      target: document.body,
      props: {
        plugins: [
          math(),
          // ... other plugins
        ],
      },
    })
  7. Develop and build the Svelte project

    main

    After creating the project and installing dependencies (npm install, pnpm install, or yarn), use the following commands to manage your development lifecycle:

    Start development server:

    npm run dev

    Start development server and open in browser:

    npm run dev -- --open

    Build for production:

    npm run build

    Preview production build:

    npm run preview

    Note: To deploy, you may need to install a SvelteKit adapter for your specific target environment.

  8. Recommended IDE Setup for Vue 3 and TypeScript

    main

    To ensure proper development experience with Vue 3 and TypeScript, use the following VSCode configuration:

    1. Extensions: Install Volar and TypeScript Vue Plugin (Volar).
    2. Disable Vetur: Ensure the Vetur extension is disabled to avoid conflicts with Volar.
    3. Type Support: Because TypeScript cannot handle .vue imports by default, this project uses vue-tsc instead of tsc for type checking.

    Enabling Volar Take Over Mode

    For better performance, you can enable Volar's 'Take Over Mode' by disabling the built-in TypeScript extension:

    1. Open the VSCode command palette and run Extensions: Show Built-in Extensions.
    2. Find TypeScript and JavaScript Language Features, right-click, and select Disable (Workspace).
    3. Run Developer: Reload Window from the command palette.
  9. Install and use @bytemd/plugin-mermaid

    main

    To add Mermaid diagram support to your ByteMD editor, install the @bytemd/plugin-mermaid package and include it in the plugins array of your Editor configuration. The plugin is initialized by calling mermaid() as a function within the plugins list.

    import mermaid from '@bytemd/plugin-mermaid'
    import { Editor } from 'bytemd'
    
    new Editor({
      target: document.body,
      props: {
        plugins: [
          mermaid(),
          // ... other plugins
        ],
      },
    })