Skip to content

Commit

Permalink
fix(widget-markdown): allow multiline shortcodes (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart authored and erezrokah committed Jan 12, 2020
1 parent 24a81ef commit 2929909
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit 2929909

Please sign in to comment.