Skip to main content

Export notion pages to markdown in Python

Project description

notion2markdown

Export notion pages and databases to markdown.

Screenshot 2022-12-30 at 6 48 22 PM

For a zero-installation demo:

  1. Check out the example Notion page, which is pictured above.
  2. Navigate to the example script, hosted on replit, and hit the "Run" button.
  3. The Notion page is exported as markdown to ./md/f8deb4d042034c6c8d03b6de37a99498.md. You can view this file by clicking on "show files" in the top-left of the embedded editor.

Install

pip install notion2markdown

CLI

Before getting started, follow the first two steps in this notion integration tutorial to create a token and grant access to a notion page.

Put the following in your ~/.zshrc, and start a new terminal session.

export NOTION_TOKEN=my_notion_token

Then, export a notion page or database.

notion2markdown my_notion_url

If you get the following error, notion_client.errors.APIResponseError: Could not find block with ID, follow the suggestion provided: Make sure the relevant pages and databases are shared with your integration. Click here for a tutorial on granting your integration access to your database or page.

By default markdown will be exported to a directory named ./md. You can also use the n2md alias

n2md my_notion_url

Library

You can also write a script to export, programmatically. See example.py.

from notion2markdown import NotionExporter
import os


exporter = NotionExporter(token=os.environ["NOTION_TOKEN"])
exporter.export_url(url='my_notion_url')

You may optionally download JSON, then convert to markdown separately. This may be helpful if you want to cache downloads, for example. You can use the exporter's downloader and converter separately, like this:

exporter.downloader.download_url(url)  # Download json
exporter.converter.convert()  # Convert json to md

You may also export to any directory of your choosing.

exporter.export_url(url, json_dir='./my_md_directory')

Why use this library?

To start, Notion's official markdown export is (1) available only via the UI and (2) buggy.

1. Fix random asterisks

Let's say we have the following piece of text. Turns out this is a pain to export to markdown correctly, from Notion's block data structure.

Here is a sentence that was bolded then typed.

Notion randomly adds a ton of asterisks. This occurs anytime you edit a bolded or italicized piece of text. To reproduce: In Notion, bold a line, then type out that line. When you export, you'll get something like the following with random leading or trailing asterisks.

************************Here is a sentence that was bolded ****then**** typed.**

notion2md generates unparse-able markdown. notion2md partially solves the above problem. There are no spurious leading asterisks, but it treats every piece of text with different annotations, separately. This means it breaks the above sentence in 3 pieces, rendering bold text, then a bold-italic text, then a bold text. This leads to the following, which is technically correct but uninterpretable by markdown parsers:

**Here is a sentence that was bolded *****then***** typed.**

Passing either of the above markdown into markdown conversion utilities will result in spurious asterisks throughout your text. By contrast, notion2markdown will render the following, which renders correctly with any standard markdown engine:

**Here is a sentence that was bolded *then* typed.**

It's worth noting that notion2md and the Notion markdown export both otherwise generate valid markdown. This annoying edge case bothered me enough to write this library.

2. Export databases, with metadata

Notion's official markdown export includes the title along with any properties associated with the page.

This library notion2markdown does the same, adding properties and the title. However, unlike the official Notion export, properties are included as YAML frontmatter, which is supported in modern markdown flavors. By contrast, notion2md excludes the metadata and page properties, just exporting the page content.

Furthermore, notion2markdown can export an entire database, like Notion's official export. On the other hand, notion2md is designed to export individual pages. Naturally, it could be extended to export entire databases.

Develop

git clone git@github.com:alvinwan/notion2markdown.git
pip install --editable .

Run tests

pytest notion2markdown --doctest-modules

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notion2markdown-0.2.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

notion2markdown-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file notion2markdown-0.2.0.tar.gz.

File metadata

  • Download URL: notion2markdown-0.2.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for notion2markdown-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a12f244d861738468bd3c5d4171147e334328af1e2da0a775b8a53731b63cd1b
MD5 b5b16a9f6653b05e1658afabd0c15fcf
BLAKE2b-256 1f2b13f8ecb335d098f561da9e83d641e57cbc12beb2ac00b60e3c4a173c6a05

See more details on using hashes here.

File details

Details for the file notion2markdown-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for notion2markdown-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fbe91c75cb2991a9324dee5deaf8c52c7c0986fc98b23f2ff5ec7d01e0b3d2a
MD5 da9ad0e97632527d322eead632e5986e
BLAKE2b-256 5f2db86e14b3cc2abe85c735ebd827687bf0001f51a5594061390ed62e68f95c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page