Package: notion2markdown
notion2markdownExport notion pages and databases to markdown.For a zero-installation demo:Check out theexample Notion page, which is pictured above.Navigate to theexample 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.Installpipinstallnotion2markdownCLIBefore getting started, follow the first two steps in thisnotion integration tutorialto create a token and grant access to a notion page.Put the following in your~/.zshrc, and start a new terminal session.exportNOTION_TOKEN=my_notion_tokenThen, export a notion page or database.notion2markdownmy_notion_urlIf 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 tutorialon granting your integration access to your database or page.By default markdown will be exported to a directory named./md. You can also use then2mdaliasn2mdmy_notion_urlLibraryYou can also write a script to export, programmatically. Seeexample.py.fromnotion2markdownimportNotionExporterimportosexporter=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 jsonexporter.converter.convert()# Convert json to mdYou 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 asterisksLet'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 boldedthentyped.Notion randomly adds a ton of asterisks.This occursanytimeyou edit a bolded or italicized piece of text. To reproduce: In Notion, bold a line,thentype 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.**notion2mdgenerates unparse-able markdown.notion2mdpartially 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 istechnicallycorrect 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,notion2markdownwill render the following, which renders correctly with any standard markdown engine:**Here is a sentence that was bolded *then* typed.**It's worth noting thatnotion2mdand the Notion markdown export both otherwise generate valid markdown. This annoying edge case bothered me enough to write this library.2. Export databases, with metadataNotion's official markdown export includes the title along with any properties associated with the page.This librarynotion2markdowndoes the same, adding properties and the title. However, unlike the official Notion export, properties are included as YAML frontmatter, which is supported inmodern markdown flavors. By contrast,notion2mdexcludes the metadata and page properties, just exporting the page content.Furthermore,notion2markdowncan export an entire database, like Notion's official export. On the other hand,notion2mdis designed to export individual pages. Naturally, it could be extended to export entire databases.Developgitclonegit@github.com:alvinwan/notion2markdown.git
pipinstall--editable.Run testspytestnotion2markdown--doctest-modules