|
| 1 | +function Format-Yaml { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Normalizes a YAML stream without projecting it to PowerShell values. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Parses YAML into the module's representation graph and emits one |
| 8 | + deterministic YAML string directly from those nodes. Document order, |
| 9 | + empty documents, effective tags, anchors, aliases, complex keys, node |
| 10 | + kinds, scalar content, and mapping order are preserved. |
| 11 | +
|
| 12 | + Comments, directives, flow styles, scalar presentation styles, document |
| 13 | + end markers, and source anchor names are normalized. Every document has |
| 14 | + an explicit start marker. Output uses LF and has no final newline. |
| 15 | +
|
| 16 | + Pipeline strings are joined with a line feed and parsed as one stream, |
| 17 | + matching ConvertFrom-Yaml. |
| 18 | +
|
| 19 | + .PARAMETER InputObject |
| 20 | + YAML text. Multiple pipeline records are joined with a line feed and |
| 21 | + parsed as one YAML stream. |
| 22 | +
|
| 23 | + .PARAMETER Indent |
| 24 | + Block indentation from 2 through 9 spaces. The default is 2. |
| 25 | +
|
| 26 | + .PARAMETER Depth |
| 27 | + Maximum YAML node nesting depth. The default is 100. |
| 28 | +
|
| 29 | + .PARAMETER MaxNodes |
| 30 | + Maximum number of YAML nodes in the stream. The default is 100000. |
| 31 | +
|
| 32 | + .PARAMETER MaxAliases |
| 33 | + Maximum number of alias nodes in the stream. The default is 1000. |
| 34 | +
|
| 35 | + .PARAMETER MaxScalarLength |
| 36 | + Maximum decoded character count for one scalar. The default is |
| 37 | + 1048576. |
| 38 | +
|
| 39 | + .PARAMETER MaxTagLength |
| 40 | + Maximum expanded character count for one tag. The default is 1024. |
| 41 | +
|
| 42 | + .PARAMETER MaxTotalTagLength |
| 43 | + Maximum cumulative expanded tag characters. The default is 65536. |
| 44 | +
|
| 45 | + .PARAMETER MaxNumericLength |
| 46 | + Maximum digits in an implicitly or explicitly typed number. The |
| 47 | + default is 4096. |
| 48 | +
|
| 49 | + .EXAMPLE |
| 50 | + Get-Content -Path '.\config.yaml' | Format-Yaml |
| 51 | +
|
| 52 | + Joins the input lines and emits one normalized YAML stream. |
| 53 | +
|
| 54 | + .EXAMPLE |
| 55 | + $normalized = Format-Yaml -InputObject '{name: Ada, active: true}' -Indent 4 |
| 56 | +
|
| 57 | + Converts flow presentation to deterministic block presentation with |
| 58 | + four-space indentation. |
| 59 | +
|
| 60 | + .INPUTS |
| 61 | + System.String[] |
| 62 | +
|
| 63 | + .OUTPUTS |
| 64 | + System.String |
| 65 | + #> |
| 66 | + [CmdletBinding()] |
| 67 | + [OutputType([string])] |
| 68 | + param ( |
| 69 | + [Parameter(Mandatory, Position = 0, ValueFromPipeline)] |
| 70 | + [AllowEmptyString()] |
| 71 | + [string[]] $InputObject, |
| 72 | + |
| 73 | + [ValidateRange(2, 9)] |
| 74 | + [int] $Indent = 2, |
| 75 | + |
| 76 | + [ValidateRange(1, 128)] |
| 77 | + [int] $Depth = 100, |
| 78 | + |
| 79 | + [ValidateRange(1, 2147483647)] |
| 80 | + [int] $MaxNodes = 100000, |
| 81 | + |
| 82 | + [ValidateRange(0, 2147483647)] |
| 83 | + [int] $MaxAliases = 1000, |
| 84 | + |
| 85 | + [ValidateRange(1, 2147483647)] |
| 86 | + [int] $MaxScalarLength = 1048576, |
| 87 | + |
| 88 | + [ValidateRange(1, 1048576)] |
| 89 | + [int] $MaxTagLength = 1024, |
| 90 | + |
| 91 | + [ValidateRange(1, 2147483647)] |
| 92 | + [int] $MaxTotalTagLength = 65536, |
| 93 | + |
| 94 | + [ValidateRange(1, 1048576)] |
| 95 | + [int] $MaxNumericLength = 4096 |
| 96 | + ) |
| 97 | + |
| 98 | + begin { |
| 99 | + $lines = [System.Collections.Generic.List[string]]::new() |
| 100 | + } |
| 101 | + process { |
| 102 | + foreach ($line in $InputObject) { |
| 103 | + $lines.Add($line) |
| 104 | + } |
| 105 | + } |
| 106 | + end { |
| 107 | + $yamlText = $lines -join "`n" |
| 108 | + try { |
| 109 | + $documentBox = Read-YamlStream -Yaml $yamlText -Depth $Depth -MaxNodes $MaxNodes ` |
| 110 | + -MaxAliases $MaxAliases -MaxScalarLength $MaxScalarLength -MaxTagLength $MaxTagLength ` |
| 111 | + -MaxTotalTagLength $MaxTotalTagLength -MaxNumericLength $MaxNumericLength |
| 112 | + $formatted = ConvertTo-YamlRepresentationText -Documents $documentBox.Value -Indent $Indent |
| 113 | + $PSCmdlet.WriteObject($formatted, $false) |
| 114 | + } catch { |
| 115 | + if (-not $_.Exception.Data.Contains('IsYamlException')) { |
| 116 | + throw |
| 117 | + } |
| 118 | + $record = New-YamlErrorRecord -Exception $_.Exception -DefaultErrorId 'YamlInvalidInput' ` |
| 119 | + -Category InvalidData -TargetObject $yamlText |
| 120 | + $PSCmdlet.ThrowTerminatingError($record) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments