Skip to content

Instantly share code, notes, and snippets.

@markwragg
Created September 7, 2024 08:54
Show Gist options
  • Save markwragg/3d9686f87ff2362bc59b29c70c2140b0 to your computer and use it in GitHub Desktop.
Save markwragg/3d9686f87ff2362bc59b29c70c2140b0 to your computer and use it in GitHub Desktop.
A PowerShell cmdlet to make markdown files more readable in the console.
function Format-MarkDown {
[CmdletBinding()]
param (
# Parameter help description
[Parameter(ValueFromPipeline)]
[string[]]
$InputObject
)
begin {
$Color = 'gray'
}
process {
$Lines = $InputObject -split '`r`n'
ForEach ($Line in $Lines) {
if (-Join $Line[0..2] -eq '###') {
$Line = ($Line -Replace '^###','').Trim()
Write-Host $Line -ForegroundColor 'Blue'
}
elseif (-Join $Line[0..1] -eq '##') {
$Line = ($Line -Replace '^##','').Trim()
Write-Host $Line -ForegroundColor 'Cyan'
}
elseif ($Line[0] -eq '#') {
$Line = ($Line -Replace '^#','').Trim()
Write-Host $Line -ForegroundColor 'White'
}
else {
$Words = $Line -Split ' '
ForEach ($Word in $Words) {
if ($Word[0] -eq '`') {
$Color = 'yellow'
$Word = $Word -Replace '`', ''
}
Write-Host "$Word " -NoNewline -ForegroundColor $Color
$Color = 'gray'
}
Write-Host ''
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment