Public/ConvertTo-MarkdownCodeBlock.ps1

Function ConvertTo-MarkdownCodeBlock
{
    <#
        .SYNOPSIS
        Converts strings to Markdown code blocks.
 
        .DESCRIPTION
        Converts strings to Markdown code blocks by inserting the correct syntax at the beginning and end of the string value.
 
        .PARAMETER String
        The text to convert.
 
        .PARAMETER Language
        Enables optional syntax highlighting for the language specified.
 
        .PARAMETER Inline
        Creates an inline code block instead of a standalone block.
 
        .EXAMPLE
        "Hello World!" | ConvertTo-MarkdownCodeBlock -Inline
 
        .EXAMPLE
        @'
        Write-Host "Hello world!"
        Write-Host "More sample text!"
        '@ | ConvertTo-MarkdownCodeBlock -Language PowerShell
 
        .NOTES
        - The list of supported languages it not exhaustive, but was chosen for brevity. The full list of languages supported by GitHub's language interpreter may be found at https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml
 
    #>


    [CmdletBinding(DefaultParameterSetName="Language")]
    PARAM
    (
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [String[]]$String,

        [Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$false, ParameterSetName="Language")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet(
            "Batchfile",
            "C",
            "csharp",
            "cpp",
            "CSV",
            "DockerFile",
            "Go",
            "HCL",
            "HTML",
            "HTTP",
            "JSON",
            "JSON with Comments",
            "Jupyter Notebook",
            "Markdown",
            "PowerShell",
            "Python",
            "regex",
            "SQL",
            "Shell",
            "Text",
            "XML",
            "YAML"
        )]
        [String[]]$Language,

        [Parameter(Mandatory=$false, ParameterSetName="Inline")]
        [Switch]$Inline
    )

    #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 Inline switch is specified
            SWITCH ($Inline)
            {
                $true
                {
                    $MarkdownString = '`'
                    $CarriageReturn = $null
                }
                $false
                {
                    $MarkdownString = '```'
                    $CarriageReturn = "`r`n"
                }
            }

            # Only make changes if the string doesn't begin and end with markdown syntax
            IF ($Entry -notmatch "^$MarkdownString|$MarkdownString$")
            {
                # Insert Markdown syntax into $Entry
                $Entry = $Entry.Insert($($Entry.Length), $CarriageReturn+$MarkdownString).Insert(0, $MarkdownString+$CarriageReturn)

                # Insert language ID if specified
                IF($Language)
                {
                    $Entry = $Entry.Insert(3, $Language)
                }
            }

            # Add to results
            $Results.Add($Entry) | Out-Null
        }
    }
    #endregion PROCESS Block

    #region END Block
    END
    {
        Return $Results
    }
    #endregion END Block
}