fix(widget-markdown): allow multiline shortcodes (#3066)

This commit is contained in:
Shawn Erquhart 2020-01-12 02:30:28 -05:00 committed by Erez Rokah
parent 24a81ef9b7
commit 29299097cf
2 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,53 @@
import { stripIndent } from 'common-tags';
import { remarkParseShortcodes } from '../remarkShortcodes';
// Stub of Remark Parser
function process(value, plugins, processEat = () => {}) {
const eat = () => processEat;
function Parser() {}
Parser.prototype.blockTokenizers = {};
Parser.prototype.blockMethods = [];
remarkParseShortcodes.call({ Parser }, { plugins });
Parser.prototype.blockTokenizers.shortcode(eat, value);
}
describe('remarkParseShortcodes', () => {
let editorComponent;
beforeEach(() => {
editorComponent = {
id: 'foo',
pattern: /bar/,
fromBlock: jest.fn(),
};
});
it('should parse shortcodes', () => {
process('foo bar', [editorComponent]);
expect(editorComponent.fromBlock).toHaveBeenCalledWith(expect.arrayContaining(['bar']));
});
it('should parse multiline shortcodes', () => {
const value = stripIndent`
foo
bar
`;
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);
});
});

View File

@ -10,10 +10,9 @@ export function remarkParseShortcodes({ plugins }) {
function createShortcodeTokenizer({ plugins }) {
return function tokenizeShortcode(eat, value, silent) {
const potentialMatchValue = value.split('\n\n')[0];
let match;
const plugin = plugins.find(plugin => {
match = potentialMatchValue.trim().match(plugin.pattern);
match = value.match(plugin.pattern);
return !!match;
});