UNPKG

@sota1235/notion-sdk-js-helper

Version:

Helper utilities for using @notion/client

234 lines (233 loc) 5.83 kB
import { DEFAULT_COLOR, richText } from "./richTextObject.js"; function forRichText(text) { if (Array.isArray(text)) { return text.map((t) => { if (typeof t === "string") { return richText(t); } return t; }); } if (typeof text === "string") { return [richText(text)]; } return [text]; } export const paragraph = (text, options = {}) => ({ type: "paragraph", paragraph: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const heading1 = (text, options = {}) => ({ type: "heading_1", heading_1: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, is_toggleable: options.isToggleable ?? false, children: options.children ?? undefined, }, }); export const heading2 = (text, options = {}) => ({ type: "heading_2", heading_2: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, is_toggleable: options.isToggleable ?? false, children: options.children, }, }); export const heading3 = (text, options = {}) => ({ type: "heading_3", heading_3: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, is_toggleable: options.isToggleable ?? false, children: options.children ?? undefined, }, }); export const callout = (text, icon, options = {}) => ({ type: "callout", callout: { rich_text: forRichText(text), icon: { emoji: icon, }, color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const quote = (text, options = {}) => ({ type: "quote", quote: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const bulletedListItem = (text, options = {}) => ({ type: "bulleted_list_item", bulleted_list_item: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const numberedListItem = (text, options = {}) => ({ type: "numbered_list_item", numbered_list_item: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const todo = (text, options = {}) => ({ type: "to_do", to_do: { rich_text: forRichText(text), checked: !!options.checked, color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const toggle = (text, options = {}) => ({ type: "toggle", toggle: { rich_text: forRichText(text), color: options.blockColor ?? DEFAULT_COLOR, children: options.children ?? undefined, }, }); export const code = (text, language) => ({ type: "code", code: { rich_text: forRichText(text), language, }, }); export const embed = (url, options = {}) => ({ type: "embed", embed: { url, caption: options.captions, }, }); export const image = (url, options = {}) => ({ type: "image", image: { external: { url, }, caption: options.captions, }, }); export const video = (url, options = {}) => ({ type: "video", video: { external: { url, }, caption: options.captions, }, }); export const file = (url, options = {}) => ({ type: "file", file: { external: { url, }, caption: options.captions, }, }); export const pdf = (url, options = {}) => ({ type: "pdf", pdf: { external: { url, }, caption: options.captions, }, }); export const bookmark = (url, options = {}) => ({ type: "bookmark", bookmark: { url, caption: options.captions, }, }); export const equation = (expression) => ({ type: "equation", equation: { expression, }, }); export const divider = () => ({ type: "divider", divider: {}, }); export const tableOfContents = (blockColor) => ({ type: "table_of_contents", table_of_contents: { color: blockColor ?? DEFAULT_COLOR, }, }); export const breadcrumb = () => ({ type: "breadcrumb", breadcrumb: {}, }); export const columnList = (children) => ({ type: "column_list", column_list: { children, }, }); export const column = (children) => ({ type: "column", column: { children: children, }, }); export const template = (text, children) => ({ type: "template", template: { rich_text: forRichText(text), children, }, }); export const linkToPage = (pageId) => ({ type: "link_to_page", link_to_page: { page_id: pageId, }, }); export const syncedBlock = (blockId, children) => { const syncedFrom = blockId !== undefined ? { block_id: blockId, } : null; return { type: "synced_block", synced_block: { synced_from: syncedFrom, children: children ?? undefined, }, }; }; export const table = (width, children, options = {}) => ({ type: "table", table: { table_width: width, children, has_column_header: !!options.has_column_header, has_row_header: !!options.has_row_header, }, }); export const tableRow = (texts) => ({ type: "table_row", table_row: { cells: texts.map((text) => forRichText(text)), }, });