Skip to content

Commit 80420d3

Browse files
Drop frontmatter extraction; require valid YAML input and tolerate document markers
1 parent 4093211 commit 80420d3

6 files changed

Lines changed: 77 additions & 147 deletions

File tree

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# {{ NAME }}
1+
# Yaml
22

3-
{{ DESCRIPTION }}
3+
A PowerShell module for working with YAML — parse YAML strings into PowerShell objects and serialize PowerShell objects back into YAML.
4+
5+
The module ships two cmdlets that mirror PowerShell's built-in `ConvertFrom-Json` / `ConvertTo-Json`, so the experience is familiar:
6+
7+
- `ConvertFrom-Yaml` — parses a YAML string into a `[PSCustomObject]` (or an ordered hashtable with `-AsHashtable`).
8+
- `ConvertTo-Yaml` — serializes a PowerShell object, hashtable, or array into a YAML-formatted string.
9+
10+
No external dependencies — pure PowerShell. Aligned with the [YAML 1.2.2 core schema](https://yaml.org/spec/1.2.2/#103-core-schema).
411

512
## Prerequisites
613

@@ -13,18 +20,21 @@ This uses the following external resources:
1320
To install the module from the PowerShell Gallery, you can use the following command:
1421

1522
```powershell
16-
Install-PSResource -Name {{ NAME }}
17-
Import-Module -Name {{ NAME }}
23+
Install-PSResource -Name Yaml
24+
Import-Module -Name Yaml
1825
```
1926

2027
## Usage
2128

2229
The module provides two cmdlets that mirror PowerShell's built-in `ConvertFrom-Json` / `ConvertTo-Json`:
2330

24-
| Cmdlet | Alias | Purpose |
25-
| ------------------- | ---------------- | -------------------------------------- |
26-
| `ConvertFrom-Yaml` | `ConvertFrom-Yml` | Parse a YAML string into an object. |
27-
| `ConvertTo-Yaml` | `ConvertTo-Yml` | Serialize an object into a YAML string. |
31+
| Cmdlet | Alias | Purpose |
32+
| ------------------- | ----------------- | ---------------------------------------- |
33+
| `ConvertFrom-Yaml` | `ConvertFrom-Yml` | Parse a YAML string into an object. |
34+
| `ConvertTo-Yaml` | `ConvertTo-Yml` | Serialize an object into a YAML string. |
35+
36+
> [!IMPORTANT]
37+
> The input to `ConvertFrom-Yaml` must be a valid YAML string. The cmdlet does not read files or extract YAML frontmatter from markdown — that is the caller's responsibility (for example, using a Markdown module to extract frontmatter, then piping the result into `ConvertFrom-Yaml`).
2838
2939
### YAML specification
3040

@@ -37,7 +47,7 @@ Practical implications of the core schema:
3747
- Integers and floats parse to their native types using invariant culture.
3848
- Anything else is a string. Quoted strings (`'...'`, `"..."`) always preserve the string type.
3949

40-
The supported YAML subset covers block-style mappings, block-style sequences, nested structures, single- and double-quoted scalars (with `\n`, `\t`, `\r`, `\\`, `\"` escapes in double quotes), and full-line / inline `#` comments. Flow style (`[a, b]`, `{a: 1}`), block scalars (`|`, `>`), anchors, aliases, tags, multi-document streams, and `!!timestamp` are not yet supported.
50+
The supported YAML subset covers block-style mappings, block-style sequences, nested structures, single- and double-quoted scalars (with `\n`, `\t`, `\r`, `\\`, `\"` escapes in double quotes), document start (`---`) and end (`...`) markers, and full-line / inline `#` comments. Flow style (`[a, b]`, `{a: 1}`), block scalars (`|`, `>`), anchors, aliases, tags, multi-document streams, and `!!timestamp` are not yet supported.
4151

4252
### Example 1: Parse a YAML string
4353

@@ -59,13 +69,7 @@ $yaml | ConvertFrom-Yaml
5969
Get-Content config.yaml -Raw | ConvertFrom-Yaml -AsHashtable
6070
```
6171

62-
### Example 3: Parse YAML frontmatter from a markdown file
63-
64-
```powershell
65-
Get-Content post.md -Raw | ConvertFrom-Yaml
66-
```
67-
68-
### Example 4: Convert an object to YAML
72+
### Example 3: Convert an object to YAML
6973

7074
```powershell
7175
[ordered]@{
@@ -74,7 +78,7 @@ Get-Content post.md -Raw | ConvertFrom-Yaml
7478
} | ConvertTo-Yaml
7579
```
7680

77-
### Example 5: Force a top-level YAML sequence
81+
### Example 4: Force a top-level YAML sequence
7882

7983
```powershell
8084
Get-Process | Select-Object -First 3 Name, Id | ConvertTo-Yaml -AsArray

examples/General.ps1

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,16 @@ $person.skills[0] # PowerShell
2222
$hash = $yaml | ConvertFrom-Yaml -AsHashtable
2323
$hash['age'] # 30
2424

25-
# 3. Parse YAML frontmatter from a markdown document
26-
$markdown = @'
27-
---
28-
title: Hello world
29-
draft: false
30-
---
31-
# Body
32-
33-
Markdown content here.
34-
'@
35-
36-
$frontmatter = $markdown | ConvertFrom-Yaml
37-
$frontmatter.title # Hello world
38-
39-
# 4. Convert an object to YAML
25+
# 3. Convert an object to YAML
4026
[ordered]@{
4127
name = 'Alice'
4228
age = 30
4329
skills = @('PowerShell', 'YAML')
4430
} | ConvertTo-Yaml
4531

46-
# 5. Force a top-level sequence with -AsArray
32+
# 4. Force a top-level sequence with -AsArray
4733
Get-Process | Select-Object -First 3 Name, Id | ConvertTo-Yaml -AsArray
4834

49-
# 6. Round-trip
35+
# 5. Round-trip
5036
$obj = [ordered]@{ a = 1; b = @('x', 'y') }
5137
$obj | ConvertTo-Yaml | ConvertFrom-Yaml -AsHashtable

src/functions/private/ConvertFrom-YamlFrontmatter.ps1

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/functions/private/ConvertFrom-YamlLineStream.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
continue
4848
}
4949

50+
# Skip YAML document markers: --- (start) and ... (end).
51+
$trimmed = $stripped.Trim()
52+
if ($trimmed -eq '---' -or $trimmed -eq '...') {
53+
continue
54+
}
55+
5056
$result.Add([pscustomobject]@{
5157
Indent = $indent
5258
Content = $stripped.TrimEnd()

src/functions/public/ConvertFrom-Yaml.ps1

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
Parses a YAML document and returns a `[PSCustomObject]` (default) or an
88
`[ordered]` hashtable when `-AsHashtable` is specified.
99
10-
Supports a useful subset of YAML 1.2:
10+
Supports a useful subset of YAML 1.2.2 (core schema):
1111
- Block-style mappings (key: value)
1212
- Block-style sequences (- item)
1313
- Nested structures
14-
- Scalars: strings, integers, floats, booleans, null
14+
- Scalars: strings, integers, floats, booleans (`true`/`false`), null (`null`/`~`/empty)
1515
- Single- and double-quoted strings (with `\n`, `\t`, `\r`, `\\`, `\"` in double quotes)
16-
- YAML frontmatter delimited by `---` (typical in markdown)
16+
- Document start (`---`) and end (`...`) markers are tolerated
1717
- Full-line comments (`#`) and inline comments after values
1818
19+
Input must be a valid YAML string. Reading frontmatter out of a markdown
20+
document is the responsibility of the caller (see the Markdown module).
21+
1922
Out of scope for this version: flow style (`[a, b]`, `{a: 1}`), block scalars
2023
(`|`, `>`), anchors/aliases, tags, multi-document streams, and `!!timestamp`.
2124
@@ -42,11 +45,6 @@
4245
4346
Reads a YAML file and returns it as an ordered hashtable.
4447
45-
.EXAMPLE
46-
Get-Content post.md -Raw | ConvertFrom-Yaml
47-
48-
Extracts and parses the YAML frontmatter from a markdown file.
49-
5048
.OUTPUTS
5149
System.Management.Automation.PSCustomObject
5250
@@ -92,10 +90,7 @@
9290
return $null
9391
}
9492

95-
# Strip frontmatter delimiters: leading "---" line and trailing "---" line.
96-
$text = ConvertFrom-YamlFrontmatter -Text $text
97-
98-
# Pre-process into logical lines (drop comments and blank lines, keep indentation).
93+
# Pre-process into logical lines (drop comments, blank lines, and document markers).
9994
$lines = ConvertFrom-YamlLineStream -Text $text
10095
if ($lines.Count -eq 0) {
10196
return $null

0 commit comments

Comments
 (0)