functions/ConvertTo-Markdown.ps1


Function ConvertTo-Markdown {

[cmdletbinding()]
    [outputtype([string[]])]
    [alias('ctm')]

    Param(
        [Parameter(Position = 0, ValueFromPipeline)]
        [object]$Inputobject,
        [Parameter()]
        [string]$Title,
        [string[]]$PreContent,
        [string[]]$PostContent,
        [ValidateScript( {$_ -ge 10})]
        [int]$Width = 80
    )

    Begin {
        Write-Verbose "[BEGIN ] Starting $($myinvocation.MyCommand)"
        #initialize an array to hold incoming data
        $data = @()

        #initialize an empty here string for markdown text
        $Text = @"
 
"@

        If ($title) {
            Write-Verbose "[BEGIN ] Adding Title: $Title"
            $Text += "# $Title`n`n"
        }
        If ($precontent) {
            Write-Verbose "[BEGIN ] Adding Precontent"
            $Text += $precontent
            $text += "`n`n"
        }

    } #begin

    Process {
        #add incoming objects to data array
        Write-Verbose "[PROCESS] Adding processed object"
        $data += $Inputobject

    } #process
    End {
        #add the data to the text
        if ($data) {
            #convert data to strings and trim each line
            Write-Verbose "[END ] Converting data to strings"
            [string]$trimmed = (($data | Out-String -Width $width).split("`n")).ForEach( {"$($_.trimend())`n"})
            Write-Verbose "[END ] Adding to markdown"
            $text += @"
``````text
$($trimmed.trimend())
``````
 
"@

        }

        If ($postcontent) {
            Write-Verbose "[END ] Adding postcontent"
            $text += "`n"
            $text += $postcontent
        }
        #write the markdown to the pipeline
        $text
        Write-Verbose "[END ] Ending $($myinvocation.MyCommand)"
    } #end

} #close ConvertTo-Markdown