From 5be386133d08e56bb56e9fc8fc3337bca36c51ba Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 13 Oct 2026 17:34:59 +0200 Subject: [PATCH] Fix: Fix blank lines being added at the end of the markdown file for each image on the page Each image reference is renamed by reading the markdown file and writing it back with `Set-ContentNoBom`, which appends a newline. So a page with e.g. 27 images (pen/hand written drawings are exported as one image per stroke) ended with 27 blank lines. Now trailing blank lines are removed before writing the final markdown file, which still ends with a single newline. Co-Authored-By: Claude Opus 5.5 --- ConvertOneNote2MarkDown-v2.Tests.ps1 | 11 +++++++++++ ConvertOneNote2MarkDown-v2.ps1 | 2 ++ 2 files changed, 13 insertions(+) diff --git a/ConvertOneNote2MarkDown-v2.Tests.ps1 b/ConvertOneNote2MarkDown-v2.Tests.ps1 index 72d1ae7..f5c323f 100644 --- a/ConvertOneNote2MarkDown-v2.Tests.ps1 +++ b/ConvertOneNote2MarkDown-v2.Tests.ps1 @@ -1792,6 +1792,17 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { Assert-MockCalled -CommandName Set-ContentNoBom -ParameterFilter { $LiteralPath } -Times 1 -Scope It } + It "Markdown mutation: Removes trailing blank lines" { + Mock Get-Content -ParameterFilter { $LiteralPath -and ! $Raw } { + # 6 lines of header, followed by the content with a newline for each renamed image + @( 1..6 | ForEach-Object { 'some header' } ) + @( 'some text', '', '', '' ) + } + + Convert-OneNotePage @params 6>$null + + Assert-MockCalled -CommandName Set-ContentNoBom -ParameterFilter { $LiteralPath -and $Value -match 'some text\z' } -Times 1 -Scope It + } + It "Does a dry run" { Mock New-Item { 'foo' } Mock Remove-Item { 'foo' } diff --git a/ConvertOneNote2MarkDown-v2.ps1 b/ConvertOneNote2MarkDown-v2.ps1 index 3668cba..9709897 100644 --- a/ConvertOneNote2MarkDown-v2.ps1 +++ b/ConvertOneNote2MarkDown-v2.ps1 @@ -1282,6 +1282,8 @@ Function Convert-OneNotePage { '' } ) -join "`n" + # Remove trailing blank lines. Each write of the markdown file (e.g. when renaming image references, once for each image) appends a newline. A single newline is appended again when writing the final file + $content = $content -replace '[\r\n \t]+\z', '' } # Mutate