revert: don't force multiline flag for editor component patterns (#3089)

* revert: don't force multiline flag for editor component patterns

refs: 338c1b68d2865a20fed851295451175b840983d4, 4839160ee6126adfbab7bfa58452560090c00737

github issues: 3088, 3086

* fix: only trim ending white spaces/line breaks when parsing shortcodes

remark validates the value of the 'eat' function using a prefix match between the original value and the value provided. Trimming the start can break that validation

* fix: change console log to warn
This commit is contained in:
Erez Rokah 2020-01-15 17:28:09 +02:00 committed by Shawn Erquhart
parent 492f6f6a20
commit c4cbae7725
2 changed files with 20 additions and 5 deletions

View File

@ -26,7 +26,7 @@ export default function createEditorComponent(config) {
icon,
widget,
// enforce multiline flag, exclude others
pattern: new RegExp(pattern, 'm'),
pattern,
fromBlock: bind(fromBlock) || (() => ({})),
toBlock: bind(toBlock) || (() => 'Plugin'),
toPreview: bind(toPreview) || (!widget && (bind(toBlock) || (() => 'Plugin'))),

View File

@ -11,8 +11,14 @@ export function remarkParseShortcodes({ plugins }) {
function createShortcodeTokenizer({ plugins }) {
return function tokenizeShortcode(eat, value, silent) {
let match;
const potentialMatchValue = value.split('\n\n')[0].trimEnd();
const plugin = plugins.find(plugin => {
match = value.match(plugin.pattern);
if (!match) {
match = potentialMatchValue.match(plugin.pattern);
}
return !!match;
});
@ -23,10 +29,19 @@ function createShortcodeTokenizer({ plugins }) {
const shortcodeData = plugin.fromBlock(match);
return eat(match[0])({
type: 'shortcode',
data: { shortcode: plugin.id, shortcodeData },
});
try {
return eat(match[0])({
type: 'shortcode',
data: { shortcode: plugin.id, shortcodeData },
});
} catch (e) {
console.warn(
`Sent invalid data to remark. Plugin: ${plugin.id}. Value: ${
match[0]
}. Data: ${JSON.stringify(shortcodeData)}`,
);
return false;
}
}
};
}