Repository navigation
Expand file tree
/
Copy pathMarkDownHelper.psm1
More file actions
78 lines (68 loc) · 2.56 KB
/
Copy pathMarkDownHelper.psm1
File metadata and controls
78 lines (68 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<#
.SYNOPSIS
Helper functions for creating markdown tables.
.PARAMETER Headers
An array of strings representing the headers of the table.
Each header should be in the format "label;alignment", where alighment can be "left", "l", "right", "r", or "center", "c".
.PARAMETER Rows
An array of arrays representing the rows of the table. Each row should have the same number of elements as there are headers.
.OUTPUTS
A string representing the markdown table, with '\n' at the end of each line.
.EXAMPLE
$headers = @("Name;left", "Age;center", "Location;right")
$rows = @(
@("Alice", 30, "New York"),
@("Bob", 25, "Los Angeles")
)
$markdownTable = BuildMarkdownTable -Headers $headers -Rows $rows
#>
function Build-MarkdownTable {
param (
[Parameter(Mandatory = $true)]
[string[]] $Headers,
[Parameter(Mandatory = $true)]
[string[][]] $Rows
)
$tableSb = [System.Text.StringBuilder]::new()
$headerRow = '|'
$separatorRow = '|'
$columnCount = $Headers.Length
foreach ($header in $Headers) {
$headerParts = $header -split ";"
$headerRow += "$($headerParts[0])|"
if ($headerParts.Length -eq 2) {
switch ($headerParts[1].ToLower()) {
{$_ -in @('l','left')} {
$separatorRow += ":---|"
}
{$_ -in @('r','right')} {
$separatorRow += "---:|"
}
{$_ -in @('c','center')} {
$separatorRow += ":---:|"
}
default {
Write-Host "Invalid alignment: ($_), should be 'left', 'right' or 'center'. Defaulting to 'left'."
$separatorRow += ":---|"
}
}
}
else {
Write-Host "Invalid header format: ($header), should be 'label;alignment'. Defaulting to 'left'."
$separatorRow += ":---|"
}
}
$tableSb.Append("$headerRow\n") | Out-Null
$tableSb.Append("$separatorRow\n") | Out-Null
foreach ($row in $Rows) {
if ($row.Length -ne $columnCount) {
throw "Row '$($row -join ';')' does not have the same number of columns ($($row.Length)) as the headers ($columnCount)"
}
$rowString = $row -join "|"
$rowString = "|$rowString|"
$tableSb.Append("$rowString\n") | Out-Null
}
$tableSb.Append("\n") | Out-Null
return $tableSb.ToString()
}
Export-ModuleMember -Function *-*