File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,6 +38,43 @@ import { markdownToDraft } from 'markdown-draft-js';
3838var rawObject = markdownToDraft (markdownString);
3939```
4040
41+ ## Example
42+
43+ ``` javascript
44+ [-- - ]
45+
46+ import { draftToMarkdown , markdownToDraft } from ' markdown-draft-js' ;
47+ import { EditorState , convertToRaw , convertFromRaw } from ' draft-js' ;
48+
49+ [-- - ]
50+
51+ constructor (props ) {
52+ super (props);
53+
54+ // Convert input from markdown to draftjs state
55+ const markdownString = this .props .markdownString ;
56+ const rawData = markdownToDraft (markdownString);
57+ const contentState = convertFromRaw (rawData);
58+ const newEditorState = EditorState .createWithContent (contentState);
59+ this .state = {
60+ editorState: newEditorState,
61+ };
62+
63+ this .onChange = (editorState ) => {
64+ this .setState ({ editorState });
65+
66+ // Convert draftjs state to markdown
67+ const content = this .state .editorState .getCurrentContent ();
68+ const rawObject = convertToRaw (content);
69+ const markdownString = draftToMarkdown (rawObject);
70+
71+ // Do something with the markdown
72+ };
73+ }
74+
75+ [-- - ]
76+ ```
77+
4178## Custom Values
4279
4380In case you want to extend markdown’s functionality, you can. ` draftToMarkdown ` accepts an (optional) second ` options ` argument.
You can’t perform that action at this time.
0 commit comments