Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ $prefixFolders = 1
$medialocation = 1
$conversion = 'markdown-simple_tables-multiline_tables-grid_tables+pipe_tables' # Default
$headerTimestampEnabled = 1
$imageAltText = 1
$keepspaces = 1
$keepescape = 1
$newlineCharacter = 1
Expand Down Expand Up @@ -87,6 +88,7 @@ $newlineCharacter = 1
$fakeUserInputs['medialocation']['value'] = ''
$fakeUserInputs['conversion']['value'] = ''
$fakeUserInputs['headerTimestampEnabled']['value'] = ''
$fakeUserInputs['imageAltText']['value'] = ''
$fakeUserInputs['keepspaces']['value'] = ''
$fakeUserInputs['keepescape']['value'] = ''
$fakeUserInputs['newlineCharacter']['value'] = ''
Expand Down Expand Up @@ -1191,6 +1193,54 @@ Describe 'New-SectionGroupConversionConfig' -Tag 'Unit' {
}
}

It "Should honor config imageAltText" {
$fakeMarkdownContent = @'
![](../media/p0.0-test-image1.png)

![some \[text\] with \\ and (brackets)](../media/p0.0-test-image2.png){width="5.1875in" height="6.83in"}text ![more text](../media/p0.0-test-image3.png)

[a link](https://example.com)
'@
$expectedMarkdownContentRemoved = @'
![](../media/p0.0-test-image1.png)

![](../media/p0.0-test-image2.png){width="5.1875in" height="6.83in"}text ![](../media/p0.0-test-image3.png)

[a link](https://example.com)
'@
$params['Config']['keepescape']['value'] = 3
$params['Config']['newlineCharacter']['value'] = 1

foreach ($imageAltText in 1, 2) {
$params['Config']['imageAltText']['value'] = $imageAltText

$result = @( New-SectionGroupConversionConfig @params 6>$null )

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

foreach ($pageCfg in $result) {
$content = $fakeMarkdownContent

# Mutate
foreach ($m in $pageCfg['mutations']) {
foreach ($r in $m['replacements']) {
$content = $content -replace $r['searchRegex'], $r['replacement']
}
}

# Strip the heading
$content = $content -replace '^# [^\n]*\n\nCreated: [^\n]*\n\nModified: [^\n]*\n\n---\n\n', ''

if ($imageAltText -eq 1) {
$content | Should -Be ($fakeMarkdownContent -replace '\r', '')
}else {
$content | Should -Be ($expectedMarkdownContentRemoved -replace '\r', '')
}
}
}
}

It "Should honor config keepspaces" {
$params['Config']['keepspaces']['value'] = 1

Expand Down
22 changes: 22 additions & 0 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ Default:
Whether to include page timestamp and separator at top of document
1: Include - Default
2: Don't include
'@
default = 1
value = 1
validateRange = 1,2
}
imageAltText = @{
description = @'
Whether to keep the alternative text of images (e.g. ![alt text](image.png)). For images in OneNote, this is usually text recognized in the image by OneNote, which can be very long
1: Keep - Default
2: Remove
'@
default = 1
value = 1
Expand Down Expand Up @@ -912,6 +922,18 @@ Function New-SectionGroupConversionConfig {
}
)
}
if ($config['imageAltText']['value'] -eq 2) {
@{
description = 'Remove image alternative text'
replacements = @(
@{
# E.g. '![some \[text\]](../media/somepage-image1.png)' -> '![](../media/somepage-image1.png)'. Pandoc escapes '\', '[', and ']' in alternative text, so this must happen before clearing '\' characters
searchRegex = '!\[(?:\\.|[^\\\]])*\]\('
replacement = '![]('
}
)
}
}
@{
description = 'Add heading'
replacements = @(
Expand Down
5 changes: 5 additions & 0 deletions config.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ $conversion = 'markdown-simple_tables-multiline_tables-grid_tables+pipe_tables'
# 2: Don't include
$headerTimestampEnabled = 1

# Whether to keep the alternative text of images (e.g. ![alt text](image.png)). For images in OneNote, this is usually text recognized in the image by OneNote, which can be very long
# 1: Keep - Default
# 2: Remove
$imageAltText = 1

# Whether to clear extra newlines between unordered (bullet) and ordered (numbered) list items, non-breaking spaces from blank lines, and `>` after unordered lists
# 1: Clear - Default
# 2: Don't clear
Expand Down