notion2markdown
Export notion pages and databases to markdown.
For a zero-installation demo:
- Check out the example Notion page, which is pictured above.
- Navigate to the example script, hosted on replit, and hit the "Run" button.
- 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
Metadata
Release files for notion2markdown 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| notion2markdown-0.2.0.tar.gz | 11.7 kB | Without provenance | Details for notion2markdown-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| notion2markdown-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for notion2markdown-0.2.0-py3-none-any.whl |
Total release size: 22.5 kB
Release files / notion2markdown-0.2.0.tar.gz
| Download URL | notion2markdown-0.2.0.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a12f244d861738468bd3c5d4171147e334328af1e2da0a775b8a53731b63cd1b
|
|
BLAKE2b-256 checksum How to use checksums |
1f2b13f8ecb335d098f561da9e83d641e57cbc12beb2ac00b60e3c4a173c6a05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / notion2markdown-0.2.0-py3-none-any.whl
| Download URL | notion2markdown-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4fbe91c75cb2991a9324dee5deaf8c52c7c0986fc98b23f2ff5ec7d01e0b3d2a
|
|
BLAKE2b-256 checksum How to use checksums |
5f2db86e14b3cc2abe85c735ebd827687bf0001f51a5594061390ed62e68f95c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Security for notion2markdown 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.