1.21.0 • Published 1 year ago

@bytemd/react v1.21.0

Weekly downloads
36
License
MIT
Repository
github
Last release
1 year ago

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

PackageStatusDescription
bytemdnpmSvelte/Vanilla JS component
@bytemd/reactnpmReact component
@bytemd/vuenpmVue 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

<template>
  <Editor {value} on:change={handleChange} plugins={[{/* Add plugins here */}]} />
</template>
<script>
import { Editor, Viewer } from 'bytemd'

let value;
function handleChange(e) {
  value = e.detail.value
}
</script>

React

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

const App = () => {
  const [value, setValue] = useState('');

  return (
    <Editor
      value={value}
      onChange={(v) => {
        setValue(v);
      }}
      plugins={
        [
          // Add plugins here
        ]
      }
    />
  );
};

Vue

<template>
  <Editor :value="value" @change="handleChange" :plugins="plugins" />
</template>

<script>
import { Editor, Viewer } from '@bytemd/vue';

export default {
  components: {
    Editor,
  },
  data() {
    return {
      value: '',
      plugins: [
        // Add plugins here
      ],
    };
  },
  methods: {
    handleChange(v) {
      value = v;
    },
  },
};
</script>

Vanilla JS

import { Editor, Viewer } from 'bytemd';

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

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

PackageStatusDescription
@bytemd/plugin-breaksnpmByteMD plugin to support breaks
@bytemd/plugin-external-linksnpmByteMD plugin to open external links in new window
@bytemd/plugin-footnotesnpmByteMD plugin to support footnotes
@bytemd/plugin-frontmatternpmByteMD plugin to parse frontmatter
@bytemd/plugin-highlightnpmByteMD plugin to highlight code blocks
@bytemd/plugin-highlight-ssrnpmByteMD plugin to highlight code blocks (SSR compatible)
@bytemd/plugin-import-htmlnpmByteMD plugin to import HTML by pasting or dropping
@bytemd/plugin-import-imagenpmByteMD plugin to import image by pasting or dropping
@bytemd/plugin-inject-stylenpmByteMD plugin to support footnotes
@bytemd/plugin-mathnpmByteMD plugin to support math equation
@bytemd/plugin-math-ssrnpmByteMD plugin to support math equation (SSR compatible)
@bytemd/plugin-medium-zoomnpmByteMD plugin to zoom images like Medium
@bytemd/plugin-mermaidnpmByteMD plugin to support Mermaid diagram and flowchart
@bytemd/plugin-scroll-syncnpmByteMD plugin to sync scroll position of edit and preview area
@bytemd/plugin-veganpmByteMD plugin to support vega charts

Write a plugin

TODO

License

MIT

1.21.0

1 year ago

1.19.0

1 year ago

1.20.1

1 year ago

1.20.2

1 year ago

1.20.0

1 year ago

1.18.0

1 year ago

1.17.4

2 years ago

1.17.3

2 years ago

1.17.2

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.16.0

2 years ago

1.15.0

2 years ago

1.14.0

2 years ago

1.12.3

2 years ago

1.13.1

2 years ago

1.12.2

2 years ago

1.13.0

2 years ago

1.12.1

2 years ago

1.12.4

2 years ago

1.11.0

2 years ago

1.10.13

3 years ago

1.10.8

3 years ago

1.10.6

3 years ago

1.10.2

3 years ago

1.9.2

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.10

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago