Public/Export-GhostContent.ps1
|
function Export-GhostContent { <# .SYNOPSIS Saves the content of the Ghost site to a file. .DESCRIPTION Writes the export of the site to a JSON file: every post and page with its tags, and the settings of the site, such as its title and navigation. It is the file that Ghost Admin offers under Settings > Advanced > Import/Export, and the file that Import-GhostContent and Ghost Admin take. Members are not in it, and neither are the image files themselves. Nothing on the site changes. The export does hold everything that was written, drafts included, so treat the file like the site itself. An integration may not export a site: connect with a staff access token of an owner or an administrator, which is on your profile in Ghost Admin. In the export an image has no site in its address, so that the site that imports it looks for the image among its own files. With -AbsoluteUrl the address of this site is written there: a site that imports the file then shows the images from this site, which is what a copy of a site on your own machine needs. .EXAMPLE Export-GhostContent -Path ./backup/site.json Saves the content of the connected site. .EXAMPLE Export-GhostContent -Path ./live.json -AbsoluteUrl -Force Saves the content for a copy of the site that shows the images of this site, and replaces the file when it is there already. .LINK https://docs.ghost.org/admin-api #> [CmdletBinding()] [OutputType([System.IO.FileInfo])] param( # The JSON file to write. [Parameter(Mandatory, Position = 0)] [ValidateNotNullOrEmpty()] [string] $Path, # Write the address of this site in the addresses of images and files. [switch] $AbsoluteUrl, # Replace the file when it exists. [switch] $Force ) begin { Test-GhostConnection $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name } process { try { $file = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) if ((Test-Path -LiteralPath $file) -and -not $Force) { throw "$file exists already. Pass -Force to replace it." } $folder = [System.IO.Path]::GetDirectoryName($file) if (-not (Test-Path -LiteralPath $folder -PathType Container)) { throw "The folder $folder does not exist." } $uri = New-URIString -Endpoint $resources.URI try { $export = Submit-Request -Uri $uri -Method $resources.Method -Success $resources.Success -Raw } catch { if ($_.FullyQualifiedErrorId -like 'NoPermissionError*') { $advice = 'Connect with a staff access token of an owner or an administrator instead.' throw "An integration may not export a site. $advice It is on your profile in Ghost Admin." } throw } if ($AbsoluteUrl) { # The site itself, without /ghost/api/admin. Only what Ghost stores as files: a link to a post # keeps its placeholder, and leads to the same post on the site that imports the file. $site = $uri -replace '/ghost/api/admin/.*$' $export = $export.Replace('__GHOST_URL__/content/', "$site/content/") } [System.IO.File]::WriteAllText($file, $export, [System.Text.UTF8Encoding]::new($false)) Get-Item -LiteralPath $file } catch { $PSCmdlet.ThrowTerminatingError($_) } } } |