Skip to content

Latest commit

 

History

History
192 lines (147 loc) · 5.21 KB

README-JP.md

File metadata and controls

192 lines (147 loc) · 5.21 KB

markdown2notion

npm version License: MIT codecov

notionのDBにmarkdownファイルを変換するツールです。フォルダ構造もタグとして表現されます。

notionのtokenの発行方法やDBとの紐付けかたはこちらを参照してください。

🔗 リンク

English👉README.md

🔽 インストール方法

npm install markdown2notion
yarn add markdown2notion

🔧 使い方

javascriptとtypescriptで使えます。

markdownToNotion()

import {markdownToNotion} from 'markdown2notion'



async function main(){
    try{
        await markdownToNotion(
        'notion_token', 
        'notion database id', 
        'markdownファイルが入っているフォルダのパス',
        'ファイル名を表示するnotionの列名。 デフォルトはTitle', 
        'タグとしてフォルダ名を表示する列名。 デフォルトはTags')
    } catch (error) {
        console.error(error);
    }
}

searchPage()

ページのURLはmarkdownToNotion()を使うたびに変更されるため、URLを使って何かしたい場合は、この関数でページのURLを取得してください。

import {searchPage} from 'markdown2notion'

async function main(){
  try{
      const result = await searchPage(
      'notion token',
      'notion database id',
      'ファイル名を表示するnotionの列名。 デフォルトはTitle', 
      'タグとしてフォルダ名を表示する列名。 デフォルトはTags'
      '検索したいファイル名',
      '検索したいファイルが入っているフォルダ名。配列で指定。'
      )
      // 同じファイル名のファイルが存在している場合、複数のページが返ってきます。
      console.log(result)// notionのpageのobjectが返ってきます。urlはresult['results'][0]['url']とかで取れます。
  } catch (error) {
      console.error(error)
  }
}

🔰 使用例

🔽markdownToNotion()

フォルダ構成

├── docs
│   ├── sample1
│   │   ├── sample1_1
│   │   │   ├── sampleContent1_1.md
│   │   ├── sampleContent1.md
│   ├── sample2
│   │   ├── sampleContent2.md
├── src
│   ├── index.ts
├── .env

notion DB

マークダウンファイルのサンプル

Sample Markdown Folder

index.ts

import {markdownToNotion} from 'markdown2notion'
import * as dotenv from 'dotenv'

async function main() {
    dotenv.config()
    const token = process.env.NOTION_TOKEN
    const databaseId = process.env.NOTION_DATABASE_ID
    try {
      await markdownToNotion(token, databaseId, 'docs', 'Title', 'Tags');
    } catch (error) {
      console.error(error);
    }
}

main()

.env

NOTION_TOKEN=secret_xxxxxxxxxxxxxx
NOTION_DATABASE_ID=xxxxxxxxxxxxxxx

index.tsの実行結果

フォルダ構造がタグとして表現されます。

タグでフィルタリングすることで、見たいファイルをすぐに探すことができます。

🔽searchPage()

index.ts

import {searchPage} from 'markdown2notion'
import * as dotenv from 'dotenv'

async function main() {
    dotenv.config()
    const token = process.env.NOTION_TOKEN
    const databaseId = process.env.NOTION_DATABASE_ID
    const title = 'sampleContent1_1';
    const tags = ['sample1_1'];

    try {
      const result = await searchPage(token, databaseId, 'Title', 'Tags', title, tags);
      console.log(result['results'][0]['url']);
    } catch (error) {
      console.error('Error searching for page:', error);
    }
}

main()

result

{
  object: 'list',
  results: [
    {
      object: 'page',
      id: '33.....',
      created_time: '2023-03-29T14:15:00.000Z',
      last_edited_time: '2023-03-29T14:15:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      cover: null,
      icon: null,
      parent: [Object],
      archived: false,
      properties: [Object],
      url: 'https://www.notion.so/sampleContent1_1-33...'
    }
  ],
  next_cursor: null,
  has_more: false,
  type: 'page',
  page: {}
}

👀 注意点

操作対象のnotionのDB上に、ファイル名と同じページがある場合は上書きされます。

ライセンス

MIT