fix(core): force multiline flag for editor component patterns (#3082)
This commit is contained in:
parent
09564bf8b6
commit
476f45096e
@ -25,7 +25,8 @@ export default function createEditorComponent(config) {
|
|||||||
type,
|
type,
|
||||||
icon,
|
icon,
|
||||||
widget,
|
widget,
|
||||||
pattern,
|
// enforce multiline flag, exclude others
|
||||||
|
pattern: new RegExp(pattern, 'm'),
|
||||||
fromBlock: bind(fromBlock) || (() => ({})),
|
fromBlock: bind(fromBlock) || (() => ({})),
|
||||||
toBlock: bind(toBlock) || (() => 'Plugin'),
|
toBlock: bind(toBlock) || (() => 'Plugin'),
|
||||||
toPreview: bind(toPreview) || (!widget && (bind(toBlock) || (() => 'Plugin'))),
|
toPreview: bind(toPreview) || (!widget && (bind(toBlock) || (() => 'Plugin'))),
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { stripIndent } from 'common-tags';
|
|
||||||
import { remarkParseShortcodes } from '../remarkShortcodes';
|
import { remarkParseShortcodes } from '../remarkShortcodes';
|
||||||
|
|
||||||
// Stub of Remark Parser
|
// Stub of Remark Parser
|
||||||
@ -11,43 +10,44 @@ function process(value, plugins, processEat = () => {}) {
|
|||||||
Parser.prototype.blockTokenizers.shortcode(eat, value);
|
Parser.prototype.blockTokenizers.shortcode(eat, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function EditorComponent({ id = 'foo', fromBlock = jest.fn(), pattern }) {
|
||||||
|
// initialize pattern as RegExp as done in the EditorComponent value object
|
||||||
|
return { id, fromBlock, pattern: new RegExp(pattern, 'm') };
|
||||||
|
}
|
||||||
|
|
||||||
describe('remarkParseShortcodes', () => {
|
describe('remarkParseShortcodes', () => {
|
||||||
let editorComponent;
|
describe('pattern matching', () => {
|
||||||
|
it('should work', () => {
|
||||||
beforeEach(() => {
|
const editorComponent = EditorComponent({ pattern: /bar/ });
|
||||||
editorComponent = {
|
process('foo bar', [editorComponent]);
|
||||||
id: 'foo',
|
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['bar']));
|
||||||
pattern: /bar/,
|
});
|
||||||
fromBlock: jest.fn(),
|
it('should match value surrounded in newlines', () => {
|
||||||
};
|
const editorComponent = EditorComponent({ pattern: /^bar$/ });
|
||||||
|
process('foo\n\nbar\n', [editorComponent]);
|
||||||
|
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['bar']));
|
||||||
|
});
|
||||||
|
it('should match multiline shortcodes', () => {
|
||||||
|
const editorComponent = EditorComponent({ pattern: /^foo\nbar$/ });
|
||||||
|
process('foo\nbar', [editorComponent]);
|
||||||
|
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['foo\nbar']));
|
||||||
|
});
|
||||||
|
it('should match multiline shortcodes with empty lines', () => {
|
||||||
|
const editorComponent = EditorComponent({ pattern: /^foo\n\nbar$/ });
|
||||||
|
process('foo\n\nbar', [editorComponent]);
|
||||||
|
expect(editorComponent.fromBlock).toHaveBeenCalledWith(
|
||||||
|
expect.arrayContaining(['foo\n\nbar']),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should parse shortcodes', () => {
|
describe('output', () => {
|
||||||
process('foo bar', [editorComponent]);
|
it('should be a remark shortcode node', () => {
|
||||||
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['bar']));
|
const processEat = jest.fn();
|
||||||
});
|
const shortcodeData = { bar: 'baz' };
|
||||||
it('should parse multiline shortcodes', () => {
|
const expectedNode = { type: 'shortcode', data: { shortcode: 'foo', shortcodeData } };
|
||||||
const value = stripIndent`
|
const editorComponent = EditorComponent({ pattern: /bar/, fromBlock: () => shortcodeData });
|
||||||
foo
|
process('foo bar', [editorComponent], processEat);
|
||||||
bar
|
expect(processEat).toHaveBeenCalledWith(expectedNode);
|
||||||
`;
|
});
|
||||||
process(value, [{ ...editorComponent, pattern: /^foo\nbar$/ }]);
|
|
||||||
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['foo\nbar']));
|
|
||||||
});
|
|
||||||
it('should parse multiline shortcodes with empty lines', () => {
|
|
||||||
const value = stripIndent`
|
|
||||||
foo
|
|
||||||
|
|
||||||
bar
|
|
||||||
`;
|
|
||||||
process(value, [{ ...editorComponent, pattern: /^foo\n\nbar$/ }]);
|
|
||||||
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['foo\n\nbar']));
|
|
||||||
});
|
|
||||||
it('should produce shortcode node', () => {
|
|
||||||
const processEat = jest.fn();
|
|
||||||
const shortcodeData = { bar: 'baz' };
|
|
||||||
const expectedNode = { type: 'shortcode', data: { shortcode: 'foo', shortcodeData } };
|
|
||||||
editorComponent.fromBlock = () => shortcodeData;
|
|
||||||
process('foo bar', [editorComponent], processEat);
|
|
||||||
expect(processEat).toHaveBeenCalledWith(expectedNode);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user