Go to file
Rongjian Zhang 706f5abb3c fix: bold icon style 2020-10-15 21:55:09 +08:00
.github/workflows test: fix tests 2020-09-29 13:21:10 +08:00
assets docs: add technical details 2020-07-21 16:22:47 +08:00
examples feat: add gfm plugin 2020-10-14 18:27:27 +08:00
packages fix: bold icon style 2020-10-15 21:55:09 +08:00
scripts docs: simplify plugin description 2020-10-14 19:22:50 +08:00
.gitignore chore: add type reference for vue and svelte files 2020-09-29 15:20:02 +08:00
.prettierignore build: fix readme generation of hyphen name 2020-03-31 19:20:35 +08:00
.prettierrc style: prettier write 2020-05-11 21:20:52 +08:00
LICENSE Initial commit 2020-02-10 20:32:52 +08:00
README.md chore: soft link readme 2020-07-21 17:00:24 +08:00
jest-setup.ts test: add editor cases 2020-07-11 19:23:33 +08:00
jest.config.js test: add editor cases 2020-07-11 19:23:33 +08:00
lerna.json v0.1.10 2020-10-12 20:52:01 +08:00
package.json build: add umd build 2020-10-15 20:12:25 +08:00
rollup.config.js build: add umd build 2020-10-15 20:12:25 +08:00
tsconfig-base.json chore: improve types 2020-09-29 16:15:48 +08:00
tsconfig.json feat: add gfm plugin 2020-10-14 18:27:27 +08:00

README.md

ByteMD

npm showcase test

ByteMD is a Markdown editor component built with Svelte. It could also be used in other libraries/frameworks such as React, Vue and Angular.

Note: It is still in development

Features

  1. 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.
  2. 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.
  3. 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.
  4. 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

Package Status Description
bytemd npm Svelte/Vanilla JS component
@bytemd/react npm React component
@bytemd/vue npm Vue component

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>

React

import { Editor, Viewer } from '@bytemd/react';
import gfm from '@bytemd/plugin-gfm';

const plugins = [
  gfm(),
  // Add more plugins here
];

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(),
  // Add more plugins here
];

const instance = new Editor({
  target: document.body, // DOM to render
  props: {
    value: '',
    plugins,
  },
});

instance.on('change', (e) => {
  const value = e.detail.value;
  console.log(value);
  // ...
});

Technical details

ByteMD uses remark and rehype ecosystem to process Markdown. The complete process is as follows:

  1. The markdown text is parsed to an AST
  2. The Markdown AST could be manipulated by several remark plugins
  3. The Markdown AST is transformed to a HTML AST
  4. The HTML AST is sanitized for security reason
  5. The HTML AST could be manipulated by several rehype plugins
  6. The HTML AST is stringified to HTML
  7. Some extra DOM manipulation after the HTML being rendered

It could also be described as a flowchart:

process

The 2,5,7 steps are designed for user customization via ByteMD plugin API.

Plugins

Package Status Description
@bytemd/plugin-breaks npm Support breaks
@bytemd/plugin-external-links npm Open external links in new window
@bytemd/plugin-footnotes npm Support footnotes
@bytemd/plugin-frontmatter npm Parse frontmatter
@bytemd/plugin-gfm npm Support GFM (autolink literals, strikethrough, tables, tasklists)
@bytemd/plugin-highlight npm Highlight code blocks
@bytemd/plugin-highlight-ssr npm Highlight code blocks (SSR compatible)
@bytemd/plugin-import-html npm Import HTML by pasting or dropping
@bytemd/plugin-import-image npm Import image by pasting or dropping
@bytemd/plugin-inject-style npm Support footnotes
@bytemd/plugin-math npm Support math equation
@bytemd/plugin-math-ssr npm Support math equation (SSR compatible)
@bytemd/plugin-medium-zoom npm Zoom images like Medium
@bytemd/plugin-mermaid npm Support Mermaid diagram and flowchart
@bytemd/plugin-scroll-sync npm Sync scroll position of edit and preview area
@bytemd/plugin-vega npm Support vega charts

Write a plugin

TODO

License

MIT