0.3.0 • Published 17 hours ago

notion2content v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
17 hours ago

notion2content

Notion のデータベースを変換しダウンロードするライブラリーとサンプル実装としての CLI ツール。

  • Properties は Frontmatter で扱いやすい JSON へ変換
  • Blocks は hast へ変換(CLI ツールでは hast から HTML/Markdown へ変換)

実装中。

Install

CLI ツールとしてインストール。

npm install -g notion2content

ライブラリーとしてインストール。

npm install --save notion2content

Usage

データベースを ndjson(JSON Lines) として出力。

$ export NOTION2CONTENT_API_KEY=<NOTION API KEY>
$ notion2content dist/main.js --database-id <DATABASE ID>
{"id":"*****","props":{ ... },"content":{"type":"root","children":[ ... ]}}
{"id":"*****","props":{ ... },"content":{"type":"root","children":[ ... ]}}
{"id":"*****","props":{ ... },"content":{"type":"root","children":[ ... ]}}

ページ別にファイルへ保存。HTML と Markdown の場合は Propery が Frontmatter となる。

$ notion2content dist/main.js --database-id <DATABASE ID> --save-dir ./tmp --save-format md
$ ll tmp
total 20
drwxrwxrwx+  2 vscode vscode 4096 Sep 30 16:15 ./
drwxrwxrwx+ 12 vscode root   4096 Sep 30 15:16 ../
-rw-rw-rw-   1 vscode vscode   41 Sep 30 16:15 *****.md
-rw-rw-rw-   1 vscode vscode   38 Sep 30 16:15 *****.md
-rw-rw-rw-   1 vscode vscode   48 Sep 30 16:15 *****.md

API

Client abstract class

@notionhq/client を使った実装方法。

import { Client as NotionClient } from '@notionhq/client'
import { ClientOptions } from '@notionhq/client/build/src/Client'
import { Client } from 'notion2content'

class CliClient extends Client {
  private client: NotionClient
  constructor(options?: ClientOptions) {
    super()
    this.client = new NotionClient(options)
  }
  queryDatabases(
    ...args: Parameters<NotionClient['databases']['query']>
  ): ReturnType<NotionClient['databases']['query']> {
    return this.client.databases.query(...args)
  }
  listBlockChildren(
    ...args: Parameters<NotionClient['blocks']['children']['list']>
  ): ReturnType<NotionClient['blocks']['children']['list']> {
    return this.client.blocks.children.list(...args)
  }
}

const client = new CliClient({
  auth: 'youre api key'
})

toContent function

import { toContent } from 'notion2content'
const client = new CliClient({
  auth: 'youre api key'
})
const ite = toContent(client, {
  target: ['props', 'content'],
  query: { database_id: '*****' },
  toItemsOpts: {},
  toHastOpts: {}
})
for await (const content of ite) {
  console.log(`${JSON.stringify(content)}\n`)
}

Options

inOpts.target

propscontent のどちらを変換するか配列で指定(同時にどちらも指定可能)

inOpts.workersNum

content の変換(child ブロックのフェッチ)を並行で行う数。

inOpts.query

Notion Clinet の databases.query へ渡す引数。

inOpts.skip

query で取得したデータをスキップする数。

inOpts.limit

query で取得したデータを制限する数。

inOpts.toItemsOpts.initialIndex

props に追加する index の初期値。

index とは toContent が返す iterator から取得したときに props に追加する連番。

inOpts.toItemsOpts.indexName

props に追加する index 名。

inOpts.toItemsOpts.toHastOpts

notion2hastblockToHast へ渡すオプション.

対応状況

Properties

  • number - null0 へ変換される
  • url - plain text へ変換される
  • select - null はブランクへ変換される
  • multi_select
  • status - plain text へ変換される
  • date - Date property values を出力(null は '' へ変換される)
  • email - plain text へ変換される
  • phone_number - plain text へ変換される
  • checkbox - boolean へ変換される
  • files
  • created_by - Created by property valuesを出力(person のみ対応、nullundefined'' へ変換される)
  • created_time - plain text へ変換される
  • last_edited_by - Last edited by property valuesを出力(person のみ対応、nullundefined'' へ変換される)
  • last_edited_time - plain text へ変換される
  • formula
  • title - plain text へ変換される
  • rich_text - plain text へ変換される
  • people - People property valuesを出力(person のみ対応、nullundefined'' へ変換される)
  • relation - id の配列へ変換
  • rollup

Blocks

Blocks の変換は notion2hast で対応。

License

MIT License

Copyright (c) 2022 hankei6km