Skip to content

Commit

Permalink
Merge pull request #79 from theohbrothers/enhancement/write-.md-files…
Browse files Browse the repository at this point in the history
…-in-utf-8-without-bom-for-powershell-5

Enhancement: Write .md files in UTF-8 without BOM for Powershell <= 5
  • Loading branch information
leojonathanoh committed Aug 11, 2021
2 parents d4af8f9 + 8e01aed commit 3c28784
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
9 changes: 5 additions & 4 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand All @@ -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" {
Expand All @@ -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

Expand Down
36 changes: 34 additions & 2 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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 )"
Expand Down Expand Up @@ -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 )"
Expand Down

0 comments on commit 3c28784

Please sign in to comment.