@wordpress/blocks
Version:
Block API for WordPress.
57 lines (56 loc) • 2.08 kB
JavaScript
// packages/blocks/src/api/raw-handling/index.js
import deprecated from "@wordpress/deprecated";
import { getPhrasingContentSchema } from "@wordpress/dom";
import { htmlToBlocks } from "./html-to-blocks";
import parse from "../parser";
import normaliseBlocks from "./normalise-blocks";
import specialCommentConverter from "./special-comment-converter";
import listReducer from "./list-reducer";
import blockquoteNormaliser from "./blockquote-normaliser";
import figureContentReducer from "./figure-content-reducer";
import shortcodeConverter from "./shortcode-converter";
import { deepFilterHTML, getBlockContentSchema } from "./utils";
import { pasteHandler } from "./paste-handler";
function deprecatedGetPhrasingContentSchema(context) {
deprecated("wp.blocks.getPhrasingContentSchema", {
since: "5.6",
alternative: "wp.dom.getPhrasingContentSchema"
});
return getPhrasingContentSchema(context);
}
function rawHandler({ HTML = "" }) {
if (HTML.indexOf("<!-- wp:") !== -1) {
const parseResult = parse(HTML);
const isSingleFreeFormBlock = parseResult.length === 1 && parseResult[0].name === "core/freeform";
if (!isSingleFreeFormBlock) {
return parseResult;
}
}
const pieces = shortcodeConverter(HTML);
const blockContentSchema = getBlockContentSchema();
return pieces.map((piece) => {
if (typeof piece !== "string") {
return piece;
}
const filters = [
// Needed to adjust invalid lists.
listReducer,
// Needed to create more and nextpage blocks.
specialCommentConverter,
// Needed to create media blocks.
figureContentReducer,
// Needed to create the quote block, which cannot handle text
// without wrapper paragraphs.
blockquoteNormaliser({ raw: true })
];
piece = deepFilterHTML(piece, filters, blockContentSchema);
piece = normaliseBlocks(piece, { raw: true });
return htmlToBlocks(piece, rawHandler);
}).flat().filter(Boolean);
}
export {
deprecatedGetPhrasingContentSchema,
pasteHandler,
rawHandler
};
//# sourceMappingURL=index.js.map