Public/New-GhostPostFile.ps1
|
function New-GhostPostFile { <# .SYNOPSIS Creates the Markdown file for a new post, with its front matter filled in. .DESCRIPTION Writes the file that Sync-GhostPost turns into a post, so that writing a post starts with writing: a folder for the post, named after its date and its title, with an index.md in it and room for its images next to it. posts/2026-11-20-my-first-post/index.md The front matter has the title, the status draft and the tags, and shows as comments what else a post can be given. The date in the name of the folder becomes the publication date of the post and the rest of the name its slug, the last part of its address. Nothing is sent to a site, and no connection is needed. With -Flat a single file is written instead of a folder: posts/2026-11-20-my-first-post.md. .EXAMPLE New-GhostPostFile -Title 'My first post' -Path ./posts Creates ./posts/<today>-my-first-post/index.md and returns the file. .EXAMPLE New-GhostPostFile -Title 'Release notes for 2.0' -Path ./posts -Tags 'News', 'Releases' | Invoke-Item Creates the file with two tags and opens it in the editor for Markdown files. .EXAMPLE New-GhostPostFile -Title 'Looking back' -Path ./posts -Date '2026-01-31' -Slug 'january' Creates ./posts/2026-01-31-january/index.md. .LINK https://docs.ghost.org/admin-api/posts/creating-a-post #> [CmdletBinding(SupportsShouldProcess)] [OutputType([System.IO.FileInfo])] param( # Title of the post. [Parameter(Mandatory, Position = 0)] [ValidateNotNullOrEmpty()] [string] $Title, # Folder that holds the posts. Created when it is not there. Defaults to the current location. [Parameter(Position = 1)] [string] $Path = '.', # Slug of the post, the last part of its address. Defaults to the title in lower case letters, digits # and hyphens. [ValidatePattern('^[a-z0-9]+(?:-[a-z0-9]+)*$')] [string] $Slug, # Tags of the post. [string[]] $Tags = @(), # Publication date of the post, which goes in the name of the folder. Defaults to today. [datetime] $Date = (Get-Date), # Write a single file instead of a folder with an index.md. [switch] $Flat ) try { # In a variable of its own: the parameter only takes a value that is a slug already. $postSlug = $Slug if (-not $PSBoundParameters.ContainsKey('Slug')) { # Letters with an accent lose it, anything that is not a letter or a digit becomes a hyphen. $plain = $Title.Normalize([System.Text.NormalizationForm]::FormD) -replace '\p{M}' $postSlug = ($plain.ToLowerInvariant() -replace '[^a-z0-9]+', '-').Trim('-') if ($postSlug.Length -gt 80) { $postSlug = $postSlug.Substring(0, 80).Trim('-') } if (-not $postSlug) { throw "The title '$Title' has no letters or digits to make a slug from. Pass -Slug." } } $name = '{0}-{1}' -f $Date.ToString('yyyy-MM-dd', [cultureinfo]::InvariantCulture), $postSlug $posts = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $folder = $posts $file = Join-Path -Path $posts -ChildPath "$name.md" if (-not $Flat) { $folder = Join-Path -Path $posts -ChildPath $name $file = Join-Path -Path $folder -ChildPath 'index.md' } if (Test-Path -LiteralPath $file) { throw "$file exists already." } # Text in double quotes can hold anything, once a backslash and a quote are escaped. $quote = { param($Text) '"{0}"' -f $Text.Replace('\', '\\').Replace('"', '\"') } $tagList = @($Tags | ForEach-Object { & $quote $_ }) -join ', ' $lines = @( '---' "title: $(& $quote $Title)" 'status: draft # published puts the post on the site' "tags: [$tagList]" 'custom_excerpt: "" # shown in lists instead of the first words of the post' '# feature_image: images/cover.png' '# featured: true' '---' '' 'Write the post here, in Markdown.' '' '<!-- An image is a file in the folder images, written as an image with the path images/picture.png -->' '' ) if ($PSCmdlet.ShouldProcess($file, 'Create the file for a new post')) { New-Item -Path $folder -ItemType Directory -Force | Out-Null if (-not $Flat) { New-Item -Path (Join-Path -Path $folder -ChildPath 'images') -ItemType Directory -Force | Out-Null } [System.IO.File]::WriteAllText($file, ($lines -join "`n"), [System.Text.UTF8Encoding]::new($false)) Get-Item -LiteralPath $file } } catch { $PSCmdlet.ThrowTerminatingError($_) } } |