notionast-util-from-notionapi

Convert a Notion page into an abstract syntax tree.


Keywords
nodejs, notion, api, nast
License
MIT
Install
npm install notionast-util-from-notionapi@0.3.1

Documentation

nast-util-from-notionapi

Moved to nast/packages/nast-util-from-notionapi

version language license Notion to Nast

Convert a Notion.so page into a tree object, using Notion's API.

For related projects, please see the topic notajs.

Documentation

Usage

npm i nast-util-from-notionapi

Also need notionapi-agent to retrieve raw data from Notion's API.

npm i notionapi-agent

require this module,

const { getOnePageAsTree, getAllBlocksInOnePage } = require('nast-util-from-notionapi')

Example

const fs = require('fs')

const NotionAgent = require('notionapi-agent')
const { getOnePageAsTree, getAllBlocksInOnePage } = require('nast-util-from-notionapi')

/* Configure NotionAgent's options */
const options = {
  token: '',
  suppressWarning: false,
  verbose: true
}
const agent = new NotionAgent(options)

async function main() {
  try {
    /* Fill in a page ID */
    let pageID = ''
    let tree = await getOnePageAsTree(pageID, agent)
    let rawBlocks = await getAllBlocksInOnePage(pageID, agent)
    fs.writeFileSync(
        `PageTree-${pageID}.json`,
        JSON.stringify(tree),
        { encoding: 'utf-8' }
    )
    fs.writeFileSync(
        `RawBlocks-${pageID}.json`,
        JSON.stringify(rawBlocks),
        { encoding: 'utf-8' }
    )
  } catch (error) {
    console.error(error)
  }
}

main()

API Reference

async getOnePageAsTree(pageID, agent)

Download a page as a tree object in an easier-to-work-with format.

  • pageID - (required) The ID of a Notion page. It must be the one with dashes as below :

    cbf2b645-xxxx-xxxx-xxxx-xxxxe8cfed93
    
  • agent - (required) A Notion.NotionAgent instance. Either from notionapi-agent package or a compatible implementation.

Returns :

A tree consists of Nast.Block nodes.

interface Nast.Block {
  id: string
  type: string
  color?: string
  createdTime: number
  lastEditedTime: number
  children: Nast.Block[]
}

async getAllBlocksInOnePage(pageID, agent)

Download all blocks of a page in Notion's record format.

Function parameters are the same as getOnePageAsTree.

Returns :

Notion.Record[], with value property of every Notion.Record being Notion.Block

[
  {
    role: string
    value: Notion.Block
  }
]

Notes

Code Structure

Generated by dependency-cruiser NPM package.

dependency graph