@@ -29,7 +29,12 @@ import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
2929import createLinkDecorator from "./decorators/link" ;
3030import createImageDecorator from "./decorators/image" ;
3131import { replaceText } from "./utils" ;
32- import { CODE_BLOCK_REGEX , CODE_BLOCK_TYPE } from "./constants" ;
32+ import {
33+ CODE_BLOCK_REGEX ,
34+ CODE_BLOCK_TYPE ,
35+ defaultInlineWhitelist ,
36+ defaultBlockWhitelist ,
37+ } from "./constants" ;
3338
3439const defaultLanguages = {
3540 bash : "Bash" ,
@@ -85,21 +90,35 @@ function inCodeBlock(editorState) {
8590 return false ;
8691}
8792
88- function checkCharacterForState ( editorState , character ) {
89- let newEditorState = handleBlockType ( editorState , character ) ;
90- if ( editorState === newEditorState ) {
93+ function checkCharacterForState ( config , editorState , character ) {
94+ let newEditorState = handleBlockType (
95+ config . features . block ,
96+ editorState ,
97+ character
98+ ) ;
99+ if (
100+ editorState === newEditorState &&
101+ config . features . inline . includes ( "IMAGE" )
102+ ) {
91103 newEditorState = handleImage ( editorState , character ) ;
92104 }
93- if ( editorState === newEditorState ) {
105+ if (
106+ editorState === newEditorState &&
107+ config . features . inline . includes ( "LINK" )
108+ ) {
94109 newEditorState = handleLink ( editorState , character ) ;
95110 }
96111 if ( editorState === newEditorState ) {
97- newEditorState = handleInlineStyle ( editorState , character ) ;
112+ newEditorState = handleInlineStyle (
113+ config . features . inline ,
114+ editorState ,
115+ character
116+ ) ;
98117 }
99118 return newEditorState ;
100119}
101120
102- function checkReturnForState ( editorState , ev ) {
121+ function checkReturnForState ( config , editorState , ev ) {
103122 let newEditorState = editorState ;
104123 const contentState = editorState . getCurrentContent ( ) ;
105124 const selection = editorState . getSelection ( ) ;
@@ -123,7 +142,7 @@ function checkReturnForState(editorState, ev) {
123142 ) {
124143 // transform markdown (if we aren't in a codeblock that is)
125144 if ( ! inCodeBlock ( editorState ) ) {
126- newEditorState = checkCharacterForState ( newEditorState , "\n" ) ;
145+ newEditorState = checkCharacterForState ( config , newEditorState , "\n" ) ;
127146 }
128147 if ( newEditorState === editorState ) {
129148 newEditorState = insertEmptyBlock ( newEditorState ) ;
@@ -134,6 +153,7 @@ function checkReturnForState(editorState, ev) {
134153 if (
135154 newEditorState === editorState &&
136155 type !== "code-block" &&
156+ config . features . block . includes ( "CODE" ) &&
137157 CODE_BLOCK_REGEX . test ( text )
138158 ) {
139159 newEditorState = handleNewCodeBlock ( editorState ) ;
@@ -157,6 +177,10 @@ function checkReturnForState(editorState, ev) {
157177const defaultConfig = {
158178 renderLanguageSelect : defaultRenderSelect ,
159179 languages : defaultLanguages ,
180+ features : {
181+ inline : defaultInlineWhitelist ,
182+ block : defaultBlockWhitelist ,
183+ } ,
160184} ;
161185
162186const createMarkdownPlugin = ( _config = { } ) => {
@@ -165,6 +189,10 @@ const createMarkdownPlugin = (_config = {}) => {
165189 const config = {
166190 ...defaultConfig ,
167191 ..._config ,
192+ features : {
193+ ...defaultConfig . features ,
194+ ..._config . features ,
195+ } ,
168196 } ;
169197
170198 return {
@@ -237,7 +265,7 @@ const createMarkdownPlugin = (_config = {}) => {
237265 handleReturn ( ev , editorState , { setEditorState } ) {
238266 if ( inLink ( editorState ) ) return "not-handled" ;
239267
240- let newEditorState = checkReturnForState ( editorState , ev ) ;
268+ let newEditorState = checkReturnForState ( config , editorState , ev ) ;
241269 const selection = newEditorState . getSelection ( ) ;
242270
243271 // exit code blocks
@@ -249,7 +277,7 @@ const createMarkdownPlugin = (_config = {}) => {
249277 return "handled" ;
250278 }
251279
252- newEditorState = checkCharacterForState ( newEditorState , "\n" ) ;
280+ newEditorState = checkCharacterForState ( config , newEditorState , "\n" ) ;
253281 let content = newEditorState . getCurrentContent ( ) ;
254282
255283 // if there are actually no changes but the editorState has a
@@ -283,7 +311,11 @@ const createMarkdownPlugin = (_config = {}) => {
283311 // If we're in a link - don't transform markdown
284312 if ( inLink ( editorState ) ) return "not-handled" ;
285313
286- const newEditorState = checkCharacterForState ( editorState , character ) ;
314+ const newEditorState = checkCharacterForState (
315+ config ,
316+ editorState ,
317+ character
318+ ) ;
287319 if ( editorState !== newEditorState ) {
288320 setEditorState ( newEditorState ) ;
289321 return "handled" ;
@@ -334,11 +366,19 @@ const createMarkdownPlugin = (_config = {}) => {
334366 newEditorState ,
335367 buffer . join ( "" ) + text [ i ]
336368 ) ;
337- newEditorState = checkCharacterForState ( newEditorState , text [ i ] ) ;
369+ newEditorState = checkCharacterForState (
370+ config ,
371+ newEditorState ,
372+ text [ i ]
373+ ) ;
338374 buffer = [ ] ;
339375 } else if ( text [ i ] . charCodeAt ( 0 ) === 10 ) {
340376 newEditorState = replaceText ( newEditorState , buffer . join ( "" ) ) ;
341- const tmpEditorState = checkReturnForState ( newEditorState , { } ) ;
377+ const tmpEditorState = checkReturnForState (
378+ config ,
379+ newEditorState ,
380+ { }
381+ ) ;
342382 if ( newEditorState === tmpEditorState ) {
343383 newEditorState = insertEmptyBlock ( tmpEditorState ) ;
344384 } else {
0 commit comments