node 79623ecd20 feat: update markdown editor | 6 years ago | |
---|---|---|
.. | ||
.storybook | 6 years ago | |
assets | 6 years ago | |
demo | 6 years ago | |
docs | 6 years ago | |
docs-md | 6 years ago | |
src | 6 years ago | |
test | 6 years ago | |
.dockerignore | 6 years ago | |
.gitignore | 6 years ago | |
.npmignore | 6 years ago | |
Dockerfile | 6 years ago | |
LICENSE | 6 years ago | |
gulpfile.js | 6 years ago | |
package-lock.json | 6 years ago | |
package.json | 6 years ago | |
readme.md | 6 years ago | |
tsconfig.build.json | 6 years ago | |
tsconfig.demo.prod.json | 6 years ago | |
tsconfig.json | 6 years ago | |
tslint.json | 6 years ago | |
webpack.config.demo.dev.js | 6 years ago | |
webpack.config.demo.prod.js | 6 years ago |
A simple yet powerful and extensible Markdown Editor editor for React. React-mde is built on top of Draft.js.
npm i --save react-mde
React-mde currently depends on:
Draft.js. This facilitates features that would otherwise be quite difficult. The best examples being history management, mentions and pasting files.
npm i --save draft-js
Font Awesome 5.* for the icons. This is not a hard dependency and can be changed (see the Customizing Icons section below).
To use Font Awesome icons, install using your preferred method.
The easiest is just add this to <head/>
:
It is possible to use React-mde with Font Awesome 4 (and possibly earlier versions) – see below under ‘Customizing Icons’.
Showdown. React-mde is not opinionated as to how to transform markdown into HTML and this can be done both in client-side, like StackOverflow, or in server-side, like GitHub. The easiest way is to use Showdown and process it in client-side. If you decide to do so, install Showdown:
npm i --save showdown
React-mde is a completely controlled component.
Minimal example using Showdown:
import * as React from "react";
import ReactMde, {ReactMdeTypes} from "../src";
import * as Showdown from "showdown";
export interface AppState {
mdeState: ReactMdeTypes.MdeState;
}
export class App extends React.Component<{}, AppState> {
converter: Showdown.Converter;
constructor(props) {
super(props);
this.state = {
mdeState: null,
};
this.converter = new Showdown.Converter({tables: true, simplifiedAutoLink: true});
}
handleValueChange = (mdeState: ReactMdeTypes.MdeState) => {
this.setState({mdeState});
}
render() {
return (
<div className="container">
<ReactMde
onChange={this.handleValueChange}
editorState={this.state.mdeState}
generateMarkdownPreview={(markdown) => Promise.resolve(this.converter.makeHtml(markdown))}
/>
</div>
);
}
}
By default, React-mde will use Font Awesome class names to render icons (see above for how to install). The default icon provider returns icons that look like the following:
<i className={`fas fa-${icon}`} aria-hidden="true"/>
This can be changed by passing a buttonContentOptions
prop to the ReactMde
component with an iconProvider
option to tell React-mde how to render icons.
For example, you can use your own custom icon component by changing the iconProvider
:
<ReactMde
buttonContentOptions={{
iconProvider: name => <MyCustomIcon name={name} />,
}}
onChange={this.handleValueChange}
// ...
/>
In order to use Font Awesome 4 classes, you can pass the following prop:
buttonContentOptions={{
iconProvider: name => <i className={`fa fa-${name}`} />,
}}
This will cause React-mde to use FA4-style classnames.
The types are described below
{markdown:'Hello!
}`, on the subsequent calls,
editorState should be the object returned by the onChange callback.iconProvider
to allow custom icon rendering.onChange
event.prop
is falsy, then no preview is going to be generated.vertical
, horizontal
, tabbed
and noPreview
.layout-component
. Each layout may or may not expect
options. It is recommended to inspect the layouts source code to see what options can be passed to each
while the documentation is not complete.
-- readOnly?: boolean: Flag to render the editor in read-only mode.The following styles from React-mde should be added: (Both .scss and .css files are available. No need to use sass-loader if you don’t want)
Easiest way: import react-mde-all.css
:
import 'react-mde/lib/styles/css/react-mde-all.css';
If you want to have a more granular control over the styles, you can import each individual file.
If you’re using SASS, you can override these variables: https://github.com/andrerpena/react-mde/blob/master/src/styles/variables.scss
You also need Font Awesome for the toolbar icons. Font Awesome 5 can be installed in different ways,
but the easiest is just adding this to the <head/>
:
<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
React-mde does not automatically sanitize the HTML preview. If your using Showdown, this has been taken from their documentation):
Cross-side scripting is a well known technique to gain access to private information of the users of a website. The attacker injects spurious HTML content (a script) on the web page which will read the user’s cookies and do something bad with it (like steal credentials). As a countermeasure, you should filter any suspicious content coming from user input. Showdown doesn’t include an XSS filter, so you must provide your own. But be careful in how you do it…
You might want to take a look at showdown-xss-filter.
React-mde allows you to use the build-in commands, implement your own commands, or both. If you wish to implement your own commands, please refer to the commands source code to understand how they should be implemented.
React-mde is designed to be composable and to facilitate new layouts:
DraftUtil.buildNewMdeState
can be used to programmatically create a new state based on the current state.
Example with a button that duplicates the text. For the initial state, you can just pass null to editorState
and React-Mde will build a new MdeState
and call onChange
with the first MdeState
Check the project here: https://github.com/andrerpena/react-mde/projects/1
React-mde is MIT licensed.
Made with :heart: by André Pena and other awesome contributors. Check out my website: http://andrerpena.me.