Script to use Notion as CMS for Gatsby website.
The script
- uses the newly released Notion API to retrieve posts from a Notion database with the status "Publish",
- converts the posts to Markdown (as best it can, see "Limitations") and saves them into the local blog directory,
- adds and commits all new posts to GitHub and then pushes them to their repository,
- and updates their statuses in Notion to "Published".
Since my Gatsby site is deployed through GitHub and Netlify, the site is then rebuilt with the new posts.
I set the script on a timer and then installed it as a Windows service so that it always runs in the background.
Your machine will need Node.js and Git installed and a local Git repository for the blog in question. For more information about Git requirements, see here.
- Clone this repository on your local machine and run
npm install
to install all relevant modules. - If you want to use it as a Windows service, also run
npm link node-windows
. - Create an
.env
file with the following variables:
NOTION_TOKEN
: Internal integration token provided by NotionNOTION_DATABASE_ID
: ID for database to query for posts - you can use this function to find itBLOG_DIRECTORY
: Path to local GitHub repository for your blog - this is where Git commands will run fromPOSTS_DIRECTORY
: Path from blog directory to wherever posts should be saved - this should just be something likesrc\posts
- Review the following settings in
index.js
:
runEveryXMin
: How frequently the script should run, in minutesdbFilter
: What Notion properties the script should use to filter for new posts - see heredbUpdate
: What Notion properties the script should update upon success - see here- Other to note: the script pulls these properties from the Notion page, and you'll need to change this if you structure your posts differently: Description (text), Slug (text), Category (select), Tags (multi-select)
- To run the script, type
npm start
. - To set it up as a serivce, type
node service.js
.
Right now, the Notion API has no support for quotes or for codeblocks, and this script has no support for numbered lists.