Last active
October 11, 2022 14:46
-
-
Save dmdboi/c178b5c93483ed453af06a82aeccb5b8 to your computer and use it in GitHub Desktop.
Save Notion articles as Nuxt/Content-ready markdown files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script reads all pages in a Notion DB and saves them as Markdown files ready for Nuxt/Content. | |
// You will need a Notion Integration key to make the Client() work and the ID of the database yo want to save. | |
// Run this from the root dir of your Nuxt project. | |
const fs = require('fs-extra'); | |
const { Client } = require("@notionhq/client"); | |
const { NotionToMarkdown } = require("notion-to-md"); | |
const notionAuth = "" | |
const database_id = "" | |
const notion = new Client({ | |
auth: notionAuth | |
}) | |
const n2m = new NotionToMarkdown({ notionClient: notion }); | |
async function fetchDatabaseArticles() { | |
return (await notion.databases.query({ database_id })).results | |
} | |
async function convertArticleToMD(article) { | |
if (!article.properties.Status.status) return; | |
const blocks = await n2m.pageToMarkdown(article.id); | |
const content = n2m.toMarkdownString(blocks); | |
const { properties, cover, created_time } = article | |
const slug = properties.Slug.rich_text[0]["plain_text"] | |
const data = `--- | |
title: ${properties.Name.title[0]["plain_text"]} | |
slug: ${slug} | |
cover: ${cover['external'] ? cover['external'].url : cover['file'].url} | |
snippet: ${properties.Snippet.rich_text[0]["plain_text"]} | |
created_at: ${created_time} | |
published: ${properties.Status.status && properties.Status.status.name === 'Done' ? true : false} | |
type: ${properties.Type.select.name} | |
--- | |
${content} | |
` | |
console.log(`Writing ${slug} to file.`) | |
return await fs.writeFileSync(`./content/articles/${slug}.md`, data) | |
} | |
async function main() { | |
const articles = await fetchDatabaseArticles() | |
return articles.map(a => convertArticleToMD(a)) | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment