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

Commit 2df3609

Browse files
author
Axel Wahlen
committed
Allow custom inline style matcher
1 parent d1d6fdf commit 2df3609

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/__test__/plugin.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ describe("draft-js-markdown-plugin", () => {
542542
expect(modifierSpy).toHaveBeenCalledWith(
543543
defaultInlineWhitelist,
544544
currentEditorState,
545-
" "
545+
" ",
546+
undefined
546547
);
547548
});
548549
it("unstickys inline style", () => {

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function checkCharacterForState(config, editorState, character) {
131131
newEditorState = handleInlineStyle(
132132
config.features.inline,
133133
editorState,
134-
character
134+
character,
135+
config.customInlineMatchers
135136
);
136137
}
137138
return newEditorState;

src/modifiers/handleInlineStyle.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { inlineMatchers } from "../constants";
44
import insertText from "./insertText";
55
import { getCurrentLine as getLine } from "../utils";
66

7-
const handleChange = (editorState, line, whitelist) => {
7+
const handleChange = (editorState, line, whitelist, customInlineMatchers) => {
88
let newEditorState = editorState;
9-
Object.keys(inlineMatchers)
9+
const matchers = Object.assign({}, inlineMatchers, customInlineMatchers);
10+
11+
Object.keys(matchers)
1012
.filter(matcher => whitelist.includes(matcher))
1113
.some(k => {
12-
inlineMatchers[k].some(re => {
14+
matchers[k].some(re => {
1315
let matchArr;
1416
do {
1517
matchArr = re.exec(line);
@@ -31,19 +33,30 @@ const handleChange = (editorState, line, whitelist) => {
3133
const handleInlineStyle = (
3234
whitelist,
3335
editorStateWithoutCharacter,
34-
character
36+
character,
37+
customInlineMatchers = {}
3538
) => {
3639
const editorState = insertText(editorStateWithoutCharacter, character);
3740
let selection = editorState.getSelection();
3841
let line = getLine(editorState);
39-
let newEditorState = handleChange(editorState, line, whitelist);
42+
let newEditorState = handleChange(
43+
editorState,
44+
line,
45+
whitelist,
46+
customInlineMatchers
47+
);
4048
let lastEditorState = editorState;
4149

4250
// Recursively resolve markdown, e.g. _*text*_ should turn into both italic and bold
4351
while (newEditorState !== lastEditorState) {
4452
lastEditorState = newEditorState;
4553
line = getLine(newEditorState);
46-
newEditorState = handleChange(newEditorState, line, whitelist);
54+
newEditorState = handleChange(
55+
newEditorState,
56+
line,
57+
whitelist,
58+
customInlineMatchers
59+
);
4760
}
4861

4962
if (newEditorState !== editorState) {

0 commit comments

Comments
 (0)