Types/OpenPackage.View/Help.md.ps1
|
<# .SYNOPSIS Views Help as Markdown .DESCRIPTION Views PowerShell Command as markdown .INPUTS Management.Automation.CommandInfo #> param() $allInputAndArgs = @($input) + $args foreach ($in in $allInputAndArgs) { if ($in -isnot [Management.Automation.CommandInfo]) { continue } $help = $null if ($in -is [Management.Automation.ExternalScriptInfo]) { $help = Get-Help $in.Source } elseif ( $in -is [Management.Automation.FunctionInfo] -or $in -is [Management.Automation.AliasInfo] -or $in -is [Management.Automation.CmdletInfo] ) { $help = Get-Help $in.Name } if (-not $help) { Write-Warning "Applications may not have help" continue } # If the help is a string, if ($help -is [string]) { # make it preformatted text "~~~" "$export" "~~~" } else { # Otherwise, add list the export "### $($export)" # And make it's synopsis a header "#### $($help.SYNOPSIS)" # put the description below that "$($help.Description.text -join [Environment]::NewLine)" # Show our examples "##### Examples" $exampleNumber = 0 foreach ($example in $help.examples.example) { $markdownLines = @() $exampleNumber++ $nonCommentLine = $false "###### Example $exampleNumber" # Combine the code and remarks $exampleLines = @( $example.Code foreach ($remark in $example.Remarks.text) { if (-not $remark) { continue } $remark } ) -join ([Environment]::NewLine) -split '(?>\r\n|\n)' # and split into lines # Go thru each line in the example as part of a loop $codeBlock = @(foreach ($exampleLine in $exampleLines) { # Any comments until the first uncommentedLine are markdown if ($exampleLine -match '^\#' -and -not $nonCommentLine) { $markdownLines += $exampleLine -replace '^\#\s{0,1}' } else { $nonCommentLine = $true $exampleLine } }) -join [Environment]::NewLine $markdownLines "~~~PowerShell" $CodeBlock "~~~" } $relatedUris = foreach ($link in $help.relatedLinks.navigationLink) { if ($link.uri) { $link.uri } } if ($relatedUris) { "#### Links" foreach ($related in $relatedUris) { "* [$related]($related)" } } # Make a table of parameters if ($help.parameters.parameter) { "##### Parameters" "" "|Name|Type|Description|" "|-|-|-|" foreach ($parameter in $help.Parameters.Parameter) { "|$($parameter.Name)|$($parameter.type.name)|$( $parameter.description.text -replace '(?>\r\n|\n)', '<br/>' )|" } "" } } } |