Skip to content

Commit

Permalink
revert: don't force multiline flag for editor component patterns (#3089)
Browse files Browse the repository at this point in the history
* revert: don't force multiline flag for editor component patterns

refs: 338c1b6, 4839160

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
  • Loading branch information
erezrokah authored and erquhart committed Jan 15, 2020
1 parent 492f6f6 commit c4cbae7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand All @@ -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;
}
}
};
}
Expand Down

0 comments on commit c4cbae7

Please sign in to comment.