Convert onenote to markdown / import markdown to logseq desktop with namespaces

hi all,

just came across an old onenote notebook and was able to get it converted to markdown and imported to logseq. for better or worse i wanted the pages in a OneNote parent namespace with a sub-namespace per notebook, e.g. OneNote/Notebook 1/Note A. i got a bit hung up getting namespaces to work and came up with this janky workaround if it helps anyone else in the future:

  1. Used OneNote Md Exporter from GitHub - alxnbl/onenote-md-exporter: ConsoleApp to export OneNote notebooks to Markdown formats to convert OneNote notebook pages to markdown files.

  2. OneNote Md Exporter creates subfolders for each notebook, which could be incorporated into the markdown filenames as namespaces for Logseq Desktop (non-DB version). I used a powershell snippet adapted from https://superuser.com/questions/1571699/adding-folder-names-to-filenames-using-powershell to prepend folder names to the files inside (adding %2F to seperate namespace from filename):

cd "\path\to\OneNote_export_folders"

get-childitem -recurse | 
  Where-Object {$_.Psiscontainer -eq $false} | 
    Rename-Item -NewName {$_.Directory.Name + "%2F" + $_.Name}
  1. (a) Move all renamed files to the parent folder using https://stackoverflow.com/questions/38063424/powershell-move-all-files-from-folders-and-subfolders-into-single-folder powershell snippet:
Get-ChildItem -Path "\path\to\OneNote_export_folders" -Recurse -File | Move-Item -Destination "\path\to\OneNote_export_folders"
  1. (b) Optional - I then renamed the folder containing all .md files to OneNote and ran the snippet again from step 2, adjusted as needed to prepend OneNote%2F to each filename (could probably be incorporated into step 2 but this was just a quick fix after the fact).

  2. It turns out OneNote Md Exporter adds a header with title: specified which overrides namespaces in Logseq. I removed the title: lines from each file per https://www.reddit.com/r/PowerShell/comments/tydl1p/script_to_remove_lines_from_csv_file_that_contain/:

$fileList = Get-ChildItem "\path\to\OneNote_exported_md_files" -Filter '*.md'

foreach ($file in $fileList) {
    $content = Select-String -Path $file -Pattern ^title:.*$ -NotMatch | Select-Object -ExpandProperty Line
    Set-Content -Path $file.FullName -Value $content
}
  1. I then dropped all .md files into the Logseq pages folder and re-indexed. Notes were now namespaced pages!
1 Like