Git Product home page Git Product logo

react-markdown-editor-lite's Introduction

react-markdown-editor-lite

NPM package NPM downloads MIT License Workflow

中文说明

  • A light-weight(20KB zipped) Markdown editor of React component
  • Supports TypeScript
  • Supports custom markdown parser
  • Full markdown support
  • Supports pluggable function bars
  • Full control over UI
  • Supports image uploading and dragging
  • Supports synced scrolling between editor and preview
  • 一款轻量的基于 React 的 Markdown 编辑器, 压缩后代码只有 20KB
  • 支持 TypeScript
  • 支持自定义 Markdown 解析器
  • 支持常用的 Markdown 编辑功能,如加粗,斜体等等...
  • 支持插件化的功能键
  • 界面可配置, 如只显示编辑区或预览区
  • 支持图片上传或拖拽
  • 支持编辑区和预览区同步滚动

Demo

Online demo
https://harrychen0506.github.io/react-markdown-editor-lite/

Default configuration

image

Pluggable bars

image

Install

npm install react-markdown-editor-lite --save
# or
yarn add react-markdown-editor-lite

Basic usage

Following steps:

  • Import react-markdown-editor-lite
  • Register plugins if required
  • Initialize a markdown parser, such as markdown-it
  • Start usage
// import react, react-markdown-editor-lite, and a markdown parser you like
import React from 'react';
import * as ReactDOM from 'react-dom';
import MarkdownIt from 'markdown-it';
import MdEditor from 'react-markdown-editor-lite';
// import style manually
import 'react-markdown-editor-lite/lib/index.css';

// Register plugins if required
// MdEditor.use(YOUR_PLUGINS_HERE);

// Initialize a markdown parser
const mdParser = new MarkdownIt(/* Markdown-it options */);

// Finish!
function handleEditorChange({ html, text }) {
  console.log('handleEditorChange', html, text);
}
export default props => {
  return (
    <MdEditor style={{ height: '500px' }} renderHTML={text => mdParser.render(text)} onChange={handleEditorChange} />
  );
};

Usage in server-side render

If you are using a server-side render framework, like Next.js, Gatsby, please use client-side render for this editor.

For example, Next.js has next/dynamic, Gatsby has loadable-components

Following is a example for Next.js:

import dynamic from 'next/dynamic';
import 'react-markdown-editor-lite/lib/index.css';

const MdEditor = dynamic(() => import('react-markdown-editor-lite'), {
  ssr: false,
});

export default function() {
  return <MdEditor style={{ height: '500px' }} renderHTML={/* Render function */} />;
}

With plugins:

import dynamic from 'next/dynamic';
import 'react-markdown-editor-lite/lib/index.css';

const MdEditor = dynamic(
  () => {
    return new Promise(resolve => {
      Promise.all([
        import('react-markdown-editor-lite'),
        import('./my-plugin'),
        /** Add more plugins, and use below */
      ]).then(res => {
        res[0].default.use(res[1].default);
        resolve(res[0].default);
      });
    });
  },
  {
    ssr: false,
  },
);

export default function() {
  return <MdEditor style={{ height: '500px' }} renderHTML={/* Render function */} />;
}

Full example see here

Import in Browser

Since 1.1.0, You can add script and link tags in your browser and use the global variable ReactMarkdownEditorLite.

You can download these files directly from cdnjs jsdelivr unpkg

Note: you should import react before ReactMarkdownEditorLite.

For example, in webpack, you import ReactMarkdownEditorLite by script tag in your page, and write webpack config like this:

externals: {
  react: 'React',
  'react-markdown-editor-lite': 'ReactMarkdownEditorLite'
}

More demos

Authors

License

MIT

react-markdown-editor-lite's People

Contributors

aitayn avatar atsushifujikawa avatar blunderchips avatar dalperin avatar daveteu avatar dcalvom avatar demozlx avatar dependabot[bot] avatar gabsii avatar harrychen0506 avatar heller avatar iwikal avatar jonatanklosko avatar kiancross avatar mihilmy avatar rdvojmoc avatar sylingd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-markdown-editor-lite's Issues

textarea ignores 'y' and 'z'

With the upgrade from minor version 0.5.1. to 0.6.0, I've noticed that I can no longer type the characters 'y' and 'z' into the text area. Downgrading to 0.5.1 returns that functionality.

only editor

有没有可能 只打开编辑器 不展示preview 也不让用户点击 ?想自定义 preview 发现就算设置了

view: { menu: true, md: true, html: false }

依然没办法保证只展示 eidtor

Error: Cannot find module 'markdown-it'

My code is almost same like the example.

import React from 'react';
import MarkdownIt from 'markdown-it'
import MdEditor from 'react-markdown-editor-lite'
// import style manually
import 'react-markdown-editor-lite/lib/index.css';

const CreatePost = () => {
    const mdParser = new MarkdownIt(/* Markdown-it options */);

    const handleEditorChange = ({ html, text }) => {
        console.log('handleEditorChange', html, text)
    }
    return (
        <div>
            <MdEditor
                value=""
                renderHTML={(text) => mdParser.render(text)}
                onChange={handleEditorChange}
            />
        </div>
    );
}

export default CreatePost;

onImageUpload执行后的回调函数能单独提供吗?

onImageUpload={(file, callback) => void}

依照现在的做法,向callback传入url可以插入图片。

如果callback这个参数所使用的函数能单独提供出来的话,就能满足图片添加功能的二次开发,例如
做一个弹窗,能选择输入url链接或者上传图片。

同类比,工具栏上其他的功能是否也能单独提供这样的callback?
或者统一成一个方法,实现不同内容的插入?

Disable/hide full screen option

Is there a way to disable/hide full screen option from top menu? I went through the config options but couldn't find one.
image

版本计划

  • 重构工具栏部分,支持插拔,支持插件
  • 重构预览部分,并将预览按钮移动至工具栏
  • 改进渲染功能,支持诸如react-markdown这类将markdown渲染为ReactNode的渲染引擎
  • 功能:支持直接粘贴图片、图片拖拽
  • 功能:支持更多API,供插件使用

name is now a required property

I just upgraded this library to the latest version and now I got one error which is that name is now a required property of the MdEditor component.

I would like to know why the changes? Why is it required? And most importantly how does it affect my app?

Thank you, and thank you for this library.

_getDecoratedText报错

线上环境:user: zlx,pass: 123,当编辑器内无内容的时候,点击toolbar插入任何内容,都会报这个错:
image,我自己试了下,应该是mdEditor.js中value默认值的问题

  componentWillReceiveProps(nextProps) {
    if (nextProps.value === this.props.value) {
      return
    }   
    // let {value} = nextProps   //  value为undefined时,报错
    let {value = ''} = nextProps  // value给默认值,即可解决
    const {text} = this.state
    value = value && value.replace(//g, '\n')    
    this.setState({
      text: value,
      html: this.renderHTML(value)
    })
  }

如果可以,烦请修复一下。

Typing errors - TS2314 and TS2694

After adding react-markdown-editor-lite through npm install I get two typing errors in /lib/index.d.ts
TS2694 (TS) Namespace 'React' has no exported member 'HTMLAttributes'. and TS2314 (TS) Generic type 'Promise<T>' requires 1 type argument(s)..

React versions:

  • react: 16.9.0
  • @types/react: 16.9.11

How to fix this errors?

Remove display both editor and preview

I'm trying to hide the both preview and editor side-by-side view mode so I can have either full-view preview or full-view editor.
I don't see this option in the canView

新增一个getNavigation获取h[1-6]标题,生成标题目录

可不可以新增一个getNavigation获取h[1-6]标题,生成标题目录,点击目录滚动到指定标题下,
image类似这种,这个是我看到别人的vue,但是我比较喜欢你的简洁,但是我是个后端人员,改不来您的代码,😂,谢谢

Navigator not available when using SSR

My application renders react components in both server and client (SSR), and I'm transpiling my server as well as client code with Webpack.
I run into some problems compiling this package at the server because navigator is not available in NodeJS environment.

Is it possible to add an extra check on existence of the navigator variable, in order to avoid this compilation problem?
Thanks.

Support ref on Editor

I'm using unform for form management and trying to create a custom input for MdEditor. This seems to require a ref to the underlying input/textarea but I don't see a way to pass this through to to the editor (example using simple code editor w/ unform shows what I'm trying to do: unform react simple code editor example .

Image and table plugin configuration doesn't work

The configuration docs mentions the following editor configuration attributes: config.imageAccept, config.table.maxRow, config.table.maxCol, but currently none is respected (e.g. the official demo renders table plugin 6x6, even though the default configuration says 4x6).

The problem is that these plugins call getConfig:

accept={this.getConfig('imageAccept', '')}

maxRow={this.getConfig('maxRow', 6)}
maxCol={this.getConfig('maxCol', 6)}

But the getConfig function looks up the options in the plugin configuration, not the editor configuration:

protected getConfig(key: string, defaultValue?: any) {
return this.props.config && typeof this.props.config[key] !== 'undefined' && this.props.config[key] !== null
? this.props.config[key]
: defaultValue;
}

Either the plugins should use the global configuration, or the global configuration options should be removed in favor of plugin specific configuration (in that case it would be great to have an example of how to configure the built-in plugins).

Configuring Theme

I'm using Material-UI on my project where I have configured a dark theme using Material UIs ThemeProvider.

When my theme is on darkmode this editor doesn't change to dark, it just remains white.

Is this editor configurable with themes or even override the css themes.

Thanks.

Image alt text issue

You are adding alt text to img tag which is selected on editor. It should be the name of image which is uploading. Can I get it fixed as soon as possible?

Can't get the markdown

I'm trying to get the Markdown from the handleEditorChange as described in your first example but getting just undefined (whereas html is fine)...

handleEditorChange ({html, md}) {    
    console.log('handleEditorChange', html, md)
}

Any idea?

thanks in advance

Is there an example to use focus() to set focus on editor?

When I add a ref to the component, I see there is nodeMdText.current, but it always seems to be null.

I am adding ref like this...
ref={node => (this.mdEditor = node || undefined)}

...and then trying...
this.mdEditor.nodeMdText.current.focus();

How to use getHtmlValue in react hooks

const handleGetHtmlValue = () => {    
    alert(MdEditor.getHtmlValue())
  }

<MdEditor **_ref={node => this.mdEditor = node}_** value={MOCK_DATA} style={{height: '400px'}} renderHTML={this.renderHTML} config={{ view: { menu: true, md: true, html: true, fullScreen: true }, imageUrl: 'https://octodex.github.com/images/minion.png' }} onChange={this.handleEditorChange} onImageUpload={this.handleImageUpload} // onCustomImageUpload={this.onCustomImageUpload} // if using onCustomImageUpload, onImageUpload will be not working onBeforeClear={this.onBeforeClear} />
showing error

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.