Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 16b4d44

Browse files
authored
Merge pull request #73 from withspectrum/new-bold-and-italic
Trigger markdown on any character and change markdown implementation
2 parents b9e11d7 + 190ec81 commit 16b4d44

File tree

8 files changed

+133
-288
lines changed

8 files changed

+133
-288
lines changed

README.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,64 @@ A [DraftJS] plugin for supporting Markdown syntax shortcuts in DraftJS. This plu
1717
npm i --save draft-js-markdown-plugin
1818
```
1919

20+
## Usage
21+
22+
```js
23+
import React, { Component } from 'react';
24+
import Editor from 'draft-js-plugins-editor';
25+
import createMarkdownPlugin from 'draft-js-markdown-plugin';
26+
import { EditorState } from 'draft-js';
27+
28+
export default class DemoEditor extends Component {
29+
30+
state = {
31+
editorState: EditorState.createEmpty(),
32+
plugins: [createMarkdownPlugin()]
33+
};
34+
35+
onChange = (editorState) => {
36+
this.setState({
37+
editorState,
38+
});
39+
};
40+
41+
render() {
42+
return (
43+
<Editor
44+
editorState={this.state.editorState}
45+
onChange={this.onChange}
46+
plugins={this.state.plugins}
47+
/>
48+
);
49+
}
50+
}
51+
```
52+
53+
### Add code block syntax highlighting
54+
55+
Using the [`draft-js-prism-plugin`](https://github.com/withspectrum/draft-js-prism-plugin) you can easily add syntax highlighting support to your code blocks!
56+
57+
```JS
58+
// Install prismjs and draft-js-prism-plugin
59+
import Prism from 'prismjs';
60+
import createPrismPlugin from 'draft-js-prism-plugin';
61+
62+
class Editor extends Component {
63+
state = {
64+
plugins: [
65+
// Add the Prism plugin to the plugins array
66+
createPrismPlugin({
67+
prism: Prism
68+
}),
69+
createMarkdownPlugin()
70+
]
71+
}
72+
}
73+
```
74+
2075
## Options
21-
The `draft-js-markdown-plugin` is configurable. Just pass a config object. Here are the available options:
2276

77+
The `draft-js-markdown-plugin` is configurable. Just pass a config object. Here are the available options:
2378

2479
### `renderLanguageSelect`
2580

@@ -152,61 +207,6 @@ const markdownPlugin = createMarkdownPlugin({ entityType });
152207
const editorPlugins = [focusPlugin, imagePlugin, markdownPlugin];
153208
```
154209

155-
## Usage
156-
157-
```js
158-
import React, { Component } from 'react';
159-
import Editor from 'draft-js-plugins-editor';
160-
import createMarkdownPlugin from 'draft-js-markdown-plugin';
161-
import { EditorState } from 'draft-js';
162-
163-
export default class DemoEditor extends Component {
164-
165-
state = {
166-
editorState: EditorState.createEmpty(),
167-
plugins: [createMarkdownPlugin()]
168-
};
169-
170-
onChange = (editorState) => {
171-
this.setState({
172-
editorState,
173-
});
174-
};
175-
176-
render() {
177-
return (
178-
<Editor
179-
editorState={this.state.editorState}
180-
onChange={this.onChange}
181-
plugins={this.state.plugins}
182-
/>
183-
);
184-
}
185-
}
186-
```
187-
188-
### Add code block syntax highlighting
189-
190-
Using the [`draft-js-prism-plugin`](https://github.com/withspectrum/draft-js-prism-plugin) you can easily add syntax highlighting support to your code blocks!
191-
192-
```JS
193-
// Install prismjs and draft-js-prism-plugin
194-
import Prism from 'prismjs';
195-
import createPrismPlugin from 'draft-js-prism-plugin';
196-
197-
class Editor extends Component {
198-
state = {
199-
plugins: [
200-
// Add the Prism plugin to the plugins array
201-
createPrismPlugin({
202-
prism: Prism
203-
}),
204-
createMarkdownPlugin()
205-
]
206-
}
207-
}
208-
```
209-
210210
## Why fork the `markdown-shortcuts-plugin`?
211211

212212
Writing is a core part of our app, and while the `markdown-shortcuts-plugin` is awesome and battle-tested there are a few opinionated things we wanted to do differently. Rather than bother [@ngs](https://github.com/ngs) with tons of PRs, we figured it'd be better to own that core part of our experience fully.

src/__test__/plugin.test.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -228,37 +228,6 @@ describe("draft-js-markdown-plugin", () => {
228228
expect(store.setEditorState).toHaveBeenCalledWith(newEditorState);
229229
});
230230

231-
it("adds list item and transforms markdown", () => {
232-
// createMarkdownPlugin.__Rewire__("leaveList", modifierSpy); // eslint-disable-line no-underscore-dangle
233-
currentRawContentState = {
234-
entityMap: {},
235-
blocks: [
236-
{
237-
key: "item1",
238-
text: "**some bold text**",
239-
type: "unordered-list-item",
240-
depth: 0,
241-
inlineStyleRanges: [],
242-
entityRanges: [],
243-
data: {},
244-
},
245-
],
246-
};
247-
currentSelectionState = currentSelectionState.merge({
248-
focusOffset: 18,
249-
anchorOffset: 18,
250-
});
251-
expect(subject()).toBe("handled");
252-
// expect(modifierSpy).toHaveBeenCalledTimes(1);
253-
expect(store.setEditorState).toHaveBeenCalledTimes(1);
254-
newEditorState = store.setEditorState.mock.calls[0][0];
255-
const newRawContentState = Draft.convertToRaw(
256-
newEditorState.getCurrentContent()
257-
);
258-
expect(newRawContentState.blocks.length).toBe(2);
259-
expect(newEditorState.getCurrentInlineStyle().size).toBe(0);
260-
});
261-
262231
const emptyBlockTypes = [
263232
"blockquote",
264233
"header-one",

src/constants.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { CHECKABLE_LIST_ITEM } from "draft-js-checkable-list-item";
33
export const CODE_BLOCK_REGEX = /^```([\w-]+)?\s*$/;
44

55
export const inlineMatchers = {
6-
BOLD: [/\*\*(.+)\*\*$/g, /__(.+)__$/g],
7-
ITALIC: [/\*(.+)\*$/g, /_(.+)_$/g],
8-
CODE: [/`(.+)`$/g],
9-
STRIKETHROUGH: [/~~(.+)~~$/g],
6+
BOLD: [/\*(.+)\*$/g],
7+
ITALIC: [/_(.+)_$/g],
8+
CODE: [/`([^`]+)`$/g],
9+
STRIKETHROUGH: [/~(.+)~$/g],
1010
};
1111

1212
export const CODE_BLOCK_TYPE = "code-block";

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import insertText from "./modifiers/insertText";
2929
import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
3030
import createLinkDecorator from "./decorators/link";
3131
import createImageDecorator from "./decorators/image";
32-
import { replaceText } from "./utils";
32+
import { replaceText, getCurrentLine } from "./utils";
3333
import {
3434
CODE_BLOCK_REGEX,
3535
CODE_BLOCK_TYPE,
@@ -57,7 +57,7 @@ const defaultLanguages = {
5757
swift: "Swift",
5858
};
5959

60-
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
60+
const INLINE_STYLE_CHARACTERS = ["*", "_", "`", "~"];
6161

6262
const defaultRenderSelect = ({ options, onChange, selectedValue }) => (
6363
<select value={selectedValue} onChange={onChange}>
@@ -341,16 +341,16 @@ const createMarkdownPlugin = (_config = {}) => {
341341
return "not-handled";
342342
},
343343
handleBeforeInput(character, editorState, { setEditorState }) {
344-
if (character !== " ") {
345-
return "not-handled";
346-
}
347-
348344
// If we're in a code block - don't transform markdown
349345
if (inCodeBlock(editorState)) return "not-handled";
350346

351347
// If we're in a link - don't transform markdown
352348
if (inLink(editorState)) return "not-handled";
353349

350+
// Don't let users type two spaces after another
351+
if (character === " " && getCurrentLine(editorState).slice(-1) === " ")
352+
return "handled";
353+
354354
const newEditorState = checkCharacterForState(
355355
config,
356356
editorState,

0 commit comments

Comments
 (0)