c4cbae7725
* 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
63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
export function remarkParseShortcodes({ plugins }) {
|
|
const Parser = this.Parser;
|
|
const tokenizers = Parser.prototype.blockTokenizers;
|
|
const methods = Parser.prototype.blockMethods;
|
|
|
|
tokenizers.shortcode = createShortcodeTokenizer({ plugins });
|
|
|
|
methods.unshift('shortcode');
|
|
}
|
|
|
|
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;
|
|
});
|
|
|
|
if (match) {
|
|
if (silent) {
|
|
return true;
|
|
}
|
|
|
|
const shortcodeData = plugin.fromBlock(match);
|
|
|
|
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;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
export function createRemarkShortcodeStringifier({ plugins }) {
|
|
return function remarkStringifyShortcodes() {
|
|
const Compiler = this.Compiler;
|
|
const visitors = Compiler.prototype.visitors;
|
|
|
|
visitors.shortcode = shortcode;
|
|
|
|
function shortcode(node) {
|
|
const { data } = node;
|
|
const plugin = plugins.find(plugin => data.shortcode === plugin.id);
|
|
return plugin.toBlock(data.shortcodeData);
|
|
}
|
|
};
|
|
}
|