notion-block-renderer
Version:
Notion Block to React Components.
142 lines (141 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBlocks = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
// import { FC } from "react";
const types_1 = require("../types/types");
const BlockList_1 = require("./BlockList");
const NotionBlockCore_1 = require("./NotionBlockCore");
const getBlocks = (blocks) => {
let preBlockType = "";
let components = [];
let blockArray = [];
for (const [index, block] of blocks.entries()) {
let isPreBlockList = Object.values(types_1.BlockListEnum).includes(preBlockType);
let isCurrentBlockList = Object.values(types_1.BlockListEnum).includes(block.type);
/**
* This is a case that does not fall under LIST.
* normal case.
*
* example:
* <div></div> <= pre
* <div></div> <= current
*/
if (!isPreBlockList && !isCurrentBlockList) {
components.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}`));
continue;
}
/**
* this case end block group and push to components
*
* example:
* <ol>
* <li></li> <= pre
* </ol> <= add
* <div></di> <= current
*/
if (isPreBlockList && !isCurrentBlockList) {
// 1. end blockList
components.push((0, jsx_runtime_1.jsx)(BlockList_1.default, { blockType: preBlockType, children: blockArray }, `${index}.1`));
// 2. Since the current block is not blockListType, need to push to components
components.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}.2`));
// 3. empty blockArray
blockArray = [];
// 4. set preBlockType to empty
preBlockType = "";
continue;
}
/**
* list block is ongoing
*
* example:
* <ol>
* <li></li> <= pre (type: ol)
* <li></li> <= current (type: ol, same as pre)
*/
if (isPreBlockList && isCurrentBlockList && block.type === preBlockType) {
blockArray.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}.1`));
/**
* current block is last
*
* example:
* <ol>
* <li></li> <= current (this is last block)
* *** <= no more blocks
* </ol> <= add
*/
if (index + 1 === blocks.length) {
// add end blockList
components.push((0, jsx_runtime_1.jsx)(BlockList_1.default, { blockType: block.type, children: blockArray }, `${index}.2`));
}
continue;
}
/**
* Start of blockListType
*
* example:
* <div></div> <= pre
* <ol> <= add
* <li></li> <= current
*/
if (!isPreBlockList && isCurrentBlockList) {
blockArray.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}.1`));
preBlockType = block.type;
/**
* current block is last
*
* example:
* <ol>
* <li></li> <= current (this is last block)
* *** <= no more blocks
* </ol> <= add
*/
if (index + 1 === blocks.length) {
// add end blockList
components.push((0, jsx_runtime_1.jsx)(BlockList_1.default, { blockType: block.type, children: blockArray }, `${index}.2`));
}
continue;
}
/**
* Cases of different types of listings between the pre and current one.
*
* example:
* <ul>
* <li></li> <= pre
* </ul> <= add
* <ol> <= add
* <li></li> <= current
*/
if (isPreBlockList && isCurrentBlockList && block.type !== preBlockType) {
// 1. Since the pre time is set in the list, need to terminate the pre list and push components.
components.push((0, jsx_runtime_1.jsx)(BlockList_1.default, { blockType: preBlockType, children: blockArray }, `${index}.1`));
// 2. Empty the blockArray.
blockArray = [];
// 3. Push the current block to blockArray.
blockArray.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}.2`));
// 4. Set the current blockType to the preBlockType.
preBlockType = block.type;
/**
* current block is last
*
* example:
* <ol>
* <li></li> <= current (this is last block)
* *** <= no more blocks
* </ol> <= add
*/
if (index + 1 === blocks.length) {
// add end blockList
components.push((0, jsx_runtime_1.jsx)(BlockList_1.default, { blockType: block.type, children: blockArray }, `${index}.3`));
}
continue;
}
/**
* exception case
*/
console.warn(`Exception case`, block);
components.push((0, jsx_runtime_1.jsx)(NotionBlockCore_1.default, { block: block, blocks: blocks }, `${index}`));
}
return components;
};
exports.getBlocks = getBlocks;