UNPKG

@wordpress/block-library

Version:
83 lines (82 loc) • 3.59 kB
import { store, useEntityBlockEditor } from "@wordpress/core-data"; import { useSelect } from "@wordpress/data"; import { __unstableStripHTML } from "@wordpress/dom"; import { useMemo } from "@wordpress/element"; import { addQueryArgs, removeQueryArgs } from "@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 ? 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 = addQueryArgs(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: __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] = useEntityBlockEditor("postType", postType, { id: postId }); const permalink = useSelect((select) => postId ? select(store).getEditedEntityRecord("postType", postType, postId)?.link : void 0, [postType, postId]); return useMemo(() => { if (!postId || !blocks) return EMPTY_ARRAY; return getLatestHeadings(blocks, clientId, permalink, { onlyIncludeCurrentPage, maxLevel }); }, [ blocks, clientId, permalink, postId, onlyIncludeCurrentPage, maxLevel ]); } export { useHeadings }; //# sourceMappingURL=hooks.mjs.map