Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active August 29, 2015 14:19
Show Gist options
  • Save ChaseFlorell/f0bd9d0b342e2540a510 to your computer and use it in GitHub Desktop.
Save ChaseFlorell/f0bd9d0b342e2540a510 to your computer and use it in GitHub Desktop.
function Add-Post {
param (
[parameter(Mandatory=$true, Position=0)][string]$post,
[parameter(Mandatory=$false, Position=1)][switch]$draft
)
if(!(Assert-IsGitRepo)) {
throw 'you are currently not in a git repository.'
}
push-location "$(git rev-parse --show-toplevel)"
$current_datetime = get-date
$current_date = $current_datetime.ToString("yyyy-MM-dd")
$current_time = $current_datetime.ToString("HH:mm:ss")
$url_title = parameterize($post)
$filename = "{0}-{1}.md" -f $current_date, $url_title
# the target dir is either _drafts or _posts based on the $draft flag set by the user.
$dir = if($draft.IsPresent){ "_drafts" } else { "_posts" }
# Jekyll and Pretzel stores posts in a _posts directory,
# therefore we have to make sure this directory exists.
if (-not (test-path $dir)) {
new-item -itemtype directory $dir
}
$path = "$dir/{0}" -f $filename
# Add the default YAML Front Matter at the top of the file.
@"
---
layout: post
title: $post
date: $current_date $current_time
categories: []
tags: []
---
"@ > $path
# Force UTF-8 Encoding
$MyFile = Get-Content $path
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($path, $MyFile, $Utf8NoBomEncoding)
pop-location
}
Set-Alias post Add-Post
function parameterize($title) {
$parameterized_title = $title.ToLower()
$words = $parameterized_title.Split()
$normalized_words = @()
foreach ($word in $words) {
# Convert a Unicode string into its ASCII counterpart, e.g. māja -> maja.
$normalized_word = $word.Normalize([Text.NormalizationForm]::FormD)
# Normalize method returns ASCII letters together with a symbol that "matches"
# the diacritical mark used in the Unicode. These symbols have to be removed
# in order to get a valid string.
$normalized_words += $normalized_word -replace "[^a-z0-9]", [String]::Empty
}
$normalized_words -join "-"
}
function Assert-IsGitRepo {
try {
git status
return $true
} catch {
return $false
}
}
if (Get-Module personal) { return }
Push-Location $psScriptRoot
. .\jekyll.ps1
Pop-Location
Export-ModuleMember `
-Alias @(
'*') `
-Function @('Add-Post')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment