Last active
November 26, 2024 19:26
-
-
Save trackd/4f1f1727a53c9d6966257f0b33eabb43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertFrom-MarkdownTable { | |
<# | |
.DESCRIPTION | |
Converts a markdown table to a PowerShell object. | |
Supports multiple tables in a single markdown document. | |
Each table is output as an array. | |
.EXAMPLE | |
@' | |
# Fun markdown tables | |
## Some Commands | |
| CommandType | Name | Version | Source | | |
|-------------|---------------|---------|---------------------------------| | |
| Cmdlet | Add-Content | 7.0.0.0 | Microsoft.PowerShell.Management | | |
| Cmdlet | Add-History | 7.5.0.3 | Microsoft.PowerShell.Core | | |
| Cmdlet | Add-Member | 7.0.0.0 | Microsoft.PowerShell.Utility | | |
| Cmdlet | Add-Type | 7.0.0.0 | Microsoft.PowerShell.Utility | | |
| Cmdlet | Clear-Content | 7.0.0.0 | Microsoft.PowerShell.Management | | |
## Example Modules | |
| ModuleType | Version | Name | | |
|------------|---------|---------------------------------------| | |
| Binary | 0.7.7 | Microsoft.PowerShell.ConsoleGuiTools | | |
| Manifest | 0.3.0 | Microsoft.PowerShell.PSAdapter | | |
| Binary | 1.0.5 | Microsoft.PowerShell.PSResourceGet | | |
| Binary | 1.1.2 | Microsoft.PowerShell.SecretManagement | | |
| Binary | 1.0.6 | Microsoft.PowerShell.SecretStore | | |
'@ | ConvertFrom-MarkdownTable | % { $_ | ft } | |
generated with: | |
https://gist.github.com/trackd/2c670e3c1a127ab32d885a68168c06a9 | |
Get-Command -ListImported -Module *microsoft* | select -First 5 | ConvertTo-MarkdownTable -Autosize | |
Get-Module -ListAvailable *microsoft* | select -First 5 -Property ModuleType, Version, Name | ConvertTo-MarkdownTable -Autosize | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[ValidateNotNullOrEmpty()] | |
[string[]] $InputObject | |
) | |
begin { | |
$options = [Markdig.MarkdownExtensions]::UseAdvancedExtensions( | |
[Markdig.MarkdownPipelineBuilder]::new()).Build() | |
} | |
process { | |
foreach ($MarkdownTable in [Markdig.Markdown]::Parse($InputObject, $options)) { | |
if ($MarkdownTable -isnot [Markdig.Extensions.Tables.Table]) { | |
continue | |
} | |
$table = foreach ($row in $MarkdownTable) { | |
if ($row.IsHeader) { | |
$header = @($row.Inline.Content) | |
} | |
else { | |
$item = [ordered]@{} | |
for ($i = 0; $i -lt $row.Count; $i++) { | |
$item[$header[$i]] = $row[$i].Inline.Content.ToString() | |
} | |
[PSCustomObject]$item | |
} | |
} | |
Write-Output $table -NoEnumerate | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment