Giter Site home page Giter Site logo

changsongyang / bytemd Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pd4d10/hashmd

0.0 1.0 0.0 43.96 MB

A hackable Markdown editor component built with Svelte

Home Page: https://bytedance.github.io/bytemd/

License: MIT License

JavaScript 21.62% TypeScript 63.23% Vue 2.54% Svelte 10.44% SCSS 2.17%

bytemd's Introduction

ByteMD

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 gzip size Svelte/Vanilla JS component
@bytemd/react npm gzip size React component
@bytemd/vue npm gzip size 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 gzip size Support breaks
@bytemd/plugin-external-links npm gzip size Open external links in new window
@bytemd/plugin-footnotes npm gzip size Support footnotes
@bytemd/plugin-frontmatter npm gzip size Parse frontmatter
@bytemd/plugin-gemoji npm gzip size Support Gemoji shortcodes
@bytemd/plugin-gfm npm gzip size Support GFM (autolink literals, strikethrough, tables, tasklists)
@bytemd/plugin-highlight npm gzip size Highlight code blocks
@bytemd/plugin-highlight-ssr npm gzip size Highlight code blocks (SSR compatible)
@bytemd/plugin-import-html npm gzip size Import HTML by pasting or dropping
@bytemd/plugin-import-image npm gzip size Import image by pasting or dropping
@bytemd/plugin-inject-style npm gzip size Inject style to markdown body
@bytemd/plugin-math npm gzip size Support math equation
@bytemd/plugin-math-ssr npm gzip size Support math equation (SSR compatible)
@bytemd/plugin-medium-zoom npm gzip size Zoom images like Medium
@bytemd/plugin-mermaid npm gzip size Support Mermaid diagram and flowchart
@bytemd/plugin-vega npm gzip size Support vega charts

Write a plugin

TODO

License

MIT

bytemd's People

Contributors

jiaoyin avatar kefahi avatar pd4d10 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.