UNPKG

@wordpress/block-library

Version:
88 lines (87 loc) • 3.88 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_data = require("@wordpress/data"); let _wordpress_dom = require("@wordpress/dom"); let _wordpress_element = require("@wordpress/element"); let _wordpress_url = require("@wordpress/url"); // packages/block-library/src/table-of-contents/hooks.js const EMPTY_ARRAY = []; /** * Flattens a block tree in document order, the same order the server walks * the post content in. * * @param {Object[]} blocks Blocks to flatten. * * @return {Object[]} Flat list of blocks. */ function flattenBlocks(blocks) { return blocks.flatMap((block) => [block, ...flattenBlocks(block.innerBlocks ?? [])]); } function getLatestHeadings(blocks, clientId, permalink, { onlyIncludeCurrentPage, maxLevel }) { const allBlocks = flattenBlocks(blocks); const isPaginated = allBlocks.some((block) => block.name === "core/nextpage"); let tocPage = 1; if (isPaginated && onlyIncludeCurrentPage) { const tocIndex = allBlocks.findIndex((block) => block.clientId === clientId); for (const block of allBlocks.slice(0, Math.max(tocIndex, 0))) if (block.name === "core/nextpage") tocPage++; } const latestHeadings = []; /** The page (of a paginated post) a heading will be part of. */ let headingPage = 1; let headingPageLink = null; if (typeof permalink === "string") headingPageLink = isPaginated ? (0, _wordpress_url.addQueryArgs)(permalink, { page: headingPage }) : permalink; for (const block of allBlocks) if (block.name === "core/nextpage") { headingPage++; if (onlyIncludeCurrentPage && headingPage > tocPage) break; if (typeof permalink === "string") headingPageLink = (0, _wordpress_url.addQueryArgs)((0, _wordpress_url.removeQueryArgs)(permalink, ["page"]), { page: headingPage }); } else if (!onlyIncludeCurrentPage || headingPage === tocPage) { if (block.name === "core/heading") { const headingAttributes = block.attributes; if (maxLevel && headingAttributes.level > maxLevel) continue; const canBeLinked = typeof headingPageLink === "string" && typeof headingAttributes.anchor === "string" && headingAttributes.anchor !== ""; latestHeadings.push({ content: (0, _wordpress_dom.__unstableStripHTML)(headingAttributes.content.replace(/(<br *\/?>)+/g, " ")), level: headingAttributes.level, link: canBeLinked ? `${headingPageLink}#${headingAttributes.anchor}` : null }); } } return latestHeadings; } /** * Returns the headings of the post given by the block context, including * unsaved edits. * * @param {Object} options * @param {string} options.clientId Table of Contents block client ID. * @param {number} options.postId Post ID from the block context. * @param {string} options.postType Post type from the block context. * @param {boolean} options.onlyIncludeCurrentPage Whether to only include headings from the current page. * @param {number} options.maxLevel Maximum heading level to include. * * @return {Object[]} Heading data. */ function useHeadings({ clientId, postId, postType, onlyIncludeCurrentPage, maxLevel }) { const [blocks] = (0, _wordpress_core_data.useEntityBlockEditor)("postType", postType, { id: postId }); const permalink = (0, _wordpress_data.useSelect)((select) => postId ? select(_wordpress_core_data.store).getEditedEntityRecord("postType", postType, postId)?.link : void 0, [postType, postId]); return (0, _wordpress_element.useMemo)(() => { if (!postId || !blocks) return EMPTY_ARRAY; return getLatestHeadings(blocks, clientId, permalink, { onlyIncludeCurrentPage, maxLevel }); }, [ blocks, clientId, permalink, postId, onlyIncludeCurrentPage, maxLevel ]); } exports.useHeadings = useHeadings; //# sourceMappingURL=hooks.cjs.map