Private/ConvertTo-GhostLexical.ps1

function ConvertTo-GhostLexical {
    <#
    .SYNOPSIS
        Wraps Markdown in a Lexical document, the format Ghost stores the content of a post in.

    .DESCRIPTION
        Ghost has a Markdown card. A document that consists of that one card holds the Markdown exactly as it was
        written, and Ghost renders it to HTML itself. Nothing is converted on this side, so the result is the same
        on every PowerShell version.

        Line endings are normalized to line feeds, so that the same text gives the same document on Windows,
        Linux and macOS. Markdown that is empty gives an empty document.

    .EXAMPLE
        ConvertTo-GhostLexical -Markdown '# Hello'

        Returns the Lexical document, as JSON, that holds one Markdown card with the heading.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        # The Markdown to wrap.
        [Parameter(Mandatory)]
        [AllowEmptyString()]
        [string] $Markdown
    )

    $children = @()
    if (-not [string]::IsNullOrWhiteSpace($Markdown)) {
        $children = @(
            [ordered]@{
                type     = 'markdown'
                version  = 1
                markdown = $Markdown -replace '\r\n?', "`n"
            }
        )
    }

    # The same shape as a document that the Ghost editor saves.
    $document = [ordered]@{
        root = [ordered]@{
            children  = $children
            direction = $null
            format    = ''
            indent    = 0
            type      = 'root'
            version   = 1
        }
    }

    ConvertTo-Json -InputObject $document -Depth 5 -Compress
}