@wordpress/blocks
Version:
Block API for WordPress.
119 lines (117 loc) • 4.71 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/blocks/src/api/raw-handling/shortcode-converter.js
var shortcode_converter_exports = {};
__export(shortcode_converter_exports, {
default: () => shortcode_converter_default
});
module.exports = __toCommonJS(shortcode_converter_exports);
var import_shortcode = require("@wordpress/shortcode");
var import_factory = require("../factory");
var import_registration = require("../registration");
var import_get_block_attributes = require("../parser/get-block-attributes");
var import_apply_built_in_validation_fixes = require("../parser/apply-built-in-validation-fixes");
var castArray = (maybeArray) => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
var beforeLineRegexp = /(\n|<p>)\s*$/;
var afterLineRegexp = /^\s*(\n|<\/p>)/;
function segmentHTMLToShortcodeBlock(HTML, lastIndex = 0, excludedBlockNames = []) {
const transformsFrom = (0, import_factory.getBlockTransforms)("from");
const transformation = (0, import_factory.findTransform)(
transformsFrom,
(transform) => excludedBlockNames.indexOf(transform.blockName) === -1 && transform.type === "shortcode" && castArray(transform.tag).some(
(tag) => (0, import_shortcode.regexp)(tag).test(HTML)
)
);
if (!transformation) {
return [HTML];
}
const transformTags = castArray(transformation.tag);
const transformTag = transformTags.find(
(tag) => (0, import_shortcode.regexp)(tag).test(HTML)
);
let match;
const previousIndex = lastIndex;
if (match = (0, import_shortcode.next)(transformTag, HTML, lastIndex)) {
lastIndex = match.index + match.content.length;
const beforeHTML = HTML.substr(0, match.index);
const afterHTML = HTML.substr(lastIndex);
if (!match.shortcode.content?.includes("<") && !(beforeLineRegexp.test(beforeHTML) && afterLineRegexp.test(afterHTML))) {
return segmentHTMLToShortcodeBlock(HTML, lastIndex);
}
if (transformation.isMatch && !transformation.isMatch(match.shortcode.attrs)) {
return segmentHTMLToShortcodeBlock(HTML, previousIndex, [
...excludedBlockNames,
transformation.blockName
]);
}
let blocks = [];
if (typeof transformation.transform === "function") {
blocks = [].concat(
transformation.transform(match.shortcode.attrs, match)
);
blocks = blocks.map((block) => {
block.originalContent = match.shortcode.content;
return (0, import_apply_built_in_validation_fixes.applyBuiltInValidationFixes)(
block,
(0, import_registration.getBlockType)(block.name)
);
});
} else {
const attributes = Object.fromEntries(
Object.entries(transformation.attributes).filter(([, schema]) => schema.shortcode).map(([key, schema]) => [
key,
schema.shortcode(match.shortcode.attrs, match)
])
);
const blockType = (0, import_registration.getBlockType)(transformation.blockName);
if (!blockType) {
return [HTML];
}
const transformationBlockType = {
...blockType,
attributes: transformation.attributes
};
let block = (0, import_factory.createBlock)(
transformation.blockName,
(0, import_get_block_attributes.getBlockAttributes)(
transformationBlockType,
match.shortcode.content,
attributes
)
);
block.originalContent = match.shortcode.content;
block = (0, import_apply_built_in_validation_fixes.applyBuiltInValidationFixes)(
block,
transformationBlockType
);
blocks = [block];
}
return [
...segmentHTMLToShortcodeBlock(
beforeHTML.replace(beforeLineRegexp, "")
),
...blocks,
...segmentHTMLToShortcodeBlock(
afterHTML.replace(afterLineRegexp, "")
)
];
}
return [HTML];
}
var shortcode_converter_default = segmentHTMLToShortcodeBlock;
//# sourceMappingURL=shortcode-converter.js.map