From d55537387e3ec548370d8259f1da5d1154081c07 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 10 Aug 2021 12:58:36 +0800 Subject: [PATCH] Hotfix: Fix markdown documents being converted with wrong file names Fixes a bug in the refactor in #50, affecting versions since v2.11.0, which occured due to insufficient unit test cases. The bug caused every subsequent page to include the previous page's basename in its name, causing increasingly long file names for every contiguous converted file. This is a cherry-pick of 4df43d35a2ab97aa5454f7093b3e73b39bd50e23 of #68. --- ConvertOneNote2MarkDown-v2.Tests.ps1 | 162 +++++++++++++++++++-------- ConvertOneNote2MarkDown-v2.ps1 | 36 +++--- 2 files changed, 136 insertions(+), 62 deletions(-) diff --git a/ConvertOneNote2MarkDown-v2.Tests.ps1 b/ConvertOneNote2MarkDown-v2.Tests.ps1 index da31f39..5db03c4 100644 --- a/ConvertOneNote2MarkDown-v2.Tests.ps1 +++ b/ConvertOneNote2MarkDown-v2.Tests.ps1 @@ -360,18 +360,20 @@ Describe "New-OneNoteConnection" -Tag 'Unit' { } function Get-FakeOneNoteHierarchy { # Sample outerXML of a hierarchy object. Here we have two identical notebooks: 'test' and 'test2' with a simple nested structure, each with 9 pages, in groups of 3: - # 1) The 1st, 2nd, and 3rd pages are uniqely named, in the notebook base - # 3) The 4th, 5th 6th pages are a copy of 1), but nested 1 level - # 3) The 7th, 8th 9th pages are a copy of 1), but nested 2 levels + # 1) The 1st, 2nd, 3rd, 4th, 5th in the notebook base + # 2) A copy of 1), but nested 1 level + # 3) A copy of 1), but nested 2 levels $hierarchy = @' - + - + + + @@ -380,24 +382,30 @@ function Get-FakeOneNoteHierarchy { - + - + + + - + - + + + - + - + + + @@ -405,15 +413,19 @@ function Get-FakeOneNoteHierarchy { - - + + + + - + + + @@ -426,8 +438,8 @@ function Get-FakeOneNoteHierarchy { function Get-FakeOneNoteHierarchyWithDuplicatePageNames { # Sample outerXML of a hierarchy object. Here we have two identical notebooks: 'test' and 'test2' with a simple nested structure, each with 9 pages, in groups of 3: # 1) The 1st, 2nd, and 3rd pages are identically named, in the notebook base - # 3) The 4th, 5th 6th pages are a copy of 1), but nested 1 level - # 3) The 7th, 8th 9th pages are a copy of 1), but nested 2 levels + # 2) A copy of 1), but nested 1 level + # 3) A copy of 1), but nested 2 levels $hierarchy = @' @@ -630,15 +642,47 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { It "Should constructs individual conversion configuration(s) for pages, based on a given Section Group XML object. Ignores pages in recycle bin." { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 + } + + It "Should get its pagePrefix from its parent page (if any)" { + $params['Config']['prefixFolders']['value'] = 1 + + $result = @( New-SectionGroupConversionConfig @params 6>$null ) + + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 + + for ($i = 0; $i -lt $result.Count; $i = $i + 5) { # Test in fives + $pageCfg1 = $result[$i] # First level page + $pageCfg2 = $result[$i + 1] # Second level page preceded by a first level page + $pageCfg3 = $result[$i + 2] # Third level page preceded by a second level page + $pageCfg4 = $result[$i + 3] # Third level page preceded by a second level page + $pageCfg5 = $result[$i + 4] # Third level page preceded by a second level page + + # Test the first level page + $pageCfg1['pagePrefix'] | Should -Be '' + + # Test the second level page preceded by a first level page + $pageCfg2['pagePrefix'] | Should -Be "$( $pageCfg1['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" + + # Test the third level page preceded by a second level page + $pageCfg3['pagePrefix'] | Should -Be "$( $pageCfg2['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" + + # Test the second level page preceded by a third level page + $pageCfg4['pagePrefix'] | Should -Be "$( $pageCfg1['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" + + # Test the first level page preceded by a second level page + $pageCfg5['pagePrefix'] | Should -Be '' + } } It "Should generate fully unique docx file for each page, even for identically-named pages in a section" { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 for ($i = 0; $i -lt $result.Count; $i++) { $count = 0 @@ -654,7 +698,7 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $params['SectionGroups'] = $fakeHierarchy.Notebooks.Notebook $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook $result.Count | Should -Be 18 for ($i = 0; $i -lt $result.Count; $i++) { @@ -673,7 +717,7 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $params['SectionGroups'] = $fakeHierarchy.Notebooks.Notebook $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook $result.Count | Should -Be 18 for ($i = 0; $i -lt $result.Count; $i = $i + 3) { # Test in threes @@ -697,8 +741,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 # Test the first object $regex = "^$( [regex]::Escape($params['Config']['notesdestpath']['value']) )" @@ -724,13 +768,15 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 - for ($i = 0; $i -lt $result.Count; $i = $i + 3) { # Test in threes + for ($i = 0; $i -lt $result.Count; $i = $i + 5) { # Test in fives $pageCfg1 = $result[$i] # First level page $pageCfg2 = $result[$i + 1] # Second level page preceded by a first level page $pageCfg3 = $result[$i + 2] # Third level page preceded by a second level page + $pageCfg4 = $result[$i + 3] # Second level page preceded by a third level page + $pageCfg5 = $result[$i + 4] # First level page preceded by a second level page # Test the first level page Split-Path $pageCfg1['fullfilepathwithoutextension'] -Leaf | Should -Be $pageCfg1['nameCompat'] @@ -746,19 +792,31 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { Split-Path $pageCfg3['fullfilepathwithoutextension'] -Leaf | Should -Be $pageCfg3['nameCompat'] Split-Path $pageCfg3['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg3['fullexportdirpath'] $pageCfg3['levelsPrefix']| Should -Be "$( '../' * ($pageCfg3['levelsFromRoot'] + $pageCfg3['pageLevel'] - 1) )" + + # Test the second level page preceded by a third level page + Split-Path $pageCfg4['fullfilepathwithoutextension'] -Leaf | Should -Be $pageCfg4['nameCompat'] + Split-Path $pageCfg4['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg4['fullexportdirpath'] + $pageCfg4['levelsPrefix']| Should -Be "$( '../' * ($pageCfg4['levelsFromRoot'] + $pageCfg4['pageLevel'] - 1) )" + + # Test the first level page preceded by a second level page + Split-Path $pageCfg5['fullfilepathwithoutextension'] -Leaf | Should -Be $pageCfg5['nameCompat'] + Split-Path $pageCfg5['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg5['fullexportdirpath'] + $pageCfg5['levelsPrefix']| Should -Be "$( '../' * ($pageCfg5['levelsFromRoot'] + $pageCfg5['pageLevel'] - 1) )" } $params['Config']['prefixFolders']['value'] = 2 $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 - for ($i = 0; $i -lt $result.Count; $i = $i + 3) { # Test in threes + for ($i = 0; $i -lt $result.Count; $i = $i + 5) { # Test in fives $pageCfg1 = $result[$i] # First level page $pageCfg2 = $result[$i + 1] # Second level page preceded by a first level page $pageCfg3 = $result[$i + 2] # Third level page preceded by a second level page + $pageCfg4 = $result[$i + 3] # Second level page preceded by a third level page + $pageCfg5 = $result[$i + 4] # First level page preceded by a second level page # Test the first level page $pageCfg1['filePathRelUnderscore'] | Should -Be "$( $pageCfg1['nameCompat'] )" @@ -777,6 +835,18 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { Split-Path $pageCfg3['fullfilepathwithoutextension'] -Leaf | Should -Be "$( $pageCfg1['nameCompat'] )_$( $pageCfg2['nameCompat'] )_$( $pageCfg3['nameCompat'] )" Split-Path $pageCfg3['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg3['fullexportdirpath'] $pageCfg3['levelsPrefix']| Should -Be "$( '../' * ($pageCfg3['levelsFromRoot'] + 1 - 1) )" + + # Test the second level page preceded by a third level page + $pageCfg4['filePathRelUnderscore'] | Should -Be "$( $pageCfg1['nameCompat'] )_$( $pageCfg4['nameCompat'] )" + Split-Path $pageCfg4['fullfilepathwithoutextension'] -Leaf | Should -Be "$( $pageCfg1['nameCompat'] )_$( $pageCfg4['nameCompat'] )" + Split-Path $pageCfg4['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg4['fullexportdirpath'] + $pageCfg4['levelsPrefix']| Should -Be "$( '../' * ($pageCfg4['levelsFromRoot'] + 1 - 1) )" + + # Test the first level page preceded by a second level page + $pageCfg5['filePathRelUnderscore'] | Should -Be "$( $pageCfg5['nameCompat'] )" + Split-Path $pageCfg5['fullfilepathwithoutextension'] -Leaf | Should -Be "$( $pageCfg5['nameCompat'] )" + Split-Path $pageCfg5['fullfilepathwithoutextension'] -Parent | Should -Be $pageCfg5['fullexportdirpath'] + $pageCfg5['levelsPrefix']| Should -Be "$( '../' * ($pageCfg5['levelsFromRoot'] + 1 - 1) )" } } @@ -785,8 +855,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $pageCfg['mediaParentPath'] | Should -Be $pageCfg['notesBaseDirectory'] @@ -796,8 +866,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $pageCfg['mediaParentPath'] | Should -Be $pageCfg['fullexportdirpath'] @@ -809,8 +879,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = '' @@ -837,8 +907,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = '' @@ -862,8 +932,8 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' { $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = @" @@ -893,8 +963,8 @@ hello world $( [char]0x00A0 ) $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = @" @@ -928,8 +998,8 @@ hello world $( [char]0x00A0 ) $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = @" @@ -954,8 +1024,8 @@ foo\bar $result = @( New-SectionGroupConversionConfig @params 6>$null ) - # 9 pages from 'test' notebook, 9 pages from 'test2' notebook - $result.Count | Should -Be 18 + # 15 pages from 'test' notebook, 15 pages from 'test2' notebook + $result.Count | Should -Be 30 foreach ($pageCfg in $result) { $fakeMarkdownContent = @" diff --git a/ConvertOneNote2MarkDown-v2.ps1 b/ConvertOneNote2MarkDown-v2.ps1 index 0f8ac4f..0aabe53 100755 --- a/ConvertOneNote2MarkDown-v2.ps1 +++ b/ConvertOneNote2MarkDown-v2.ps1 @@ -571,28 +571,32 @@ Function New-SectionGroupConversionConfig { 6 { 'markdown_strict' } default { 'markdown' } } - $pageCfg['pagePrefix'] = switch ($pageCfg['pageLevel']) { - # process for subpage prefixes - 1 { + $pageCfg['pagePrefix'] = & { + if ($pageCfg['pageLevel'] -eq 1) { '' - } - 2 { - if ($previousPage) { - "$( $previousPage['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" - }else { - '' - } - } - 3 { + }else { if ($previousPage) { - "$( $previousPage['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" + if ($previousPage['pageLevel'] -lt $pageCfg['pageLevel']) { + # 1 -> 2, 1 -> 3, or 2 -> 3 + "$( $previousPage['filePathRel'] )$( [io.path]::DirectorySeparatorChar )" + }elseif ($previousPage['pageLevel'] -eq $pageCfg['pageLevel']) { + # 2 -> 2, or 3 -> 3 + "$( Split-Path $previousPage['filePathRel'] -Parent )$( [io.path]::DirectorySeparatorChar )" + }else { + # 3 -> 2 + $previousLevel1Page = @( + $sectionCfg['pages'] | Where-Object { $_['pageLevel'] } + ) + if ($previousLevel1Page.Count -gt 0) { + "$( Split-Path (Split-Path $previousPage['filePathRel'] -Parent) -Parent )$( [io.path]::DirectorySeparatorChar )" + }else { + '' + } + } }else { '' } } - default { - '' - } } $pageCfg['filePathRel'] = & { $filePathRel = "$( $pageCfg['pagePrefix'] )$( $pageCfg['nameCompat'] )"