Skip to content

Commit

Permalink
Merge pull request #73 from theohbrothers/fix/fix-inserted-attachment…
Browse files Browse the repository at this point in the history
…-names-not-being-markdown-encoded-in-final-markdown

Fix: Fix inserted attachment names not being markdown-encoded in final markdown
  • Loading branch information
leojonathanoh committed Aug 10, 2021
2 parents afdb0dc + 1b42b86 commit cc20c68
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
48 changes: 44 additions & 4 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ Describe "Remove-InvalidFileNameChars" -Tag 'Unit' {
$fileName = 'foo'

{ $fileName | Remove-InvalidFileNameChars } | Should -Not -Throw

}

}
Expand Down Expand Up @@ -289,7 +288,6 @@ Describe "Remove-InvalidFileNameCharsInsertedFiles" -Tag 'Unit' {
$fileName = 'foo'

{ $fileName | Remove-InvalidFileNameCharsInsertedFiles } | Should -Not -Throw

}

}
Expand Down Expand Up @@ -336,6 +334,34 @@ Describe "Remove-InvalidFileNameCharsInsertedFiles" -Tag 'Unit' {

}


Describe "Encode-Markdown" -Tag 'Unit' {

Context 'Parameters' {

It "Accept pipeline input" {
$content = 'foo'

{ $content | Encode-Markdown } | Should -Not -Throw
}

}

Context 'Behavior' {

It "Should encode given content in markdown" {
$content = '\*_{}[]()#+-.!`'
$expectedContent = '\\\*\_\{\}\[\]\(\)\#\+\-\.\!```'

$result = Encode-Markdown -Name $content

$result | Should -Be $expectedContent
}

}

}

Describe "New-OneNoteConnection" -Tag 'Unit' {

Context 'Behavior' {
Expand Down Expand Up @@ -636,10 +662,10 @@ AAAAAElFTkSuQmCC</one:Data>
<one:T selected="all"><![CDATA[Attachments:]]></one:T>
</one:OE>
<one:OE creationTime="2021-08-06T16:08:25.000Z" lastModifiedTime="2021-08-06T16:08:25.000Z" objectID="{902AD630-91B7-4D8B-96A7-107572724072}{49}{B0}" selected="all" alignment="left">
<one:InsertedFile selected="all" pathCache="C:\Users\LeonardJonathan\AppData\Local\Microsoft\OneNote\16.0\cache\00001BNS.bin" pathSource="C:\Users\LeonardJonathan\Desktop\attachment1.txt" preferredName="attachment1.txt" />
<one:InsertedFile selected="all" pathCache="C:\Users\LeonardJonathan\AppData\Local\Microsoft\OneNote\16.0\cache\00001BNS.bin" pathSource="C:\Users\LeonardJonathan\Desktop\attachment1(something in brackets).txt" preferredName="attachment1(something in brackets).txt" />
</one:OE>
<one:OE creationTime="2021-08-06T16:08:10.000Z" lastModifiedTime="2021-08-06T16:08:10.000Z" objectID="{902AD630-91B7-4D8B-96A7-107572724072}{41}{B0}" selected="all" alignment="left">
<one:InsertedFile selected="all" pathCache="C:\Users\LeonardJonathan\AppData\Local\Microsoft\OneNote\16.0\cache\00001BNS.bin" pathSource="C:\Users\LeonardJonathan\Desktop\attachment2.txt" preferredName="attachment2.txt" />
<one:InsertedFile selected="all" pathCache="C:\Users\LeonardJonathan\AppData\Local\Microsoft\OneNote\16.0\cache\00001BNS.bin" pathSource="C:\Users\LeonardJonathan\Desktop\attachment2[something in brackets].txt" preferredName="attachment2(something in brackets).txt" />
</one:OE>
</one:OEChildren>
</one:Outline>
Expand Down Expand Up @@ -714,6 +740,20 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' {
}
}

It "Should determine attachment references correctly" {
$result = @( New-SectionGroupConversionConfig @params 6>$null )

# 15 pages from 'test' notebook, 15 pages from 'test2' notebook
$result.Count | Should -Be 30

foreach ($pageCfg in $result) {
$pageCfg['insertedAttachments'].Count | Should -Be 2

$pageCfg['insertedAttachments'][0]['markdownFileName'] | Should -Be 'attachment1\(something\-in\-brackets\)\.txt'
$pageCfg['insertedAttachments'][1]['markdownFileName'] | Should -Be 'attachment2\(something\-in\-brackets\)\.txt'
}
}

It "Should generate fully unique docx file for each page, even for identically-named pages in a section" {
$result = @( New-SectionGroupConversionConfig @params 6>$null )

Expand Down
20 changes: 19 additions & 1 deletion ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,24 @@ Function Remove-InvalidFileNameCharsInsertedFiles {
return $newName
}

Function Encode-Markdown {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,Position = 0,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[string]$Name
)

$markdownChars = '\*_{}[]()#+-.!'.ToCharArray()
foreach ($c in $markdownChars) {
$Name = $Name.Replace("$c", "\$c")
}
$markdownChars2 = '`'
foreach ($c in $markdownChars2) {
$Name = $Name.Replace("$c", "$c$c$c")
}
$Name
}
Function New-OneNoteConnection {
[CmdletBinding()]
param ()
Expand Down Expand Up @@ -674,7 +692,7 @@ Function New-SectionGroupConversionConfig {
$attachmentCfg = [ordered]@{}
$attachmentCfg['object'] = $i
$attachmentCfg['nameCompat'] = $i.preferredName | Remove-InvalidFileNameCharsInsertedFiles
$attachmentCfg['markdownFileName'] = $attachmentCfg['nameCompat'].Replace("$", "\$").Replace("^", "\^").Replace("'", "\'")
$attachmentCfg['markdownFileName'] = $attachmentCfg['nameCompat'] | Encode-Markdown
$attachmentCfg['source'] = $i.pathCache
$attachmentCfg['destination'] = [io.path]::combine( $pageCfg['mediaPath'], $attachmentCfg['nameCompat'] )

Expand Down

0 comments on commit cc20c68

Please sign in to comment.