@@ -25,6 +25,19 @@ import { CODE_BLOCK_REGEX } from "./constants";
2525
2626const INLINE_STYLE_CHARACTERS = [ " " , "*" , "_" ] ;
2727
28+ function inCodeBlock ( editorState ) {
29+ const startKey = editorState . getSelection ( ) . getStartKey ( ) ;
30+ if ( startKey ) {
31+ const currentBlockType = editorState
32+ . getCurrentContent ( )
33+ . getBlockForKey ( startKey )
34+ . getType ( ) ;
35+ if ( currentBlockType === "code-block" ) return true ;
36+ }
37+
38+ return false ;
39+ }
40+
2841function checkCharacterForState ( editorState , character ) {
2942 let newEditorState = handleBlockType ( editorState , character ) ;
3043 if ( editorState === newEditorState ) {
@@ -147,14 +160,7 @@ const createMarkdownPlugin = (config = {}) => {
147160 }
148161
149162 // If we're in a code block don't add markdown to it
150- const startKey = editorState . getSelection ( ) . getStartKey ( ) ;
151- if ( startKey ) {
152- const currentBlockType = editorState
153- . getCurrentContent ( )
154- . getBlockForKey ( startKey )
155- . getType ( ) ;
156- if ( currentBlockType === "code-block" ) return "not-handled" ;
157- }
163+ if ( inCodeBlock ( editorState ) ) return "not-handled" ;
158164
159165 newEditorState = checkCharacterForState ( editorState , "\n" ) ;
160166 if ( editorState !== newEditorState ) {
@@ -168,15 +174,9 @@ const createMarkdownPlugin = (config = {}) => {
168174 if ( character !== " " ) {
169175 return "not-handled" ;
170176 }
177+
171178 // If we're in a code block don't add markdown to it
172- const startKey = editorState . getSelection ( ) . getStartKey ( ) ;
173- if ( startKey ) {
174- const currentBlockType = editorState
175- . getCurrentContent ( )
176- . getBlockForKey ( startKey )
177- . getType ( ) ;
178- if ( currentBlockType === "code-block" ) return "not-handled" ;
179- }
179+ if ( inCodeBlock ( editorState ) ) return "not-handled" ;
180180
181181 const newEditorState = checkCharacterForState ( editorState , character ) ;
182182 if ( editorState !== newEditorState ) {
@@ -218,6 +218,8 @@ const createMarkdownPlugin = (config = {}) => {
218218 if ( html ) {
219219 return "not-handled" ;
220220 }
221+ // If we're in a code block don't add markdown to it
222+ if ( inCodeBlock ( editorState ) ) return "not-handled" ;
221223 let newEditorState = editorState ;
222224 let buffer = [ ] ;
223225 for ( let i = 0 ; i < text . length ; i ++ ) {
0 commit comments