@udus/notion-renderer
Version:

64 lines (63 loc) • 2.3 kB
JavaScript
import { generateUUID } from "../../utils/utils.js";
export const wrapListItems = (blocks) => {
return blocks.reduce((prevList, currBlock) => {
/* If the block.type is neither
* 'bulleted_list_item' nor 'numbered_list_item' nor 'bulleted_list' nor 'numbered_list',
* return the block as it is.
*/
if (currBlock.type !== "bulleted_list" &&
currBlock.type !== "numbered_list" &&
currBlock.type !== "bulleted_list_item" &&
currBlock.type !== "numbered_list_item") {
return [...prevList, currBlock];
}
const prevBlock = prevList[prevList.length - 1];
// For the first time,
// create a list object and add list_item to the list.
const id = generateUUID();
if (
// bulleted_list_item
currBlock.type === "bulleted_list_item" &&
prevBlock?.type !== "bulleted_list") {
const bulletedList = {
id,
type: "bulleted_list",
bulleted_list: { items: [currBlock] },
};
return [...prevList, bulletedList];
}
if (
// numbered_list_item
currBlock.type === "numbered_list_item" &&
prevBlock?.type !== "numbered_list") {
const numberedList = {
id,
type: "numbered_list",
numbered_list: { items: [currBlock] },
};
return [...prevList, numberedList];
}
// Intermediate,
// add the second and subsequent list items to the previous list.
if (
// bulleted_list
currBlock.type === "bulleted_list_item" &&
prevBlock?.type === "bulleted_list") {
prevBlock.bulleted_list.items = [
...prevBlock.bulleted_list.items,
currBlock,
];
}
if (
// numbered_list_item
currBlock.type === "numbered_list_item" &&
prevBlock?.type === "numbered_list") {
prevBlock.numbered_list.items = [
...prevBlock.numbered_list.items,
currBlock,
];
}
// If the condition is not met, do not display/render.
return prevList;
}, []);
};