Export notion pages to markdown in Python
Project description
notion2markdown
Export notion pages to markdown in Python.
Install
pip install notion2markdown
CLI
Before getting started, create a notion integration, and grab the token.
Put the following in your ~/.zshrc
, and start a new terminal session.
export NOTION_TOKEN=...
Then, export a notion page or database.
notion2markdown 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 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='https://lvinwan.notion.site/lvinwan/Example-Notion-Page-f8deb4d042034c6c8d03b6de37a99498')
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_special_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 in the same format that the official Notion export does. 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file notion2markdown-0.1.0.tar.gz
.
File metadata
- Download URL: notion2markdown-0.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56570e1671d8ab5466a7af1e66b9c5c087ca7875d804f101402b142dd8372a24 |
|
MD5 | 67b0ef5d8e3d9909b2a227c7ffba5cd4 |
|
BLAKE2b-256 | 2f8913cdc4845ace91531e1376827a8110dc7746a50524b02e79956a1a4109bc |
File details
Details for the file notion2markdown-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: notion2markdown-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb93fe941f4d142b8ddbc92be8494241b6173d8ec25e52c3793597471a9e9a3b |
|
MD5 | 07aaf8c4077f5e85faf4b3a28de9e63e |
|
BLAKE2b-256 | b6ee91f0b5d37acc79861457ad1f10d966784eaeccc6449622f5304c8d8521d4 |