Last active
December 12, 2022 10:19
-
-
Save AucT/5e685d4bca9bce6abcfa8d797df148bc to your computer and use it in GitHub Desktop.
powershell script to create new post for hugo with good file name like 2022-12-12_my-awesome-title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This is my file hugonew | |
## | |
## 1. Place this file where your helper scripts are. For example I have dropbox folder "my_scripts". | |
## Then add this folder to path variable. | |
## 2. cd to folder of your hugo blog and type: | |
## hugonew 'my awesome new nice title' | |
## | |
## this will create file 2022-12-12_my-awesome-new-nice-title.md in content/posts folder (if today is 2022-12-12) | |
## Some dummy ConvertTo-Slug function. scroll to bottom to see code. | |
## I have no idea how to properly import function so I just copy pasted from https://github.com/kns7/KNSHelper | |
## At least now this file has no dependencies ;) | |
######################################################################################### | |
# KNS Helper Module | |
# | |
# Copyright 2019, Nicolas Kapfer | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software | |
# and associated documentation files (the "Software"), to deal in the Software without restriction, | |
# including without limitation the rights to use, copy, modify, merge, publish, distribute, | |
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all copies or | |
# substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
######################################################################################### | |
Function ConvertTo-Slug | |
{ | |
<# | |
.SYNOPSIS | |
Slugify a String in entry | |
.DESCRIPTION | |
This function "slugifies" a string given in entry | |
.PARAMETER Text | |
String. Text to slugify | |
.PARAMETER Delimiter | |
String. Optional, the delimiter used. If omitted, "-" will be used | |
.PARAMETER CapitalizeFirstLetter | |
Switch. If the switch is set, it will capitalize the first letter of each word in the "Text" string. | |
.EXAMPLE | |
PS> ConvertTo-Slug "That's all folks!" | |
Will return "thats-all-folks" | |
.EXAMPLE | |
PS> ConvertTo-Slug "That's all Folks!" -Delimiter "_" | |
Will return "thats_all_folks" | |
.EXAMPLE | |
PS> ConvertTo-Slug "That's all Folks!" -Delimiter "" -CapitalizeFirstLetter | |
Will return "ThatsAllFolks" | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=0)] | |
[String] | |
$Text, | |
[Parameter(Mandatory=$False,Position=1)] | |
[string] | |
$Delimiter="-", | |
[Parameter()] | |
[Switch] | |
$CapitalizeFirstLetter | |
) | |
$bytes = [System.Text.Encoding]::GetEncoding("Cyrillic").GetBytes($text) | |
$result = [System.Text.Encoding]::ASCII.GetString($bytes).ToLower() | |
if($CapitalizeFirstLetter){ | |
$TextInfo = (Get-Culture).TextInfo | |
$result = $TextInfo.ToTitleCase($result) | |
} | |
$rx = [System.Text.RegularExpressions.Regex] | |
$result = $rx::Replace($result, "[^a-zA-Z0-9\s-]", "") | |
$result = $rx::Replace($result, "\s+", " ").Trim(); | |
$result = $rx::Replace($result, "\s", $Delimiter); | |
Write-Output $result | |
} | |
$title = $args[0] | |
$dateTime = Get-Date -Format "yyyy-MM-ddTHH:mm:sszzz" | |
$date = Get-Date -Format "yyyy-MM-dd" | |
$slug = ConvertTo-Slug $title | |
$slug = $slug.Substring(0,[Math]::Min(60,$slug.Length)) | |
$fileName = "content\posts\"+$date+"_"+$slug+".md" | |
$content=@" | |
--- | |
title: "$title" | |
date: $dateTime | |
tags: ["ubuntu"] | |
--- | |
"@ | |
New-Item $fileName | |
Set-Content $fileName $content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment