Last active
July 16, 2024 00:30
-
-
Save trackd/cd3b6f791326c17ff12fdd91476f965c to your computer and use it in GitHub Desktop.
This file contains 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). | |
Where{ $_ -is [Markdig.Extensions.Tables.Table] }) { | |
$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 | |
} | |
[PSCustomObject]$item | |
} | |
} | |
Write-Output $table -NoEnumerate | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment