notion-helper

1.1.28

Object with methods to construct the majority of block types supported by the Notion API.

Block types include bookmark, bulleted list item, callout, code, divider, embed, file, heading, image, numbered list item, paragraph, pdf, quote, table, table row, table of contents, to-do, toggle, and video. Some block types return null if they are provided with invalid data; you should filter these out your final children array.

Not implemented: Breadcrumb, column list, column, equation, link preview (unsupported), mention, synced block (unsupported)

block
Static Members
bookmark
bulleted_list_item
callout
code
divider
embed
file
heading_1
heading_2
heading_3
image
numbered_list_item
paragraph
pdf
quote
table
table_row
table_of_contents
to_do
toggle
video

BlockShorthand

blocks.mjs

Block shorthand methods – these allow you to call the createBlock() method for the properties of the block object more quickly. Import them directly into a file, or call them on NotionHelper.

BlockShorthand
Static Members
bookmark(options)
bulletedListItem(options)
bullet(options)
callout(options)
code(options)
divider()
embed(options)
file(options)
heading1(options)
heading2(options)
heading3(options)
image(options)
numberedListItem(options)
num(options)
paragraph(options)
pdf(options)
quote(options)
table(options)
tableRow(cells)
tableOfContents(options)
toDo(options)
toggle(options)
video(options)

makeParagraphBlocks

blocks.mjs

Simple function to create standard Paragraph blocks from an array of strings without any special formatting. Each Paragraph block will contain a single Rich Text Object.

makeParagraphBlocks(strings: Array<string>): Array<Object>
Parameters
strings (Array<string>) an array of strings.
Returns
Array<Object>: array of Paragraph blocks.
setIcon(value: string): Object
Parameters
value (string) either an emoji character or a URL for an externally-hosted image file.
Returns
Object: An object representing the icon.

createExternal

emoji-and-files.mjs

Creates a representation of an external link.

createExternal(url: string): Object
Parameters
url (string) The URL of the external link.
Returns
Object: An object containing the external URL.

Creates a representation of an emoji.

createEmoji(emoji: string): Object
Parameters
emoji (string) The emoji character.
Returns
Object: An object containing the emoji.

Creates a representation of a file link.

createFile(url: string): Object
Parameters
url (string) The URL of the file.
Returns
Object: An object containing the file URL.

page_meta

page-meta.mjs

Object with methods for constructing Notion page metadata, including parent, page, block, property, cover, and icon.

Parent creates a parent object. Page, block, and property create ID objects. Cover creates an external image object, while icon can create an external image object or an emoji object.

page_meta
Static Members
parent
page
block
property
icon
cover

PageShorthand

page-meta.mjs

Page shorthand methods - these allow you to call the createMeta() method for the properties of the page_meta object more quickly. Import them directly into a file, or call them on NotionHelper.

PageShorthand
Static Members
parentDb(database_id)
parentPage(page_id)
pageId(page_id)
blockId(block_id)
propertyId(property_id)
cover(url)
icon(url)

page_props

page-meta.mjs

Object with methods for constructing each of the possible property types within a Notion database page.

Property types include title, rich_text, checkbox, date, email, files, multi_select, number, people, phone_number, relation, select, status, and url.

page_props
Static Members
title
rich_text
checkbox
date
email
files
multi_select
number
people
phone_number
relation
select
status
url

PropertyShorthand

page-meta.mjs

Property shorthand methods - these allow you to call the setProp() method for the properties of the page_props object more quickly. Import them directly into a file, or call them on NotionHelper.

PropertyShorthand
Static Members
title(value)
richText(value)
checkbox(value)
date(start, end?)
email(value)
files(files)
multiSelect(values)
number(value)
people(people)
phoneNumber(value)
relation(values)
select(value)
status(value)
url(value)

validateValue

page-meta.mjs

Validates values passed to the setProp() methods above. Performs some transformation in certain cases.

validateValue(value: any, type: string)
Parameters
value (any) the value being passed to setProp(), which invokes this function
type (string) the type of value expected by this Notion API property

quickPages

pages.mjs
quickPages($0: Object, options: Object, parent: string, parent_type: string, pages: (Array<Object> | Object), schema: Object, childrenFn: function): Array<Object>
Parameters
$0 (Object)
Name Description
$0.parent any
$0.parent_type any
$0.pages any
$0.schema any
$0.childrenFn any
options (Object)
parent (string) The ID of the parent page or database.
parent_type (string) "page_id" or "database_id".
pages ((Array<Object> | Object)) an array of simple objects, each of which will be turned into a valid page object. Each can have property types that match to valid Notion page properties, as well as a "cover", "icon", and "children" property. The "children" prop's value should be either a string or an array. You can also pass a single object, but the function will still return an array.
schema (Object) an object that maps the schema of the pages objects to property names and types in the parent. Saves you from needing to specify the property name and type from the target Notion database for every entry in your pages object. For each property in your pages object that should map to a Notion database property, specify the key as the property name in the pages object and set the value as an array with the Notion property name as the first element and the property type as the second. Non-valid property types will be filtered out. Optionall, you can specify custom keys for the icon (["Icon", "icon"]), cover (["Cover", "cover"]), and children array (["Children", "children"]).
childrenFn (function) a callback you can specify that will run on any array elements present in a "children" property of any object in the pages array. If that "children" property contains a single string, it'll run on that as well. If omitted, any "children" values will be converted to Paragraph blocks by default.
Returns
Array<Object>: An array of page objects, each of which can be directly passed as the children for a POST request to https://api.notion.com/v1/pages (or as the single argument to notion.pages.create() when using the SDK).
Example
const database = "abcdefghijklmnopqrstuvwxyz"

const tasks = [ {
  icon: "😛",
  task: "Build Standing Desk",
  due: "2024-09-10",
  status: "Not started"
} ]

const schema = {
  task: [ "Name", "title" ],
  due: [ "Due Date", "date"],
  status: [ "Status", "status" ]
}

const pageObjects = quickPages({
     parent: database,
     parent_type: "database_id",
     pages: tasks,
     schema: schema,
     childrenFn: (value) => NotionHelper.makeParagraphs(value)
})

NotionBuilder

pages.mjs

A builder object for Notion content.

NotionBuilder

Type: Object

Properties
parentDb (function (string): this) : Sets the parent database for the page.
parentPage (function (string): this) : Sets the parent page for the page. // Add more properties here for each method in the builder

createNotion

pages.mjs

A factory function that provides methods for building Notion objects, including pages, properties, and blocks. It adds an unhealthily-large spoonful of syntactic sugar onto the Notion API.

Returns an object with two possible properties:

  1. content (always returned) - can be a full page object, an array of blocks, or a properties object.

  2. addititionalBlocks - array containing arrays of blocks passed to the builder function that go over Notion's limit for the number of blocks that can be in a children array. This allows you to add these to the returned page or block in further requests.

This builder supports chaining methods so you can build pages or structures incrementally. It also supports block-nesting with the startParent() and endParent() methods.

After adding all your blocks and properties, call build() to return the final object. It can be passed directly as the data object in Notion API requests.

createNotion(): NotionBuilder
Returns
NotionBuilder: A builder object with methods for constructing and managing Notion content. The builder includes methods to set page and property details, add various block types, manage nested structures, and ultimately build Notion-compatible objects.
Example
const notionBuilder = createNotion();

// Build a new Notion page with various blocks
const result = notionBuilder
  .parentDb('database-id')
  .title('Page Title', 'Hello World')
  .paragraph('This is the first paragraph.')
  .heading1('Main Heading')
  .build();

// Access the built content and handle additional blocks if they exist
console.log(result.content);  // The main Notion page content
console.log(result.additionalBlocks);  // Any blocks that need separate requests due to size constraints

// Create a page in Notion with the result (assumes you've installed and imported the Notion SDK and instantiated a client bound to a 'notion' variable)
const response = await notion.pages.create(result.content)

builder

pages.mjs
builder

Type: NotionBuilder

Static Members
parentDb(database_id)
parentPage(page_id)
pageId(page_id)
propertyId(property_id)
blockId(block_id)
cover(url)
icon(url)
property(name, type, value)
title(name, value)
richText(name, value)
checkbox(name, value)
date(name, start, end)
email(name, value)
files(name, fileArray)
multiSelect(name, valuesArray)
number(name, value)
people(name, personArray)
phoneNumber(name, value)
relation(name, pageArray)
select(name, value)
status(name, value)
url(name, value)
startParent(blockType, options)
endParent()
addBlock(blockType, options)
paragraph(options)
heading1(options)
heading2(options)
heading3(options)
bulletedListItem(options)
bullet(options)
numberedListItem(options)
num(options)
toDo(options)
toggle(options)
code(options)
quote(options)
callout(options)
divider()
image(options)
video(options)
file(options)
pdf(options)
bookmark(options)
embed(options)
tableOfContents(options)
table(options)
tableRow(options)
loop(blockTypeOrCallback, arr)
build()
reset()

buildRichTextObj

rich-text.mjs

Builds a Rich Text Object. See: https://developers.notion.com/reference/rich-text

buildRichTextObj(input: (string | Object), annotations: any, url: string, type: string, opts: Object): Array<Object>
Parameters
input ((string | Object)) The text content or input object. If string, the input can be normal text or an equation. If object, it can be a text, equation, or mention object.
annotations (any = {})
url (string) The URL for this object, if any. Creates a clickable link.
type (string = text) An optional type for the Rich Text Object. Supports text, equation, and mention.
opts (Object) Options for the Annotation object.
Name Description
opts.bold boolean Bold text
opts.italic boolean Italic text
opts.strikethrough boolean Strikethrough text
opts.underline boolean Underlined text
opts.code boolean Code-style text
opts.color string String specifying the text's color or background color. Opts: "blue", "brown", "default", "gray", "green", "orange", "pink", "purple", "red", "yellow". All except "default" can also be used as a background color with "[color]_background" - example: "blue_background". See: https://developers.notion.com/reference/rich-text#the-annotation-object
Returns
Array<Object>: Array with a single Rich Text Object

enforceRichText

rich-text.mjs

Enforces Rich Text format for content.

enforceRichText(content: (string | Object | Array)): Array
Parameters
content ((string | Object | Array)) The content to be enforced as Rich Text.
Returns
Array: An array of Rich Text Objects.

enforceRichTextObject

rich-text.mjs

Enforces a single Rich Text Object format.

enforceRichTextObject(obj: (string | Object)): Object
Parameters
obj ((string | Object)) The object to be enforced as a Rich Text Object.
Returns
Object: A Rich Text Object.

isSingleEmoji

utils.mjs

Checks if a string contains only a single emoji.

isSingleEmoji(string: string): boolean
Parameters
string (string)
Returns
boolean:

isValidURL

utils.mjs

Checks if a string is a valid URL.

isValidURL(string: string): boolean
Parameters
string (string)
Returns
boolean:

validateImageURL

utils.mjs

Checks if an image URL is both a valid URL and has a supported image file type.

validateImageURL(url: url): boolean
Parameters
url (url) the URL to be checked
Returns
boolean:

validateVideoURL

utils.mjs

Checks if a video URL is both a valid URL and will be accepted by the API, based on a list of supported file extensions and embed websites.

validateVideoURL(url: url): boolean
Parameters
url (url) the URL to be checked
Returns
boolean:

validatePDFURL

utils.mjs

Checks if a PDF URL is both a valid URL and ends with the .pdf extension.

validatePDFURL(url: url): boolean
Parameters
url (url) the URL to be checked
Returns
boolean:

enforceStringLength

utils.mjs

Enforces a length limit on a string. Returns the original string in a single-element array if it is under the limit. If not, returns an array with string chunks under the limit.

enforceStringLength(string: string, limit: number): Array<string>
Parameters
string (string) the string to be tested
limit (number) optional string-length limit
Returns
Array<string>: array with the original string, or chunks of the string if it was over the limit.

validateDate

utils.mjs

Validates Date object or string input that represents a date, and converts it to an ISO-8601 date string if possible.

validateDate(dateInput: any, date: (string | Date)): string
Parameters
dateInput (any)
date ((string | Date)) a Date object or string representing a date
Returns
string: