forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-Markdown.ps1
More file actions
102 lines (86 loc) · 2.83 KB
/
ConvertTo-Markdown.ps1
File metadata and controls
102 lines (86 loc) · 2.83 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Function ConvertTo-Markdown {
[cmdletbinding()]
[outputtype([string[]])]
[alias('ctm')]
Param(
[Parameter(Position = 0, ValueFromPipeline)]
[object]$Inputobject,
[Parameter()]
[string]$Title,
[string[]]$PreContent,
[string[]]$PostContent,
[ValidateScript( {$_ -ge 10})]
[int]$Width = 80,
#display results as a markdown table
[switch]$AsTable
)
Begin {
Write-Verbose "[BEGIN ] Starting $($myinvocation.MyCommand)"
#initialize an array to hold incoming data
$data = @()
#initialize an empty here string for markdown text
$Text = @"
"@
If ($title) {
Write-Verbose "[BEGIN ] Adding Title: $Title"
$Text += "# $Title`n`n"
}
If ($precontent) {
Write-Verbose "[BEGIN ] Adding Precontent"
$Text += $precontent
$text += "`n`n"
}
} #begin
Process {
#add incoming objects to data array
Write-Verbose "[PROCESS] Adding processed object"
$data += $Inputobject
} #process
End {
#add the data to the text
if ($data) {
if ($AsTable) {
Write-Verbose "[END ] Formatting as a table"
$names = $data[0].psobject.Properties.name
$head = "| $($names -join " | ") |"
$text += $head
$text += "`n"
$bars = "| $(($names -replace '.','-') -join " | ") |"
$text += $bars
$text += "`n"
foreach ($item in $data) {
$line = "| "
$values = @()
for ($i = 0; $i -lt $names.count; $i++) {
# $line += $item.($names[$i])
$values += $item.($names[$i])
}
$line += $values -join " | "
$line += " |"
$text += $line
$text += "`n"
}
}
else {
#convert data to strings and trim each line
Write-Verbose "[END ] Converting data to strings"
[string]$trimmed = (($data | Out-String -Width $width).split("`n")).ForEach( { "$($_.trim())`n" })
Write-Verbose "[END ] Adding to markdown"
$clean = $($trimmed.trimend())
$text += @"
``````text
$clean
``````
"@
} #else as text
} #if $data
If ($postcontent) {
Write-Verbose "[END ] Adding postcontent"
$text += "`n"
$text += $postcontent
}
#write the markdown to the pipeline
$text.TrimEnd()
Write-Verbose "[END ] Ending $($myinvocation.MyCommand)"
} #end
} #close ConvertTo-Markdown