public/Read-HelpExample.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

function Read-HelpExample() {
    Param(
        [PsCustomObject] $Example
    )

    $code = $Example.Code 
    $remarks = $Example.Remarks 

    $nextCode = $code.TrimEnd("`n")
    
    $nextRemark = "";
    if($remarks) {

        # Powershell only allows one line examples.
        
        # PS C:\> can be used to extend example code blocks
        # where "PS C:\>" acts as a marker to instruct
        # the code below to be added to the code example

        foreach($line in $remarks.Split("`n")) {
            if($line.Trim().StartsWith("PS C:\>")) {
                
                $l = $line.SubString(7)
                $nextCode += "`n$l"
            } else {
                $nextRemark += "`n$line"
            }
        }
    }
    
    $Example.Code = (Write-EscapedMarkdownString $nextCode)
    $Example.Remark = (Write-EscapedMarkdownString $nextRemark)
    return $Example
}