From 2d528c2c87fdc74be1a98e1f5efe29bbbb4364b6 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 29 Oct 2023 23:35:22 +0100 Subject: [PATCH] Add README --- README.md | 31 +++++++++++++++++++++++++++++++ notion-to-joplin.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..30abcab --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# This script is used to convert a Notion export to a Joplin import (MD - Markdown directory). + +## Requirements + +- Python 3.6+ + +## Usage + +1. Export your Notion workspace as Markdown & CSV (with subpages) and unzip the archive. + +2. Download script. + +```bash +curl -O https://github.com/tanrax/notion-to-joplin/raw/main/notion-to-joplin.py +``` + +3. Run the script: + +```bash +python3 notion-to-joplin.py -f +``` + +Example: + +```bash +python3 notion-to-joplin.py -f b25c8352-f87b-4b5b-ce0a-61d09c5bd81b_Export-9e0c6ec4-762b-4d70-b30e-045ece8b4722.zip +``` + +4. Import the generated folder into Joplin. + +You can now import the folder `import to joplin` to Joplin (File > Import > MD - Markdown directory). diff --git a/notion-to-joplin.py b/notion-to-joplin.py index 4746fa0..b1cf1ee 100755 --- a/notion-to-joplin.py +++ b/notion-to-joplin.py @@ -1,4 +1,10 @@ #!/usr/bin/env python3 +# This script is used to convert a Notion export to a Joplin import (MD - Markdown directory). +# Step 1: Get the notion export zip file +# Step 2: Unzip the notion export zip file +# Step 3: Rename every file with a .md extension with the heading of the file and fix all the links +# Step 4: Rename all folder. Remove the "hash" from the ending of the folder name and fix all the links + from argparse import ArgumentParser import sys import zipfile @@ -8,12 +14,6 @@ from os import path import ntpath import urllib.parse - -# Step 1: Get the notion export zip file -# Step 2: Unzip the notion export zip file -# Step 3: Rename every file with a .md extension with the heading of the file and fix all the links -# Step 4: Rename all folder. Remove the "hash" from the ending of the folder name - # VARIABLES FOLDER_EXTRACTION = "import to joplin" MARKDOWN_EXTENSION = "md" @@ -73,3 +73,26 @@ for filename in glob.iglob(path_to_files, recursive=True): old_filename_encoded, heading_encoded ) file.write(text_to_write) +print("Renaming files and fixing links done.") + +## Step 4: Rename all folder. Remove the "hash" from the ending of the folder name +print("Renaming folders...") +path_of_folders = path.join(FOLDER_EXTRACTION, '**/*') +for folder in glob.iglob(path_of_folders, recursive=True): + if path.isdir(folder): + current_folder_name = path.basename(folder) + new_folder_name = " ".join(current_folder_name.split(" ")[:-1]) + shutil.move(folder, path.join(path.dirname(folder), new_folder_name)) + # Fix all the links + old_folder_name_encoded = urllib.parse.quote(current_folder_name) + new_folder_name_encoded = urllib.parse.quote(new_folder_name) + for filename_to_fix in glob.iglob(path_to_files, recursive=True): + with open(filename_to_fix, "r") as file: + lines_to_fix = file.readlines() + with open(filename_to_fix, "w") as file: + text_to_write = "".join(lines_to_fix).replace( + old_folder_name_encoded, new_folder_name_encoded + ) + file.write(text_to_write) +print("Renaming folders done.") +print("All done. You can now import the folder \"" + FOLDER_EXTRACTION + "\" to Joplin (File > Import > MD - Markdown directory)")