Skip to content

Commit c6570a1

Browse files
authored
Merge pull request #142 from dfinke/update-convert-markdown
handle trimming headers
2 parents 401ebf2 + c4cd342 commit c6570a1

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

Public/ConvertFrom-GPTMarkdownTable.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ function ConvertFrom-GPTMarkdownTable {
2525

2626
$lines = $markdown.Trim() -split "`n"
2727

28-
$(
29-
foreach ($line in $lines) {
30-
if ($line -match '[A-Za-z0-9]') {
31-
$line.Trim() -replace "^\|", ""
32-
}
28+
$data = foreach ($line in $lines) {
29+
if ($line -match '[A-Za-z0-9]') {
30+
$line.Trim() -replace "^\|", "" -replace "\| ", "|" -replace " \|", "|"
3331
}
34-
) | ConvertFrom-Csv -Delimiter '|'
32+
}
33+
34+
$data | ConvertFrom-Csv -Delimiter '|'
3535
}
3636
}

__tests__/ConvertFrom-GPTMarkdownTable.tests.ps1

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
Import-Module "$PSScriptRoot\..\PowerShellAI.psd1" -Force
22

3-
Describe "ConvertFrom-GPTMarkdownTable" {
3+
Describe "ConvertFrom-GPTMarkdownTable" -Tag GPTMarkdownTable {
44
It "ConvertFrom-GPTMarkdownTable" {
55
$markdown = @"
66
| p1 | p2 | p3 |
77
| --- | --- | --- |
88
| 1 | 2 | 3 |
99
| 4 | 5 | 6 |
1010
"@
11-
$actual = ConvertFrom-GPTMarkdownTable $markdown
11+
$actual = ConvertFrom-GPTMarkdownTable $markdown.Trim()
1212

1313
$actual | Should -Not -BeNullOrEmpty
1414

1515
$actual.Count | Should -Be 2
1616
$names = $actual[0].psobject.Properties.Name
1717

1818
$names.Count | Should -Be 3
19-
$names[0] | Should -Be 'p1 '
20-
$names[1] | Should -Be 'p2 '
21-
$names[2] | Should -Be 'p3 '
19+
$names[0] | Should -Be 'p1'
20+
$names[1] | Should -Be 'p2'
21+
$names[2] | Should -Be 'p3'
22+
2223

23-
$actual[0].'p1 ' | Should -Be 1
24-
$actual[0].'p2 ' | Should -Be 2
25-
$actual[0].'p3 ' | Should -Be 3
24+
$actual[0].'p1' | Should -Be 1
25+
$actual[0].'p2' | Should -Be 2
26+
$actual[0].'p3' | Should -Be 3
2627

27-
$actual[1].'p1 ' | Should -Be 4
28-
$actual[1].'p2 ' | Should -Be 5
29-
$actual[1].'p3 ' | Should -Be 6
28+
$actual[1].'p1' | Should -Be 4
29+
$actual[1].'p2' | Should -Be 5
30+
$actual[1].'p3' | Should -Be 6
3031

3132
}
3233

@@ -50,22 +51,22 @@ Celery | 16 | 0 | 3 | 0
5051
$names = $actual[0].psobject.Properties.Name
5152

5253
$names.Count | Should -Be 5
53-
$names[0] | Should -Be 'Vegetable '
54-
$names[1] | Should -Be 'Calories '
55-
$names[2] | Should -Be 'Protein (g) '
56-
$names[3] | Should -Be 'Carbs (g) '
54+
$names[0] | Should -Be 'Vegetable'
55+
$names[1] | Should -Be 'Calories'
56+
$names[2] | Should -Be 'Protein (g)'
57+
$names[3] | Should -Be 'Carbs (g)'
5758
$names[4] | Should -Be 'Fat (g)'
5859

59-
$actual[0].'Vegetable ' | Should -Be 'Carrot '
60-
$actual[0].'Calories ' | Should -Be 41
61-
$actual[0].'Protein (g) ' | Should -Be 1
62-
$actual[0].'Carbs (g) ' | Should -Be 9
60+
$actual[0].'Vegetable' | Should -Be 'Carrot'
61+
$actual[0].'Calories' | Should -Be 41
62+
$actual[0].'Protein (g)' | Should -Be 1
63+
$actual[0].'Carbs (g)' | Should -Be 9
6364
$actual[0].'Fat (g)' | Should -Be 0
6465

65-
$actual[-1].'Vegetable ' | Should -Be 'Celery '
66-
$actual[-1].'Calories ' | Should -Be 16
67-
$actual[-1].'Protein (g) ' | Should -Be 0
68-
$actual[-1].'Carbs (g) ' | Should -Be 3
66+
$actual[-1].'Vegetable' | Should -Be 'Celery'
67+
$actual[-1].'Calories' | Should -Be 16
68+
$actual[-1].'Protein (g)' | Should -Be 0
69+
$actual[-1].'Carbs (g)' | Should -Be 3
6970
$actual[-1].'Fat (g)' | Should -Be 0
7071
}
7172

@@ -88,17 +89,17 @@ Celery | 16 | 0 | 3 | 0
8889
$names = $actual[0].psobject.Properties.Name
8990

9091
$names.Count | Should -Be 3
91-
$names[0] | Should -Be 'President '
92-
$names[1] | Should -Be 'Term '
93-
$names[2] | Should -Be 'Vice President '
92+
$names[0] | Should -Be 'President'
93+
$names[1] | Should -Be 'Term'
94+
$names[2] | Should -Be 'Vice President'
9495

95-
$actual[0].'President ' | Should -Be 'George Washington '
96-
$actual[0].'Term ' | Should -Be '1789-1797 '
97-
$actual[0].'Vice President ' | Should -Be 'John Adams '
96+
$actual[0].'President' | Should -Be 'George Washington'
97+
$actual[0].'Term' | Should -Be '1789-1797'
98+
$actual[0].'Vice President' | Should -Be 'John Adams'
9899

99-
$actual[-1].'President ' | Should -Be 'James Monroe '
100-
$actual[-1].'Term ' | Should -Be '1817-1825 '
101-
$actual[-1].'Vice President ' | Should -Be 'Daniel D. Tompkins '
100+
$actual[-1].'President' | Should -Be 'James Monroe'
101+
$actual[-1].'Term' | Should -Be '1817-1825'
102+
$actual[-1].'Vice President' | Should -Be 'Daniel D. Tompkins'
102103
}
103104

104105
It "ConvertFrom-GPTMarkdownTable - with whitespace" {
@@ -118,16 +119,16 @@ Celery | 16 | 0 | 3 | 0
118119

119120
$names = $actual[0].psobject.Properties.Name
120121
$names.Count | Should -Be 3
121-
$names[0] | Should -Be 'Column 1 '
122-
$names[1] | Should -Be 'Column 2 '
123-
$names[2] | Should -Be 'Column 3 '
122+
$names[0] | Should -Be 'Column 1'
123+
$names[1] | Should -Be 'Column 2'
124+
$names[2] | Should -Be 'Column 3'
124125

125-
$actual[0].'Column 1 ' | Should -Be 'Cell 1 '
126-
$actual[0].'Column 2 ' | Should -Be 'Cell 2 '
127-
$actual[0].'Column 3 ' | Should -Be 'Cell 3 '
126+
$actual[0].'Column 1' | Should -Be 'Cell 1 '
127+
$actual[0].'Column 2' | Should -Be 'Cell 2 '
128+
$actual[0].'Column 3' | Should -Be 'Cell 3 '
128129

129-
$actual[-1].'Column 1 ' | Should -Be 'Cell 7 '
130-
$actual[-1].'Column 2 ' | Should -Be 'Cell 8 '
131-
$actual[-1].'Column 3 ' | Should -Be 'Cell 9 '
130+
$actual[-1].'Column 1' | Should -Be 'Cell 7 '
131+
$actual[-1].'Column 2' | Should -Be 'Cell 8 '
132+
$actual[-1].'Column 3' | Should -Be 'Cell 9 '
132133
}
133134
}

0 commit comments

Comments
 (0)