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.
Features
- 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 compatiable: 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.
Installation
For legacy browsers
The default entry of NPM package only supports modern browsers. For legacy browsers (IE9+), There is also a ES5 bundle(dist/index.es5.js
) to get it work. Notice that polyfills are not included in these bundles 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.min.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>
React
import { Editor, Viewer } from '@bytemd/react';
import gfm from '@bytemd/plugin-gfm';
const plugins = [
gfm(),
];
const App = () => {
const [value, setValue] = useState('');
return (
<Editor
value={value}
plugins={plugins}
onChange={(v) => {
setValue(v);
}}
/>
);
};
Vue
<template>
<Editor :value="value" :plugins="plugins" @change="handleChange" />
</template>
<script>
import { Editor, Viewer } from '@bytemd/vue';
import gfm from '@bytemd/plugin-gfm';
const plugins = [
gfm(),
// Add more plugins here
];
export default {
components: { Editor },
data() {
return { value: '', plugins };
},
methods: {
handleChange(v) {
value = v;
},
},
};
</script>
Vanilla JS
import { Editor, Viewer } from 'bytemd';
import gfm from '@bytemd/plugin-gfm';
const plugins = [
gfm(),
];
const editor = new Editor({
target: document.body,
props: {
value: '',
plugins,
},
});
editor.on('change', (e) => {
editor.$set({ value: e.detail.value });
});
Options
Viewer
Key | Type | Description |
---|
value | string (required) | Markdown text |
plugins | BytemdPlugin[] | ByteMD plugin list |
sanitize | (schema: Schema) => Schema | Sanitize strategy |
Editor
Editor
component also accepts the options of Viewer
. Besides that, there are some other options:
Key | Type | Description |
---|
mode | `'split' | 'tab'` |
previewDebounce | number | Debounce time (ms) for preview |
editorConfig | documentation | CodeMirror editor config |
Style customization
The default height of ByteMD's body area (.bytemd-body
) is 300px
. It could be overrided by CSS:
.bytemd-body {
height: calc(100vh - 200px);
}
or by JavaScript (via plugin) if you want more control:
const stylePlugin = {
editorEffect({ $el }) {
const $body = $el.querySelector('.bytemd-body');
$body.style.height = 'calc(100vh - 200px)';
},
};
new Editor({
value: '',
plugins: [
stylePlugin,
],
});
The other styles could also be overrided, see the default style.
Technical details
ByteMD uses remark and rehype ecosystem to process Markdown. The complete process is as follows:
- The markdown text is parsed to an AST
- 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.
Plugins
Write a plugin
TODO
License
MIT