Skip to content

Commit

Permalink
Feature (configuration): Add option to choose whether to keep spaces …
Browse files Browse the repository at this point in the history
…in file and folder names

Closes #20

If the user decides to keep spaces, one space is kept between words in the name of a file or folder.
  • Loading branch information
leojonathanoh committed Jul 29, 2021
1 parent c390833 commit f11cb3b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
52 changes: 36 additions & 16 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ Function Remove-InvalidFileNameChars {
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string]$Name
[string]$Name,
[switch]$KeepPathSpaces
)


$newName = $Name.Split([IO.Path]::GetInvalidFileNameChars()) -join '-'
return (((($newName -replace "\s", "-") -replace "\[", "(") -replace "\]", ")").Substring(0, $(@{$true = 130; $false = $newName.length }[$newName.length -gt 150])))
$newName = $newName -replace "\[", "("
$newName = $newName -replace "\]", ")"
$newName = if ($KeepPathSpaces) {
$newName -replace "\s", " "
} else {
$newName -replace "\s", "-"
}
$newName = $newName.Substring(0, $(@{$true = 130; $false = $newName.length }[$newName.length -gt 150]))
return $newName.Trim() # Remove boundary whitespaces
}
Function Remove-InvalidFileNameCharsInsertedFiles {
param(
Expand All @@ -22,21 +30,27 @@ Function Remove-InvalidFileNameCharsInsertedFiles {
ValueFromPipelineByPropertyName = $true)]
[string]$Name,
[string]$Replacement = "",
[string]$SpecialChars = "#$%^*[]'<>!@{};"

[string]$SpecialChars = "#$%^*[]'<>!@{};",
[switch]$KeepPathSpaces
)

$rePattern = ($SpecialChars.ToCharArray() | ForEach-Object { [regex]::Escape($_) }) -join "|"

$newName = $Name.Split([IO.Path]::GetInvalidFileNameChars()) -join '-'
return ($newName -replace $rePattern, "" -replace "\s", "-")
$newName = $newName -replace $rePattern, ""
$newName = if ($KeepPathSpaces) {
$newName -replace "\s", " "
} else {
$newName -replace "\s", "-"
}
return $newName.Trim() # Remove boundary whitespaces
}

Function ProcessSections ($group, $FilePath) {
foreach ($section in $group.Section) {
"--------------" | Write-Host
"### " + $section.Name | Write-Host
$sectionFileName = "$($section.Name)" | Remove-InvalidFileNameChars
$sectionFileName = "$($section.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($FilePath)" -Name "$($sectionFileName)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
[int]$previouspagelevel = 1
Expand All @@ -52,7 +66,7 @@ Function ProcessSections ($group, $FilePath) {
$pageid = ""
$pageid = $page.ID
$pagename = ""
$pagename = $page.name | Remove-InvalidFileNameChars
$pagename = $page.name | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$fullexportdirpath = ""
$fullexportdirpath = "$($FilePath)\$($sectionFileName)"
$fullfilepathwithoutextension = ""
Expand Down Expand Up @@ -181,7 +195,7 @@ Function ProcessSections ($group, $FilePath) {
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host | Out-Null
$destfilename = ""
try {
$destfilename = $pageinsertedfile.InsertedFile.preferredName | Remove-InvalidFileNameCharsInsertedFiles
$destfilename = $pageinsertedfile.InsertedFile.preferredName | Remove-InvalidFileNameCharsInsertedFiles -KeepPathSpaces:($keepPathSpaces -eq 2)
Copy-Item -Path "$($pageinsertedfile.InsertedFile.pathCache)" -Destination "$($mediaPath)\media\$($destfilename)" -Force
}
catch {
Expand Down Expand Up @@ -357,10 +371,16 @@ $targetNotebook = Read-Host -Prompt "Enter name of notebook"

# prompt to clear escape symbols from md files
"-----------------------------------------------"
"1: Clear '\' symbol escape character from files"
"1: Clear '\' symbol escape character from files - Default"
"2: Keep '\' symbol escape"
[int] $keepescape = Read-Host -Prompt "Entry"

#prompt to replace spaces with dashes in file
"-----------------------------------------------"
"1: Replace spaces with dashes i.e. '-' in file and folder names - Default"
"2: Keep spaces in file and folder names (1 space between words, removes preceding and trailing spaces)"
[int]$keepPathSpaces = Read-Host -Prompt "Entry"

# Fix encoding problems for languages other than English
$PSDefaultParameterValues['*:Encoding'] = 'utf8'

Expand Down Expand Up @@ -407,7 +427,7 @@ try {
foreach ($notebook in $notebooks) {
" " | Write-Host
$notebook.Name | Write-Host
$notebookFileName = "$($notebook.Name)" | Remove-InvalidFileNameChars
$notebookFileName = "$($notebook.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\" -Name "$($notebookFileName)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
$NotebookFilePath = "$($notesdestpath)\$($notebookFileName)"
Expand All @@ -425,7 +445,7 @@ try {
$levelsfromroot = 1
if ($sectiongroup1.isRecycleBin -ne 'true') {
"# " + $sectiongroup1.Name | Write-Host
$sectiongroupFileName1 = "$($sectiongroup1.Name)" | Remove-InvalidFileNameChars
$sectiongroupFileName1 = "$($sectiongroup1.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\$($notebookFileName)" -Name "$($sectiongroupFileName1)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host | Out-Null
$sectiongroupFilePath1 = "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)"
Expand All @@ -436,7 +456,7 @@ try {
$levelsfromroot = 2
if ($sectiongroup2.isRecycleBin -ne 'true') {
"## " + $sectiongroup2.Name | Write-Host
$sectiongroupFileName2 = "$($sectiongroup2.Name)" | Remove-InvalidFileNameChars
$sectiongroupFileName2 = "$($sectiongroup2.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)" -Name "$($sectiongroupFileName2)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
$sectiongroupFilePath2 = "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)"
Expand All @@ -447,7 +467,7 @@ try {
$levelsfromroot = 3
if ($sectiongroup3.isRecycleBin -ne 'true') {
"### " + $sectiongroup3.Name | Write-Host
$sectiongroupFileName3 = "$($sectiongroup3.Name)" | Remove-InvalidFileNameChars
$sectiongroupFileName3 = "$($sectiongroup3.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)" -Name "$($sectiongroupFileName3)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
$sectiongroupFilePath3 = "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)\$($sectiongroupFileName3)"
Expand All @@ -458,7 +478,7 @@ try {
$levelsfromroot = 4
if ($sectiongroup4.isRecycleBin -ne 'true') {
"#### " + $sectiongroup4.Name | Write-Host
$sectiongroupFileName4 = "$($sectiongroup4.Name)" | Remove-InvalidFileNameChars
$sectiongroupFileName4 = "$($sectiongroup4.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)\$($sectiongroupFileName3)" -Name "$($sectiongroupFileName4)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
$sectiongroupFilePath4 = "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)\$($sectiongroupFileName3)\$($sectiongroupFileName4)"
Expand All @@ -469,7 +489,7 @@ try {
$levelsfromroot = 5
if ($sectiongroup5.isRecycleBin -ne 'true') {
"#### " + $sectiongroup5.Name | Write-Host
$sectiongroupFileName5 = "$($sectiongroup5.Name)" | Remove-InvalidFileNameChars
$sectiongroupFileName5 = "$($sectiongroup5.Name)" | Remove-InvalidFileNameChars -KeepPathSpaces:($keepPathSpaces -eq 2)
$item = New-Item -Path "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)\$($sectiongroupFileName3)\$($sectiongroupFileName4)" -Name "$($sectiongroupFileName5)" -ItemType "directory" -Force -ErrorAction SilentlyContinue
"Directory: $($item.FullName)" | Format-Table | Out-String | Write-Host
$sectiongroupFilePath5 = "$($notesdestpath)\$($notebookFileName)\$($sectiongroupFileName1)\$($sectiongroupFileName2)\$($sectiongroupFileName3)\$($sectiongroupFileName4)\$($sectiongroupFileName5)"
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The powershell script 'ConvertOneNote2MarkDown-v2.ps1' will utilize the OneNote
* See more details on these options here: https://pandoc.org/MANUAL.html#options
* Allow to choose whether to include page timestamp and separator at top of document
* Remove double spaces and "\" escape symbol that are created when converting with Pandoc
* Allow to choose whether to keep spaces in file and folder names (1 space between words, removes preceding and trailing spaces).
* Improved file headers, with title now as a # heading, standardized DateTime format, and horizontal line to separate from rest of document

## Known Issues
Expand Down

0 comments on commit f11cb3b

Please sign in to comment.