diff --git a/ConvertOneNote2MarkDown-v2.Tests.ps1 b/ConvertOneNote2MarkDown-v2.Tests.ps1 index ff746f6..113168f 100644 --- a/ConvertOneNote2MarkDown-v2.Tests.ps1 +++ b/ConvertOneNote2MarkDown-v2.Tests.ps1 @@ -1325,7 +1325,8 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { Mock Get-Content { '' } - Mock Set-Content {} + function Set-ContentNoBom {} + Mock Set-ContentNoBom {} $params = @{ OneNoteConnection = 'some connection' Config = Get-DefaultConfiguration @@ -1440,7 +1441,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { Convert-OneNotePage @params 6>$null Assert-MockCalled -CommandName Get-Content -Times 1 - Assert-MockCalled -CommandName Set-Content -Times 1 + Assert-MockCalled -CommandName Set-ContentNoBom -Times 1 } It "Does not halt conversion if renaming image(s) references in markdown fails" { @@ -1455,7 +1456,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { Convert-OneNotePage @params 6>$null Assert-MockCalled -CommandName Get-Content -Times 1 - Assert-MockCalled -CommandName Set-Content -Times 1 + Assert-MockCalled -CommandName Set-ContentNoBom -Times 1 } It "Does a dry run" { @@ -1465,7 +1466,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { Mock Start-Process { 'foo' } Mock Move-Item { 'foo' } Mock Get-Content { 'foo' } - Mock Set-Content { 'foo' } + Mock Set-ContentNoBom { 'foo' } $params['Config']['DryRun']['value'] = 2 diff --git a/ConvertOneNote2MarkDown-v2.ps1 b/ConvertOneNote2MarkDown-v2.ps1 index bf13cf9..3441663 100755 --- a/ConvertOneNote2MarkDown-v2.ps1 +++ b/ConvertOneNote2MarkDown-v2.ps1 @@ -407,6 +407,38 @@ Function Encode-Markdown { } $Name } + +Function Set-ContentNoBom { + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true,Position = 0,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)] + [AllowEmptyString()] + [string] + $Path + , + [Parameter(Mandatory = $true)] + [AllowEmptyString()] + [array] + $Value + ) + process { + if ($PSVersionTable.PSVersion.Major -le 5) { + try { + $content = $Value -join '' + [IO.File]::WriteAllLines($Path, $content) + }catch { + if ($ErrorActionPreference -eq 'Stop') { + throw + }else { + Write-Error -ErrorRecord $_ + } + } + }else { + Set-Content @PSBoundParameters + } + } +} + Function New-OneNoteConnection { [CmdletBinding()] param () @@ -1021,7 +1053,7 @@ Function Convert-OneNotePage { if ($config['dryRun']['value'] -eq 1) { $content = Get-Content -Path "$( $pageCfg['fullfilepathwithoutextension'] ).md" -Raw -ErrorAction Stop # Get-Content -ErrorAction Stop can produce random "Cannot find path 'xxx' because it does not exist" $content = $content.Replace("$($image.Name)", "$($newimageName)") - Set-Content -Path "$( $pageCfg['fullfilepathwithoutextension'] ).md" -Value $content -ErrorAction Stop + Set-ContentNoBom -Path "$( $pageCfg['fullfilepathwithoutextension'] ).md" -Value $content -ErrorAction Stop } }catch { Write-Error "Error while renaming image file name references to '$( $newimageName ): $( $_.Exception.Message )" @@ -1059,7 +1091,7 @@ Function Convert-OneNotePage { } } if ($config['dryRun']['value'] -eq 1) { - Set-Content "$( $pageCfg['fullfilepathwithoutextension'] ).md" -Value $content -ErrorAction Stop + Set-ContentNoBom "$( $pageCfg['fullfilepathwithoutextension'] ).md" -Value $content -ErrorAction Stop } }catch { Write-Error "Error while mutating markdown content: $( $_.Exception.Message )"