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

Commit a78e804

Browse files
authored
Merge pull request #6 from withspectrum/markdown-in-code-blocks
Disable markdown in code blocks
2 parents 3a39746 + fa51c39 commit a78e804

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/__test__/plugin.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@ describe("draft-js-markdown-plugin", () => {
331331
character = " ";
332332
subject = () =>
333333
plugin.handleBeforeInput(character, store.getEditorState(), store);
334+
currentRawContentState = {
335+
entityMap: {},
336+
blocks: [
337+
{
338+
key: "item1",
339+
text: "",
340+
type: "unstyled",
341+
depth: 0,
342+
inlineStyleRanges: [],
343+
entityRanges: [],
344+
data: {},
345+
},
346+
],
347+
};
334348
});
335349
[
336350
"handleBlockType",

src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ const createMarkdownPlugin = (config = {}) => {
140140
return "not-handled";
141141
},
142142
handleReturn(ev, editorState, { setEditorState }) {
143-
console.log("HANDLE RETURN");
144143
const newEditorState = checkReturnForState(editorState, ev);
145144
if (editorState !== newEditorState) {
146145
setEditorState(newEditorState);
@@ -152,6 +151,16 @@ const createMarkdownPlugin = (config = {}) => {
152151
if (character !== " ") {
153152
return "not-handled";
154153
}
154+
// If we're in a code block don't add markdown to it
155+
const startKey = editorState.getSelection().getStartKey();
156+
if (startKey) {
157+
const currentBlockType = editorState
158+
.getCurrentContent()
159+
.getBlockForKey(startKey)
160+
.getType();
161+
if (currentBlockType === "code-block") return "not-handled";
162+
}
163+
155164
const newEditorState = checkCharacterForState(editorState, character);
156165
if (editorState !== newEditorState) {
157166
setEditorState(newEditorState);

0 commit comments

Comments
 (0)