Private/Test-GhostPostChange.ps1

function Test-GhostPostChange {
    <#
    .SYNOPSIS
        Tells whether a post on the site differs from the post a Markdown file describes.

    .DESCRIPTION
        Sync-GhostPost uses this to leave a post alone when nothing would change. An update that changes nothing
        is not harmless: it moves updated_at, which themes show as the date the post was updated, and it sets
        off webhooks.

        Only the content and the fields that the file sets are compared; everything else is not touched by an
        update either. The answer is false only when every one of them is known to be equal. Whenever that
        cannot be established, the answer is true and the post is updated, which is always correct.

        The Markdown is compared with Test-GhostSiteLinkMatch. A link to a page of the same site that the file
        has without the address of the site, such as [text](/a-page/), is equal to the link that Ghost gives
        back with that address in it.

        A tag given as text is compared with the name of the tag, an author given as text with the email address
        of the author. A tag or an author given as an object has to match in every property it has.

    .EXAMPLE
        Test-GhostPostChange -Post $post -Parameters $file.Parameters -Fields $fields -Markdown $file.Markdown

        Returns true when the post has to be updated to match the file.
    #>

    [CmdletBinding()]
    [OutputType([bool])]
    param(
        # The post as Get-GhostPost returned it, with its lexical content.
        [Parameter(Mandatory)]
        [object] $Post,

        # The parameters the file sets, as Read-GhostPostFile returned them.
        [Parameter(Mandatory)]
        [System.Collections.IDictionary] $Parameters,

        # Parameter name = name of the field in the Ghost API, for the fields that are compared.
        [Parameter(Mandatory)]
        [System.Collections.IDictionary] $Fields,

        # The Markdown of the file.
        [Parameter(Mandatory)]
        [AllowEmptyString()]
        [string] $Markdown,

        # The addresses of the site, as Get-GhostSiteUrl returns them. Without one the Markdown has to be the
        # same in every character.
        [string[]] $SiteUrl = @()
    )

    # The content: the Markdown has to be the one and only card of the post.
    $children = $null
    $lexical = $Post.PSObject.Properties['lexical']
    if ($lexical -and $lexical.Value) {
        try {
            $children = @((ConvertFrom-Json -InputObject $lexical.Value).root.children)
        }
        catch {
            Write-Debug -Message "The content of the post cannot be read: $($_.Exception.Message)"
        }
    }
    if ($null -eq $children) {
        return $true
    }

    if ([string]::IsNullOrWhiteSpace($Markdown)) {
        # A post without content is a document with nothing in it or, as the editor saves it, with one empty
        # paragraph.
        foreach ($child in $children) {
            $type = if ($child) { $child.PSObject.Properties['type'] }
            $inner = if ($child) { $child.PSObject.Properties['children'] }
            if (-not $type -or $type.Value -ne 'paragraph' -or ($inner -and @($inner.Value).Count -gt 0)) {
                return $true
            }
        }
        if ($children.Count -gt 1) {
            return $true
        }
    }
    else {
        if ($children.Count -ne 1 -or $null -eq $children[0]) {
            return $true
        }
        $type = $children[0].PSObject.Properties['type']
        $stored = $children[0].PSObject.Properties['markdown']
        if (-not $type -or $type.Value -ne 'markdown' -or -not $stored) {
            return $true
        }
        # The same text, or the same apart from the address of the site in a link.
        if (-not (Test-GhostSiteLinkMatch -Text $Markdown -Stored ([string] $stored.Value) -SiteUrl $SiteUrl)) {
            return $true
        }
    }

    # The fields that the file sets.
    foreach ($name in $Parameters.Keys) {
        if ($Fields.Keys -notcontains $name) {
            continue
        }

        $property = $Post.PSObject.Properties[$Fields[$name]]
        if (-not $property) {
            return $true
        }
        $current = $property.Value
        $desired = $Parameters[$name]

        if ($name -eq 'Tags' -or $name -eq 'Authors') {
            $key = if ($name -eq 'Tags') { 'name' } else { 'email' }
            $currentItems = @($current)
            $desiredItems = @($desired)
            if ($currentItems.Count -ne $desiredItems.Count) {
                return $true
            }

            for ($index = 0; $index -lt $desiredItems.Count; $index++) {
                $wanted = $desiredItems[$index]
                $actual = $currentItems[$index]
                if ($wanted -is [string]) {
                    $wanted = [pscustomobject]@{ $key = $wanted }
                }
                # The full type name: [pscustomobject] stands for PSObject, which matches more than objects.
                if ($wanted -isnot [System.Management.Automation.PSCustomObject] -or $null -eq $actual) {
                    return $true
                }
                foreach ($detail in $wanted.PSObject.Properties) {
                    $actualDetail = $actual.PSObject.Properties[$detail.Name]
                    if (-not $actualDetail -or [string] $actualDetail.Value -cne [string] $detail.Value) {
                        return $true
                    }
                }
            }
            continue
        }

        if ($name -eq 'PublishedAt') {
            # Ghost keeps dates to the second.
            try {
                $difference = ([datetime] $desired).ToUniversalTime() - ([datetime] $current).ToUniversalTime()
            }
            catch {
                return $true
            }
            if ([System.Math]::Abs($difference.TotalSeconds) -ge 1) {
                return $true
            }
            continue
        }

        # The value as it would be sent: an empty string clears a field, which Ghost stores as null.
        $desired = ConvertTo-ApiValue -Value $desired
        if ($null -eq $desired) {
            if (-not [string]::IsNullOrEmpty([string] $current)) {
                return $true
            }
        }
        elseif ($desired -is [bool]) {
            if ($current -isnot [bool] -or $current -ne $desired) {
                return $true
            }
        }
        elseif ([string] $current -cne [string] $desired) {
            return $true
        }
    }

    $false
}