Private/Read-GhostPostFile.ps1
|
function Read-GhostPostFile { <# .SYNOPSIS Reads a Markdown file with front matter and returns the post it describes. .DESCRIPTION The file starts with front matter: a JSON object between two lines of three dashes. The Markdown below it is the content of the post. --- { "title": "My post", "tags": ["News"], "custom_excerpt": "Shown in lists" } --- # Heading A key of the front matter is the name of a field of the Ghost API, such as custom_excerpt, or the name of the matching parameter of New-GhostPost, such as CustomExcerpt. An unknown key is an error, so that a typo does not go unnoticed. The slug identifies the post on the site. Without a slug in the front matter, the name of the file without its extension is the slug. It has to be in the form Ghost stores slugs in, lower case letters, digits and hyphens, because Ghost would change any other slug and the post would not be found again. The file is read as UTF-8. Line endings are normalized to line feeds and empty lines around the Markdown are removed, so that the same file gives the same post on every platform. .EXAMPLE Read-GhostPostFile -Path ./posts/my-post.md -Fields $fields Returns an object with the path, the parameters for New-GhostPost or Set-GhostPost, and the Markdown. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # Full path of the Markdown file. [Parameter(Mandatory)] [string] $Path, # The fields the front matter may set: parameter name = name of the field in the Ghost API. [Parameter(Mandatory)] [System.Collections.IDictionary] $Fields ) $text = [System.IO.File]::ReadAllText($Path, [System.Text.Encoding]::UTF8) $pattern = '(?s)\A---[ \t]*\r?\n(?<json>.*?)\r?\n---[ \t]*(?:\r?\n(?<markdown>.*))?\z' $match = [regex]::Match($text, $pattern) if (-not $match.Success) { throw "$Path does not start with front matter: a JSON object between two lines of three dashes (---)." } try { $metadata = ConvertFrom-Json -InputObject $match.Groups['json'].Value } catch { throw "The front matter of $Path is not valid JSON: $($_.Exception.Message)" } # The full type name: [pscustomobject] stands for PSObject, which also matches a list on Windows PowerShell. if ($metadata -isnot [System.Management.Automation.PSCustomObject]) { throw "The front matter of $Path has to be a JSON object: { ... }." } # Both the name of the parameter and the name of the field lead to the parameter. $names = @{} foreach ($name in $Fields.Keys) { $names[$name] = $name $names[$Fields[$name]] = $name } $parameters = @{} foreach ($property in $metadata.PSObject.Properties) { $name = $names[$property.Name] if (-not $name) { $known = @($Fields.Values | Sort-Object) -join ', ' throw "The front matter of $Path has the unknown key '$($property.Name)'. Known keys: $known." } if ($parameters.ContainsKey($name)) { throw "The front matter of $Path sets $($Fields[$name]) twice." } $parameters[$name] = $property.Value } if (-not $parameters.ContainsKey('Slug') -or [string]::IsNullOrWhiteSpace([string] $parameters['Slug'])) { $parameters['Slug'] = [System.IO.Path]::GetFileNameWithoutExtension($Path) } $slug = [string] $parameters['Slug'] if ($slug -cnotmatch '^[a-z0-9]+(?:-[a-z0-9]+)*$' -or $slug.Length -gt 185) { $advice = 'Use lower case letters, digits and hyphens: rename the file or set "slug" in its front matter.' throw "'$slug' cannot be the slug of the post in $Path. $advice" } $parameters['Slug'] = $slug [pscustomobject]@{ Path = $Path Parameters = $parameters Markdown = ($match.Groups['markdown'].Value -replace '\r\n?', "`n").Trim("`n") } } |