@wordpress/block-library
Version:
Block library for the WordPress editor.
38 lines (37 loc) • 1.25 kB
JavaScript
import { autop, removep } from "@wordpress/autop";
import { getBlockTransforms, rawHandler } from "@wordpress/blocks";
import { next } from "@wordpress/shortcode";
// packages/block-library/src/shortcode/transforms.js
const getShortcodeFromTransforms = () => getBlockTransforms("from").filter((transform) => transform.type === "shortcode" && transform.blockName !== "core/shortcode");
const isSingleShortcode = (text, tag) => {
const trimmed = text.trim();
const match = next(tag, trimmed);
return !!match && match.index === 0 && match.content.length === trimmed.length;
};
const transforms = {
from: [{
type: "shortcode",
tag: "[a-z][a-z0-9_-]*",
attributes: { text: {
type: "string",
shortcode: (attrs, { content }) => {
return removep(autop(content));
}
} },
priority: 20
}],
get to() {
return getShortcodeFromTransforms().map((fromTransform) => ({
type: "block",
blocks: [fromTransform.blockName],
isMatch: ({ text = "" }) => {
return [].concat(fromTransform.tag).some((tag) => isSingleShortcode(text, tag));
},
transform: ({ text = "" }) => {
return rawHandler({ HTML: `<p>${text.trim()}</p>` });
}
}));
}
};
export { transforms as default };
//# sourceMappingURL=transforms.mjs.map