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

Commit 7b34b50

Browse files
committed
More lenient code block regex
This issue popped up in combination with the `draft-js-image-plugin`. When an image is inserted and then deleted, the image plugin leaves an empty line after the image. Because this empty line is in the same block it breaks the code block regex because suddenly it's "``` " (notice the space) rather than "```", so one couldn't insert a code block right before a deleted image anymore. By changing the code block regex to be ever so slightly more lenient with whitespace we fix the issue.
1 parent a882615 commit 7b34b50

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

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)