Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
Last active December 13, 2016 21:06
Show Gist options
  • Save midnightfreddie/b98f898825daeedeb2f5d1e8ac48f34e to your computer and use it in GitHub Desktop.
Save midnightfreddie/b98f898825daeedeb2f5d1e8ac48f34e to your computer and use it in GitHub Desktop.
A couple of quickie functions to generate filename and front matter for Jekyll posts for copy/paste
<#
.Synopsis
Generates file name and front matter scaffolding for a Jekyll post.
.DESCRIPTION
Given a post title string, creates file name and front matter for a Jekyll
post using the current date and time. Optionally creates the file in the
.\_posts directory or designated -Path\_posts or -PostsPath.
.EXAMPLE
New-JekyllPost "Title of the new post"
.EXAMPLE
New-JekyllPost "Title of the new post" -CreateFile -Path \path\to\jekyll
#>
function New-JekyllPost {
param(
[string]$Title = "This is a title",
[string]$Path = ".",
[string]$PostsPath = "$Path/_posts",
[datetime]$Date = (Get-Date),
[switch]$CreateFile
)
$FileName = "{0}-{1}.md" -f (Get-Date ($Date) -Format "yyyy-MM-dd"), ($Title.ToLower() -replace '\W+', '-')
$Content = @(
"---"
"layout: post"
"title: $Title"
"date: {0}" -f (Get-Date ($Date) -Format "yyyy-MM-dd HH:mm:ss zzz") -replace ':00$', '00'
"---"
) -join "`n"
if ($CreateFile) {
$File = New-Item -ItemType File -Path ("{0}/{1}" -f $PostsPath, $FileName)
Set-Content -Path $File -Value $Content
Write-Output $File
} else {
Write-Output $FileName
Write-Output $Content
}
}
<#
.Synopsis
Generates last_modified_at front matter for a Jekyll post.
.DESCRIPTION
Generates last_modified_at front matter for a Jekyll post based on the current
date and time. To be copied and pasted into the front matter.
.EXAMPLE
Update-JekyllPost
.EXAMPLE
Update-JekyllPost | Set-Clipboard
#>
function Update-JekyllPost {
param(
[datetime]$Date = (Get-Date)
)
"last_modified_at: {0}" -f (Get-Date ($Date) -Format "yyyy-MM-dd HH:mm:ss zzz") -replace ':00$', '00'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment