# nast-util-from-notionapi

> Transform Notion.so pages to NAST representation.

Latest version **1.4.8** (published 2023-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install nast-util-from-notionapi
pnpm add nast-util-from-notionapi
yarn add nast-util-from-notionapi
bun add nast-util-from-notionapi
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.4.8 |
| Published | 2023-09-20 |
| First published | 2019-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 98.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | dragonman225 |
| Maintainers | dragonman225 |
| Keywords | notion, nast, util, api |

## Links

- npm: https://www.npmjs.com/package/nast-util-from-notionapi
- Repository: https://github.com/dragonman225/nast
- Homepage: https://github.com/dragonman225/nast/tree/master/packages/nast-util-from-notionapi
- Issues: https://github.com/dragonman225/nast/issues
- npm.io page: https://npm.io/package/nast-util-from-notionapi

## Dependencies (2)

- [@dnpr/logger](https://npm.io/package/@dnpr/logger.md) ^0.2.0
- [notionapi-agent](https://npm.io/package/notionapi-agent.md) ^3.0.0

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.4.8 (latest) — 2023-09-20
- 1.4.7 (beta) — 2023-09-20
- 1.4.6 — 2023-09-20
- 1.4.5 — 2023-09-20
- 1.4.4 — 2023-09-20
- 1.4.3 — 2023-09-20
- 1.4.2 — 2021-11-21
- 1.4.1 — 2021-10-06
- 1.4.0 — 2021-10-06
- 1.3.1 — 2021-07-24
- 1.3.0 — 2021-07-24
- 1.2.1 — 2021-04-10
- 1.2.0 — 2021-04-10
- 1.1.1 — 2020-09-06
- 1.1.0 — 2020-09-06
- … 10 more at https://npm.io/package/nast-util-from-notionapi/versions

## README

# nast-util-from-notionapi

![version](https://img.shields.io/npm/v/nast-util-from-notionapi.svg?style=flat-square&color=007acc&label=version) ![language](https://img.shields.io/badge/language-typescript-blue.svg?style=flat-square) ![license](https://img.shields.io/github/license/dragonman225/nast-util-from-notionapi.svg?style=flat-square&label=license&color=08CE5D) ![Notion to NAST](https://img.shields.io/static/v1.svg?label=&message=Notion%E2%86%92Nast&style=flat-square&color=333333)

Convert Notion.so pages into NAST representation, using Notion's API.

## Documentation

* [Usage](#Usage)
* [Example](#Example)
* [API Reference](#API-Reference)
* [Notes](#Notes)

## Usage

```bash
npm i nast-util-from-notionapi
```

Also need [`notionapi-agent`](https://github.com/dragonman225/notionapi-agent) to retrieve raw data from Notion's API.

```bash
npm i notionapi-agent
```

`require` this module,

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

## Example

```javascript
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 [`notionapi-agent`-compatible](https://github.com/dragonman225/notionapi-agent/blob/35f406f418760536a3da8c336411a3b770ede598/src/agent.ts#L32) instance. You can just use the [`notionapi-agent`](https://github.com/dragonman225/notionapi-agent) package.

#### Returns :

A tree consists of [`NAST.Block`](https://github.com/dragonman225/nast/blob/5574180d3d36e77148c66d8608ec77c294ae817f/packages/nast-types/index.d.ts#L12) nodes.

```typescript
interface NAST.Block {
  uri: URI
  type: string
  color?: string
  createdTime: TimestampNumber
  lastEditedTime: TimestampNumber
  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.BlockRecord[]`](https://notionapi.netlify.app/globals.html#blockrecord), where the `role` property of every `Notion.BlockRecord` being a [Notion.Role](https://notionapi.netlify.app/modules/permission.html#role), and `value` property of every `Notion.BlockRecord` being a [Notion.Block](https://notionapi.netlify.app/modules/block.html)

```typescript
[
  {
    role: Notion.Role
    value: Notion.Block
  }
]
```

## Notes

### Code Structure

Generated by `dependency-cruiser` NPM package.

![dependency graph](documentation/dependency-graph.svg)

### Notice

Before NAST is redesigned to have better representation of links and references, `alias` blocks (created by "Link to page" since some day in June or July, 2021) are dereferenced to `page` blocks, `transclusion_container` block and `transclusion_reference` block are dereferenced to their content.

---
_Source: https://npm.io/package/nast-util-from-notionapi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
