2.0.8 • Published 2 years ago

@deboxsoft/bytemd v2.0.8

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

ByteMD

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.

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 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.

Installation

PackageStatusDescription
bytemdnpm gzip sizeSvelte/Vanilla JS component
@bytemd/reactnpm gzip sizeReact component
@bytemd/vuenpm gzip sizeVue component

Legacy browsers support

The default entry of NPM package only supports modern browsers. There are two ways to make legacy browsers (IE9+) work:

  1. Compile it with ESNext -> ES5 transpilers, such as Babel
  2. Use the ES5 bundle(dist/index.es5.js)

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.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(),
  // 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) {
      this.value = v
    },
  },
}
</script>

Vanilla JS

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

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

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

editor.$on('change', (e) => {
  editor.$set({ value: e.detail.value })
})

Options

Viewer

KeyTypeDescription
valuestring (required)Markdown text
pluginsBytemdPlugin[]ByteMD plugin list
sanitize(schema: Schema) => SchemaSanitize strategy

Editor

Editor component also accepts the options of Viewer for preview. Besides that, there are some other options:

KeyTypeDescription
modesplit, tab, autoEditor display mode
previewDebouncenumberDebounce time (ms) for preview, default: 300
placeholderstringEditor placeholder
editorConfigdocumentationCodeMirror editor config
localei18n locale. Available locales could be found at bytemd/locales
uploadImagesfunctionSpecify how to upload images
maxLengthnumberMaximum length (number of characters) of value

Style customization

Editor

The default height of ByteMD Editor is 300px. It could be overridden by CSS:

.bytemd {
  height: calc(100vh - 200px);
}

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.

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
@deboxsoft/bytemd-plugin-breaksnpm gzip sizeSupport breaks
@deboxsoft/bytemd-plugin-footnotesnpm gzip sizeSupport footnotes
@deboxsoft/bytemd-plugin-frontmatternpm gzip sizeParse frontmatter
@deboxsoft/bytemd-plugin-gemojinpm gzip sizeSupport Gemoji shortcodes
@deboxsoft/bytemd-plugin-gfmnpm gzip sizeSupport GFM (autolink literals, strikethrough, tables, tasklists)
@deboxsoft/bytemd-plugin-highlightnpm gzip sizeHighlight code blocks
@deboxsoft/bytemd-plugin-highlight-ssrnpm gzip sizeHighlight code blocks (SSR compatible)
@deboxsoft/bytemd-plugin-mathnpm gzip sizeSupport math formula
@deboxsoft/bytemd-plugin-math-ssrnpm gzip sizeSupport math formula (SSR compatible)
@deboxsoft/bytemd-plugin-medium-zoomnpm gzip sizeZoom images like Medium
@deboxsoft/bytemd-plugin-mermaidnpm gzip sizeSupport Mermaid diagram

Write a plugin

TODO: plugin API not stable yet

Contributors

npm.io

License

MIT

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.8

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.8

2 years ago

2.0.0-beta.7

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.0

2 years ago

2.0.0-beta.6

2 years ago

2.0.0-beta.5

2 years ago

2.0.0-beta.4

2 years ago

2.0.0-beta.3

2 years ago

1.11.13

2 years ago

1.11.12

2 years ago

1.11.11

2 years ago

1.11.10

2 years ago

1.11.9

2 years ago

1.11.8

2 years ago

1.11.7

2 years ago

1.11.6

2 years ago

1.11.5

2 years ago

1.11.4

2 years ago

1.11.3

2 years ago

1.11.2

2 years ago

1.11.1

2 years ago

1.11.0

2 years ago

1.10.14

2 years ago

1.10.13

2 years ago