Mk Notes
Guides

Programmatic usage

Reference guide to use Mk Notes programmatically

Mk Notes can be used programmatically in your Javascript/Typescript projects.


The library exposes a MkNotes class that allows you to use the main functionalities of Mk Notes directly inside your code.

Installation

npm install mk-notes

Basic Usage

import MkNotes from 'mk-notes';

const client = new MkNotes({
  notionApiKey: 'YOUR_NOTION_SECRET',
});

// Preview synchronization
client
  .previewSynchronization({
    inputPath: './notes/',
    format: 'plainText',
  })
  .then(console.log);

Constructor Options

OptionTypeDescriptionDefault
notionApiKeystringYour Notion API secret token (required)-
LOG_LEVEL'error' | 'warn' | 'info' | 'debug'Log level for the client'error'
loggerwinston.LoggerCustom Winston logger instance-
import MkNotes from 'mk-notes';

const client = new MkNotes({
  notionApiKey: process.env.NOTION_API_KEY,
  LOG_LEVEL: 'info',
});

Methods

previewSynchronization

Preview how your markdown files will be organized in Notion without making any changes.

const preview = await client.previewSynchronization({
  inputPath: './docs/',
  format: 'plainText', // or 'json'
  output: './preview.txt', // optional: save to file
});

console.log(preview);

Parameters

ParameterTypeDescriptionRequired
inputPathstringPath to a markdown file or directoryYes
format'plainText' | 'json'Output format for the previewYes
outputstringPath to save the preview outputNo

synchronizeMarkdownToNotionFromFileSystem

Synchronize markdown files to a Notion page or database.

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: 'https://notion.so/myworkspace/page-123456',
  cleanSync: false,
  lockPage: false,
  saveId: true,
  forceNew: false,
});

Parameters

ParameterTypeDescriptionDefault
inputPathstringPath to a markdown file or directoryRequired
parentNotionPageIdstringURL of the Notion page or databaseRequired
cleanSyncbooleanRemove existing content before syncingfalse
lockPagebooleanLock the Notion page after syncingfalse
saveIdbooleanSave Notion page IDs back to markdown frontmatterfalse
forceNewbooleanForce creation of new pages, ignoring existing IDsfalse

Examples

Basic Sync

import MkNotes from 'mk-notes';

const client = new MkNotes({
  notionApiKey: process.env.NOTION_API_KEY,
});

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: process.env.NOTION_PAGE_URL,
});

Sync with Incremental Updates

Use saveId: true to enable incremental updates. After the first sync, page IDs are saved to your markdown files and subsequent syncs will update existing pages.

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: process.env.NOTION_PAGE_URL,
  saveId: true,
});

Clean Sync with Locking

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: process.env.NOTION_PAGE_URL,
  cleanSync: true,
  lockPage: true,
});

Force New Pages

Create new pages regardless of existing IDs in the frontmatter:

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: process.env.NOTION_PAGE_URL,
  forceNew: true,
  saveId: true, // Save the new page IDs
});

Complete Reset

Combine options for a complete reset:

await client.synchronizeMarkdownToNotionFromFileSystem({
  inputPath: './docs/',
  parentNotionPageId: process.env.NOTION_PAGE_URL,
  cleanSync: true, // Delete existing content
  forceNew: true, // Ignore existing IDs
  saveId: true, // Save new IDs
});