Lightweight and framework agnostic: ByteMD is built with Svelte. It compiles to vanilla JS DOM manipulation without importing any UI Framework runtime bundle, which makes it lightweight, and easily adapted to other libraries/frameworks.
Easy to extend: ByteMD has a plugin system to extend the basic Markdown syntax, which makes it easy to add additional features such as code syntax highlight, math equation and Mermaid flowcharts. You can also write your own plugin if these ones don’t meet your needs.
Secure by default: Cross-site scripting(XSS) attack such as <script> and <img onerror> have been correctly handled by ByteMD. No need to introduce extra DOM sanitize steps.
SSR compatible: ByteMD could be used in the Server-side rendering(SSR) environment without extra config. SSR is widely used in some cases due to its better SEO and fast time-to-content in slow network connection.
The default entry of NPM package only supports modern browsers. To make legacy browsers (IE9+) work, You can compile it with ESNext -> ES5 transpilers, such as Babel or SWC.
The ES5 bundle will no longer be available after version 1.11.0. If you need it, you can use version 1.11.0 or earlier versions
Notice that polyfills are not included, and should be imported manually, see the legacy browser example.
Usage
There are two components: Editor and Viewer. Editor is the Markdown editor, as the name suggests; Viewer is commonly used to display rendered Markdown results without editing.
Before using the component, remember to import CSS file to make styles correct:
import 'bytemd/dist/index.css'
Svelte
<script>
import { Editor, Viewer } from 'bytemd'
import gfm from '@bytemd/plugin-gfm'
let value
const plugins = [
gfm(),
// Add more plugins here
]
function handleChange(e) {
value = e.detail.value
}
</script>
<template>
<Editor {value} {plugins} on:change={handleChange} />
</template>
ByteMD provides a powerful plugin system for customization. There are several official plugins to support features such as code syntax highlight, math equation and Mermaid flowcharts.
If you have more customized needs, you could also write your own plugin to support them.
The Markdown AST could be manipulated by several remark plugins
The Markdown AST is transformed to a HTML AST
The HTML AST is sanitized for security reason
The HTML AST could be manipulated by several rehype plugins
The HTML AST is stringified to HTML
Some extra DOM manipulation after the HTML being rendered
It could also be described as a flowchart:
The 2,5,7 steps are designed for user customization via ByteMD plugin API.
Authoring a Plugin
We’ll take Math formula plugin as an example to walk you through the process.
First of all, scaffold the project according to the BytemdPlugin type signature:
import type { BytemdPlugin } from 'bytemd'
export default function mathPlugin(): BytemdPlugin {
return {
// to be implement
}
}
Then we look into the requirement more closely: If we want to render syntax like $a+b$ to Math formula, there are several things we need to do:
Support $a+b$ syntax as a Math formula in Markdown (step 2)
Render these nodes correctly in HTML (step 5 or 7)
Additionally, an extra icon in the toolbar would be great for user convenience
For the first thing, luckily, we don’t need to implement it with our own because remark-math already did it. The only thing we need to do is to import and use it:
import type { BytemdPlugin } from 'bytemd'
+import remarkMath from 'remark-math'
export default function mathPlugin(): BytemdPlugin {
return {
- // to be implement
+ remark: (processor) => processor.use(remarkMath),
}
}
Then consider the second thing, it would be a little complicated because we have two choices, do it in step 5 or 7. The difference is that step 5 is more friendly with SSR, while step 7 hand over the rendering to the client-side. This is why we have two plugin: @bytemd/plugin-math and @bytemd/plugin-math-ssr.
// if we choose step 5:
import type { BytemdPlugin } from 'bytemd'
import remarkMath from 'remark-math'
+import rehypeKatex from 'rehype-katex'
export default function mathPlugin(): BytemdPlugin {
return {
remark: (processor) => processor.use(remarkMath),
+ rehype: (processor) => processor.use(rehypeKatex),
}
}
// if we choose step 7:
import type { BytemdPlugin } from 'bytemd'
import remarkMath from 'remark-math'
+import rehypeKatex from 'rehype-katex'
export default function mathPlugin(): BytemdPlugin {
return {
remark: (processor) => processor.use(remarkMath),
+ viewerEffect({ markdownBody }) {
+ const renderMath = async (selector: string, displayMode: boolean) => {
+ const katex = await import('katex').then((m) => m.default)
+
+ const els = markdownBody.querySelectorAll<HTMLElement>(selector)
+ els.forEach((el) => {
+ katex.render(el.innerText, el, { displayMode })
+ })
+ }
+
+ renderMath('.math.math-inline', false)
+ renderMath('.math.math-display', true)
+ },
}
}
The last thing is to add an icon to the toolbar. we use the actions prop to implement it:
export default function mathPlugin(): BytemdPlugin {
return {
actions: [
{
title: 'Insert an math formula',
icon: '', // 16x16 SVG icon
handler: {
type: 'action',
click(ctx) {
// to be implement:
// the `ctx` is an instance of `BytemdEditorContext`, which has
// several utility methods to help operate the CodeMirror editor state.
// remember to call `focus` to avoid lost of focus
editor.focus()
},
},
},
],
}
}
Now we have completed a minimalist version of the plugin! For more details and references please check out the source code.
ByteMD
ByteMD is a Markdown editor component built with Svelte. It could also be used in other libraries/frameworks such as React, Vue and Angular.
Playground here: https://bytemd.js.org/playground/
Features
<script>
and<img onerror>
have been correctly handled by ByteMD. No need to introduce extra DOM sanitize steps.Installation
Legacy browsers support
The default entry of NPM package only supports modern browsers. To make legacy browsers (IE9+) work, You can compile it with ESNext -> ES5 transpilers, such as Babel or SWC.
Notice that polyfills are not included, and should be imported manually, see the legacy browser example.
Usage
There are two components:
Editor
andViewer
.Editor
is the Markdown editor, as the name suggests;Viewer
is commonly used to display rendered Markdown results without editing.Before using the component, remember to import CSS file to make styles correct:
Svelte
React
Vue
Vanilla JS
Options
Viewer
value
string
(required)plugins
BytemdPlugin[]
sanitize
(schema: Schema) => Schema
remarkRehype
Editor
Editor
component also accepts the options ofViewer
for preview. Besides that, there are some other options:mode
split
,tab
,auto
auto
previewDebounce
number
300
placeholder
string
editorConfig
locale
bytemd/locales
, default: useen.json
uploadImages
function
maxLength
number
Style customization
Editor
The default height of ByteMD Editor is
300px
. It could be overridden by CSS:The other styles could also be overridden, see the default style.
Viewer
There is no built-in styles for the Viewer. You could use third-party markdown themes, for example juejin-markdown-themes and github-markdown-css.
Plugin System
ByteMD provides a powerful plugin system for customization. There are several official plugins to support features such as code syntax highlight, math equation and Mermaid flowcharts.
If you have more customized needs, you could also write your own plugin to support them.
Official Plugins
Technical Overview
ByteMD uses remark and rehype ecosystem to process Markdown. The complete process is as follows:
It could also be described as a flowchart:
The 2,5,7 steps are designed for user customization via ByteMD plugin API.
Authoring a Plugin
We’ll take Math formula plugin as an example to walk you through the process.
First of all, scaffold the project according to the
BytemdPlugin
type signature:Then we look into the requirement more closely: If we want to render syntax like
$a+b$
to Math formula, there are several things we need to do:$a+b$
syntax as a Math formula in Markdown (step 2)For the first thing, luckily, we don’t need to implement it with our own because remark-math already did it. The only thing we need to do is to import and use it:
Then consider the second thing, it would be a little complicated because we have two choices, do it in step 5 or 7. The difference is that step 5 is more friendly with SSR, while step 7 hand over the rendering to the client-side. This is why we have two plugin: @bytemd/plugin-math and @bytemd/plugin-math-ssr.
The last thing is to add an icon to the toolbar. we use the
actions
prop to implement it:Now we have completed a minimalist version of the plugin! For more details and references please check out the source code.
Contributors
License
MIT