Skip to main content

CLI

The MotionLink CLI enables you to transform your Notion pages into text files that can later be consumed by your static site generators. It also comes with Notion Page to Markdown transformers for those many static site generators using Markdown for content. You can learn more about the CLI tool here.

Configuration file

To use the motionlink-cli, one needs to first create a motionlink.config.js file in the root folder of their project. The file should export an array of TemplateRule objects.

/** @type {import("motionlink-cli/lib/models/config_models").TemplateRule[]} */
const rules = [];

module.exports = rules;

TemplateRule

A TemplateRule specifies 4 things, all of which are required fields:

  1. The path to the template file to populate pages with

  2. The path to the output directory where populated pages are to be written

  3. The primary DatabaseRule which specifies how the Notion pages are to be fetched and mapped

  4. An array of secondary DatabaseRule objects. Secondary DatabaseRules come into use when you want to populate the contents of a page from more than one Notion database. For example, if your primary DatabaseRule fetches articles, you can have a secondary DatabaseRule that fetches their authors.

Given below is an example of a TemplateRule

{
template: "page_template.md",
outDir: "./public",
uses: {
database: "articles",
fetchBlocks: true,
map: (page, ctx) => {
// You can access the authors database as follows.
const authors = ctx.others.authors;
return page;
},
},
alsoUses: [
{
database: "authors",
map: (page, ctx) => {
return page;
},
},
],
}

The TemplateRule object has the following type:

export type TemplateRule = {
template: string;
outDir: string;
uses: DatabaseRule;
alsoUses: DatabaseRule[];
};

DatabaseRule

A DatabaseRule tells the tool how data is be fetched from the database.

A DatabaseRule has the following type:

export type DatabaseRule = {
database: string;
fetchBlocks?: boolean;
takeOnly?: number;
map?: (notionPage: NotionPage, context: Context) => NotionPage;
sort?: SortsParams;

/**
* See: https://developers.notion.com/reference/post-database-query#post-database-query-filter
*/
filter?: object;
};
  1. database is an alias of the data source. For example, if it is articles then when running the cli tool you will need to pass an argument of the form articles=<link_access_key>, where <link_access_key> is the access key of the link to pull the data from. Technically, the data will be pulled from the Notion database the link connects.

  2. fetchBlocks is false by default. Page blocks are fetched only when it is true.

  3. takeOnly specifies the number of pages to read from the database (after the sorting). By default it is undefined which reads all pages.

  4. map is passed a notion page that it should transform to any object object it wants. The object returned by map is passed to the template file as is. Normally this is where you generate markdown. You should also set page._title to the filename you will like the populated file for the page to have. By default, this is the page id.

  5. sort and filter are the same types exported by the Notion API. Use sort to order pages and filter to exclude certain pages. You want to filter out unpublished pages by reading only for pages whose Status property is Published, Completed or Public. For example:

filter: {
or: [
{
property: "Status",
select: {
equals: "Published",
},
},
{
property: "Status",
select: {
equals: "Completed",
},
},
{
property: "Status",
select: {
equals: "Public",
},
},
],
}

Learn More

You can learn more about the types used in the motionlink-cli by referring to this file. You can also follow the link to learn more about the context object passed to map which allows you to generate markdown and fetch media.