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. With -IncludeImage the images that
        were uploaded to the site are saved as files as well. Those are the images with /content/images/ in
        their address. Such an image is fetched from its address, which needs no key, and written in a folder
        images next to the file of the page. Its name is the path it has below /content/images/ with a hyphen
        for every slash, such as images/2026-11-diagram.png. The file of the page then has that path where the
        address was, in the content and as feature_image, og_image or twitter_image. That is how Sync-GhostPage
        reads an image file. The property SavedImages of the result has the files that were written.

        - An image that has its file already is not fetched again.
        - An image at any other address stays an address. So does an image that cannot be fetched, with a
          warning, and the next run tries it again.
        - An image in HTML loses its srcset and sizes, which hold addresses of the same image in other widths.
          A site then shows the image as it was uploaded.
        - Sync-GhostPage uploads an image file to the site it sends the page to. That is also true for the
          site the image came from, which then has it twice. Ghost cannot remove an image through its API.
          After that the page has the address of the upload, and an export writes the path of the file for it.

        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. A file counts as the same as its page with its images as addresses and with
        its images as paths. With -IncludeImage a file with addresses is written again with the paths.

        A link to another page of the same site may be written without the address of the site, such as
        [text](/a-page/). Ghost gives it back with the address, and the file still counts as the same as its
        page. Writing such a file again would put the address in its links, so that only happens with -Force.
        When -IncludeImage has an image to save for such a file, the file is reported as Skipped.

    .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 -IncludeImage

        Also saves the images of the pages next to them, such as ./pages/about/images/2026-11-team.png.

    .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,

        # Also save the images that were uploaded to the site, in a folder images next to the file of the page,
        # and write each of them in the file as the path of its file.
        [switch] $IncludeImage
    )

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

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

        # The fields a page file can set, to read a file the way Sync-GhostPage reads it.
        $body = (Get-GhostAPIData -Endpoint 'Set-GhostPage').Body
        $fields = @{}
        foreach ($name in $body.Keys) {
            if (@('Lexical', 'Html', 'UpdatedAt') -notcontains $name) {
                $fields[$name] = $body[$name]
            }
        }

        # The addresses of the site, to compare a link that a file has without them.
        $siteUrl = @(Get-GhostSiteUrl)

        foreach ($page in @(Get-GhostPage @query)) {
            # The slug is the name of the folder.
            $directory = Join-Path -Path $folder -ChildPath $page.slug
            $file = Join-Path -Path $directory -ChildPath 'index.md'

            # What the file needs: to be written, its images to be saved, or nothing.
            $comparison = @{
                Post         = $page
                Path         = $file
                Fields       = $fields
                IncludeImage = $IncludeImage
                Force        = $Force
                SiteUrl      = $siteUrl
            }
            $change = Compare-GhostPostFile @comparison
            $action = $change.Action
            $saved = @()
            if ($action -eq 'Skipped') {
                Write-Warning -Message "$file differs from the page '$($page.slug)'. Pass -Force to overwrite it."
            }
            elseif ($change.Write -or $change.Images.Count -gt 0) {
                $count = $change.Images.Count
                $noun = if ($count -eq 1) { 'image' } else { 'images' }
                $operation = "Write the page '$($page.slug)'"
                if ($count -gt 0 -and $change.Write) {
                    $operation = '{0} and save {1} {2}' -f $operation, $count, $noun
                }
                elseif ($count -gt 0) {
                    $operation = "Save {0} {1} of the page '{2}'" -f $count, $noun, $page.slug
                }
                if (-not $PSCmdlet.ShouldProcess($file, $operation)) {
                    continue
                }
                $done = Save-GhostPostFile -Post $page -Change $change
                $action = $done.Action
                $saved = $done.SavedImages
            }

            $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        = $change.Content
                SavedImages    = @($saved)
            }
        }
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}