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

Commit 4c15dc1

Browse files
authored
Merge branch 'master' into fix-first-block
2 parents a4ef4ec + 016ee0c commit 4c15dc1

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ demo/public
4141
.deploy
4242
test-results.xml
4343

44+
*.swp

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CODE_BLOCK_REGEX = /^```([\w-]+)?\s*$/;

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
2121
import createLinkDecorator from "./decorators/link";
2222
import createImageDecorator from "./decorators/image";
2323
import { replaceText } from "./utils";
24+
import { CODE_BLOCK_REGEX } from "./constants";
2425

2526
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
2627

@@ -63,7 +64,7 @@ function checkReturnForState(editorState, ev) {
6364
if (
6465
newEditorState === editorState &&
6566
type !== "code-block" &&
66-
/^```([\w-]+)?$/.test(text)
67+
CODE_BLOCK_REGEX.test(text)
6768
) {
6869
newEditorState = handleNewCodeBlock(editorState);
6970
}
@@ -139,6 +140,7 @@ const createMarkdownPlugin = (config = {}) => {
139140
return "not-handled";
140141
},
141142
handleReturn(ev, editorState, { setEditorState }) {
143+
console.log("HANDLE RETURN");
142144
const newEditorState = checkReturnForState(editorState, ev);
143145
if (editorState !== newEditorState) {
144146
setEditorState(newEditorState);

src/modifiers/handleNewCodeBlock.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import changeCurrentBlockType from "./changeCurrentBlockType";
22
import insertEmptyBlock from "./insertEmptyBlock";
3+
import { CODE_BLOCK_REGEX } from "../constants";
34

45
const handleNewCodeBlock = editorState => {
56
const contentState = editorState.getCurrentContent();
67
const selection = editorState.getSelection();
78
const key = selection.getStartKey();
89
const currentBlock = contentState.getBlockForKey(key);
9-
const matchData = /^```([\w-]+)?$/.exec(currentBlock.getText());
10-
const isLast = selection.getEndOffset() === currentBlock.getLength();
10+
const matchData = CODE_BLOCK_REGEX.exec(currentBlock.getText());
11+
const currentText = currentBlock.getText();
12+
const endOffset = selection.getEndOffset();
13+
// We .trim the text here to make sure pressing enter after "``` " works even if the cursor is before the space
14+
const isLast =
15+
endOffset === currentText.length || endOffset === currentText.trim().length;
1116
if (matchData && isLast) {
1217
const data = {};
1318
const language = matchData[1];

0 commit comments

Comments
 (0)