Skip to content

Commit 4b407f1

Browse files
authored
Merge pull request accordproject#96 from accordproject/ds-use-markdown-transform
(chore) update to use markdown-transform
2 parents 1b4ee23 + 3e1d916 commit 4b407f1

File tree

20 files changed

+6606
-6234
lines changed

20 files changed

+6606
-6234
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
This repo contains two React-based editors:
88
1. A WYSIWYG [Slate][slate]-based editor that edits rich text and calls an `onChange`
9-
callback with the modified Slate DOM and the [CommonMark][CommonMark] markdown serialization.
9+
callback with the modified Slate DOM.
1010
2. A TextArea-based markdown editor that edits markdown text and calls an `onChange`
11-
callback with the equivalent Slate DOM and the modified markdown text.
11+
callback with the modified markdown text.
12+
13+
The demo editor uses the `markdown-transform` package to transform Slate DOM
14+
to/from markdown text.
1215

1316
Using these editors you could allow people to either edit rich formatted text using
1417
markdown (and provide a WYSIWYG preview), or allow them to edit using a WYSIWYG
1518
editor and use markdown for persistence.
1619

17-
The editor is plugin-based and includes a formatting toolbar. Plugins are used to
18-
extend the core editor with support with new formatting functionality and/or new
19-
types of blocks.
20+
The editor includes a formatting toolbar.
2021

2122
This component is Apache-2 licensed Open Source. Contributors welcome!
2223

@@ -32,11 +33,13 @@ npm install @accordproject/markdown-editor
3233
import { SlateAsInputEditor } from '@accordproject/markdown-editor';
3334
import List from '@accordproject/markdown-editor/dist/plugins/list';
3435
import Video from '@accordproject/markdown-editor/dist/plugins/video';
36+
import { SlateTransformer } from '@accordproject/markdown-slate';
3537
36-
const plugins = [List(), Video()];
38+
const plugins = [List()];
39+
const slateTransformer = new SlateTransformer();
3740
38-
function storeLocal(slateValue, markdown) {
39-
// console.log(markdown);
41+
function storeLocal(slateValue) {
42+
const markdown = slateTransformer.toMarkdown(slateValue);
4043
localStorage.setItem('markdown-editor', markdown);
4144
}
4245
@@ -50,9 +53,6 @@ For an example React App see the `./examples/` folder.
5053

5154
A `TextArea` containing [CommonMark][CommonMark] synchronized with a `MarkdownEditor` component, rendered using [Slate][slate].
5255

53-
The code for the sample `video` plugin used in the demo is here:
54-
https://github.com/accordproject/markdown-editor/blob/master/src/plugins/video.js
55-
5656
![overview image](overview.png)
5757

5858
In order to run an isolated local development example, run `npm run dev` and then navigate to: http://localhost:3001/examples

examples/src/index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ import {
44
} from 'semantic-ui-react';
55

66
import ReactDOM from 'react-dom';
7+
import { SlateTransformer } from '@accordproject/markdown-slate';
8+
79
import './index.css';
810
import MarkdownAsInputEditor from '../../src/MarkdownAsInputEditor';
911
import SlateAsInputEditor from '../../src/SlateAsInputEditor';
10-
import PluginManager from '../../src/PluginManager';
11-
import FromMarkdown from '../../src/markdown/fromMarkdown';
1212

1313
import * as serviceWorker from './serviceWorker';
1414
import 'semantic-ui-css/semantic.min.css';
15-
import List from '../../src/plugins/list';
16-
import Video from '../../src/plugins/video';
1715
import NoEdit from '../../src/plugins/noedit';
16+
import List from '../../src/plugins/list';
17+
18+
const plugins = [NoEdit(), List()];
19+
const slateTransformer = new SlateTransformer();
1820

19-
const plugins = [List(), Video(), NoEdit()];
20-
const pluginManager = new PluginManager(plugins);
21-
const fromMarkdown = new FromMarkdown(pluginManager);
21+
const defaultMarkdown = `# My Heading
2222
23-
const defaultMarkdown = `# Heading One
2423
This is text. This is *italic* text. This is **bold** text. This is a [link](https://clause.io). This is \`inline code\`.
2524
2625
This is ***bold and italic*** text
@@ -43,13 +42,10 @@ Or:
4342
4443
### Sub heading
4544
46-
Video:
47-
48-
<video/>
49-
50-
Another video:
45+
This is more text.
5146
52-
<video src="https://www.youtube.com/embed/cmmq-JBMbbQ"/>`;
47+
Fin.
48+
`;
5349

5450
const propsObj = {
5551
WIDTH: '600px',
@@ -63,23 +59,22 @@ function Demo() {
6359
/**
6460
* Current Slate Value
6561
*/
66-
const [slateValue, setSlateValue] = useState(fromMarkdown.convert(defaultMarkdown));
62+
const [slateValue, setSlateValue] = useState(slateTransformer.fromMarkdown(defaultMarkdown));
6763
const [markdown, setMarkdown] = useState(defaultMarkdown);
6864

6965
/**
7066
* Called when the markdown changes
7167
*/
72-
const onMarkdownChange = useCallback((slateValue, markdown) => {
68+
const onMarkdownChange = useCallback((markdown) => {
7369
localStorage.setItem('markdown-editor', markdown);
74-
// setSlateValue(slateValue);
75-
// setMarkdown(markdown);
7670
}, []);
7771

7872
/**
7973
* Called when the Slate Value changes
8074
*/
81-
const onSlateValueChange = useCallback((slateValue, markdown) => {
75+
const onSlateValueChange = useCallback((slateValue) => {
8276
localStorage.setItem('slate-editor-value', slateValue.toJSON());
77+
const markdown = slateTransformer.toMarkdown(slateValue);
8378
setSlateValue(slateValue);
8479
setMarkdown(markdown);
8580
}, []);

0 commit comments

Comments
 (0)