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

Commit 0b99407

Browse files
committed
Disable all inline styling in code blocks
Close #13
1 parent 21b192d commit 0b99407

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/index.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ import { CODE_BLOCK_REGEX } from "./constants";
2525

2626
const 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+
2841
function 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

Comments
 (0)