SjoerdV / ConvertOneNote2MarkDown

Ready to make the step to Markdown and saying farewell to your OneNote, EverNote or whatever proprietary note taking tool you are using? Nothing beats clear text, right? Read on!
GNU General Public License v3.0
196 stars 113 forks source link

Error when title is longer than 183 characters #4

Closed johnkyle4 closed 3 years ago

johnkyle4 commented 3 years ago

First of all, thank you for this miraculous script. I am so close to realizing my dream of moving out of OneNote to Markdown.

I'm almost there. My prior errors were caused by not running OneNote in administrator mode, and not having MS Word installed. Now it's working, except...

Many of my OneNote pages have no title, just content. OneNote substitutes note content for blank titles.

I'm getting errors in this script when my titles are blank and the substituted content is over 183 characters.

Error while publishing file 'Nice. Watts a force of nature. Yeah I play professionally and tour frequently with a singer songwriter out of western canada. Portland is a real gas. Used to love playing at Satyricon back in the day' to docx: Exception calling "Publish" with "4" argument(s): "Exception from HRESULT: 0x80042006"
pandoc.exe: C:\Users\John\Desktop\OneNote\File_Cabinet\-_-_TEST\Nice._Watts_a_force_of_nature._Yeah_I_play_professionally_and_tour_frequently_with_a_singer_songwriter_out_of_western_canada._Portland_is_a_real_gas._Used_to_love_playing_at_Satyricon_back_in_the_day.docx: openBinaryFile: does not exist (No such file or directory)
Get-Content : Cannot find path 'C:\Users\John\Desktop\OneNote\File_Cabinet\-_-_TEST\Nice._Watts_a_force_of_nature._Yeah
_I_play_professionally_and_tour_frequently_with_a_singer_songwriter_out_of_western_canada._Portland_is_a_real_gas._Used
_to_love_playing_at_Satyricon_back_in_the_day.md' because it does not exist.
At C:\Users\John\Desktop\ConvertOneNote2MarkDown-master\ConvertOneNote2MarkDown.ps1:192 char:19
+ ...           ((Get-Content -path "$($fullexportpathwithoutextension).md" ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\John\D...k_in_the_day.md:String) [Get-Content], ItemNotFoundEx
   ception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Error while renaming image file path references for file 'Nice. Watts a force of nature. Yeah I play professionally and tour frequently with a singer songwriter out of western canada. Portland is a real gas. Used to love playing at Satyricon back in the day': You cannot call a method on a null-valued expression.

I am wondering if there is a way to modify your script to handle this, to do something like truncating the title? I have thousands of notes and I can't do it manually.

I've looked at some Onetastic macros but I can't find one that works to add a title to all pages.

Thank you for your help and for creating this script 🙏

SjoerdV commented 3 years ago

hi @johnkyle4 could your try:

Hope you manage to make it work.

johnkyle4 commented 3 years ago

Thank you so much! Tried each of your suggestions, and this one is working:

if above does not work you can change line 10 to: return ($newName.Replace(" ", "_").Substring(0,$(@{$true=130;$false=$newName.length}[$newName.length -gt 130]))) to cap all file and folder names to 130 characters (number is an example, maybe you could 'up' this, not exceeding 248 NTFS limit).

Now it's drilling through my thousands of notes successfully! I will go get that cup of coffee now :)

BTW, as it's running now there are very occasional errors, for example when the title has one of these characters: [ ] Error while renaming image file path references for file 'Random shit [tbo]': A parameter cannot be found that matches parameter name 'Raw'.

These are so rare I can likely sort them out myself.

I vastly appreciate your help and hard work! This script is a godsend.

SjoerdV commented 3 years ago

@johnkyle4, Great to hear you are making good progress.

The function used to get all illegal characters ([IO.Path]::GetInvalidFileNameChars()) is not flawless but if you feel the [ ] character errors should be prevented please change line 10 from: return ($newName.Replace(" ", "_").Substring(0,$(@{$true=130;$false=$newName.length}[$newName.length -gt 130]))) to: return (((($newName -replace "\s", "_") -replace "\[", "(") -replace "\]", ")").Substring(0,$(@{$true=130;$false=$newName.length}[$newName.length -gt 130]))) This will additionally rename the square brackets to round ones.