Private/Convert-GhostPostImage.ps1
|
function Convert-GhostPostImage { <# .SYNOPSIS Replaces the references to image files in a post file with the addresses of the images on the site. .DESCRIPTION Takes the post file as Read-GhostPostFile returned it and the references as Get-GhostPostImage returned them, and returns the post with the address of the image in the place of every reference that has one: in the Markdown and in the fields of the front matter. A reference without an address, an image that is not uploaded yet, stays as it is. The post file that was passed is not changed. .EXAMPLE Convert-GhostPostImage -Source $source -Images (Get-GhostPostImage -Source $source -Post $post) Returns the post with the addresses of the images that the post on the site already has. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # The post file, as Read-GhostPostFile returned it. [Parameter(Mandatory)] [object] $Source, # The references to image files, as Get-GhostPostImage returned them. [Parameter(Mandatory)] [AllowEmptyCollection()] [object[]] $Images ) $parameters = @{} foreach ($name in $Source.Parameters.Keys) { $parameters[$name] = $Source.Parameters[$name] } $markdown = $Source.Markdown # From the end of the Markdown to its start, so that the positions of the references before it stay right. foreach ($image in $Images | Sort-Object -Property Index -Descending) { if (-not $image.Url) { continue } if ($image.Field -eq 'Markdown') { $markdown = $markdown.Remove($image.Index, $image.Length).Insert($image.Index, $image.Url) } else { $parameters[$image.Field] = $image.Url } } [pscustomobject]@{ Path = $Source.Path Parameters = $parameters Markdown = $markdown } } |