Public/ConvertTo-MarkdownList.ps1
Function ConvertTo-MarkdownList { <# .SYNOPSIS Converts strings to Markdown list items. .DESCRIPTION Converts strings to Markdown lists by inserting the proper syntax at the beginning of the string. .PARAMETER String The text to convert. .PARAMETER Level The level of indentation for list items. .PARAMETER Ordered Converts strings to an ordered list. .PARAMETER AsBlock Skips processing of carriage returns. By default, multi-line strings will be split and processed as separate items. .EXAMPLE ConvertTo-MarkdownList "Test1","Test2","Test3" .EXAMPLE ConvertTo-MarkdownList "Test1","Test2","Test3" -Level 2 -Ordered .EXAMPLE $Text = "Loremipsum Loremipsum Loremipsum Loremipsum Loremipsum Loremipsum " ConvertTo-MarkdownList $text,"Test1","Test2" -AsBlock .EXAMPLE $Text = "Loremipsum Loremipsum Loremipsum Loremipsum Loremipsum Loremipsum " ConvertTo-MarkdownList $text,"Test1","Test2" .EXAMPLE 1..5 | %{ConvertTo-MarkdownList "Test$_" -Level $_ -Ordered} #> [CmdletBinding()] PARAM ( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] [ValidateNotNullOrEmpty()] [String[]]$String, [Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$false)] [ValidateRange(1,10)] [Int]$Level = 1, [Parameter(Mandatory=$false)] [Switch]$Ordered, [Parameter(Mandatory=$false)] [Switch]$AsBlock ) #region BEGIN Block BEGIN { # Locally scope ErrorActionPreference for predictable behavior of Try/Catch blocks inside the function $ErrorActionPreference = 'Stop' # Create output variable $Results = [System.Collections.ArrayList]::new() } #endregion BEGIN Block #region PROCESS Block PROCESS { FOREACH ($Entry in $String) { # Change insertion syntax based on whether the list is ordinate $MarkdownString = SWITCH ($Ordered) { $true {"1. "} $false {"- "} } # Trim carriage returns and join $Entry = SWITCH ($AsBlock) { $true {$Entry.Split("`r`n").TrimEnd(" ") -join "<br>"} $false {$Entry.Split("`r`n").TrimEnd(" ")} } # Only make changes if the string doesn't begin with markdown syntax IF ($Entry -notmatch "^$MarkdownString") { # Insert Markdown syntax into $Entry based on $Level $Entry = $Entry | %{($_).Insert(0, $MarkdownString)} # Insert tabs if indent level greater than 1 is specified IF ($Level -gt 1) { $Entry = $Entry | %{($_).Insert(0, $("`t" * ($Level - 1)))} } } # Add to results $Results.Add($Entry) | Out-Null } } #endregion PROCESS Block #region END Block END { Return $Results } #endregion END Block } |