Public/Export-GhostPage.ps1

function Export-GhostPage {
    <#
    .SYNOPSIS
        Writes the pages of the Ghost site to Markdown files, one folder per page.

    .DESCRIPTION
        Saves every page of the site as a file that Sync-GhostPage reads, so that pages that were made in Ghost
        Admin can be kept in a repository and be given to another site, such as one on your own machine.

        A page becomes <slug>/index.md in the folder. The file starts with front matter that holds the title of
        the page and every other field that has a value: its status, tags, excerpt, images, template and the
        fields for search engines. The authors are left out. The content of the page follows:

        - A page that was written as Markdown, in one or more Markdown cards, gives that Markdown back.
        - Any other page gives the HTML that Ghost renders for it. Markdown may contain HTML, so Sync-GhostPage
          can send it to a site, where it is stored in a Markdown card. Such a page looks the same in most
          cases, but it is no longer made of the cards of the editor. The HTML is written without empty lines,
          because Markdown stops reading HTML at an empty line. An empty line in a code block is kept. The
          line break before it is written as a character reference, which a browser reads as that line break.

        The property Content of the result says which of the two a page was: Markdown, Html or Empty.

        An image is written as its address, and stays on the site it is on.

        A file that is the same as the page already is left alone. A file that differs is only overwritten with
        -Force; without it the file is reported as Skipped, with a warning. A file whose page is no longer on
        the site is not removed.

    .EXAMPLE
        Export-GhostPage -Path ./pages

        Writes a file for every page of the site that has none yet in the folder pages.

    .EXAMPLE
        Export-GhostPage -Path ./pages -Force

        Also overwrites the files that differ from their page: the folder becomes what the site has.

    .EXAMPLE
        Export-GhostPage -Path ./pages -Filter 'status:published'

        Writes the published pages only.

    .LINK
        https://docs.ghost.org/admin-api/pages/overview
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('Ghost.PageSync')]
    param(
        # Folder for the pages. Created when it is not there.
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string] $Path,

        # Which pages, as a filter of Ghost, such as status:published. Defaults to every page.
        [string] $Filter,

        # Overwrite a file that differs from its page.
        [switch] $Force
    )

    try {
        Test-GhostConnection
        $folder = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)

        $query = @{
            Formats = 'lexical', 'html'
            Include = 'tags'
            Order   = 'slug asc'
        }
        if ($Filter) {
            $query['Filter'] = $Filter
        }

        foreach ($page in @(Get-GhostPage @query)) {
            # The front matter and the content. The slug is the name of the folder.
            $converted = ConvertTo-GhostPostFile -Post $page
            $content = $converted.Text

            $directory = Join-Path -Path $folder -ChildPath $page.slug
            $file = Join-Path -Path $directory -ChildPath 'index.md'
            $action = 'Created'
            if (Test-Path -LiteralPath $file) {
                $existing = [System.IO.File]::ReadAllText($file, [System.Text.Encoding]::UTF8)
                if (($existing -replace '\r\n?', "`n") -ceq $content) {
                    $action = 'Unchanged'
                }
                elseif ($Force) {
                    $action = 'Updated'
                }
                else {
                    $action = 'Skipped'
                    Write-Warning -Message "$file differs from the page '$($page.slug)'. Pass -Force to overwrite it."
                }
            }

            if (@('Created', 'Updated') -contains $action) {
                if (-not $PSCmdlet.ShouldProcess($file, "Write the page '$($page.slug)'")) {
                    continue
                }
                New-Item -Path $directory -ItemType Directory -Force | Out-Null
                [System.IO.File]::WriteAllText($file, $content, [System.Text.UTF8Encoding]::new($false))
            }

            $url = $null
            $address = $page.PSObject.Properties['url']
            if ($address) {
                $url = $address.Value
            }

            [pscustomobject]@{
                PSTypeName     = 'Ghost.PageSync'
                Action         = $action
                Status         = $page.status
                Slug           = $page.slug
                Title          = $page.title
                Id             = $page.id
                Url            = $url
                Path           = $file
                UploadedImages = @()
                Content        = $converted.Content
            }
        }
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}