This blog used to a be a single HTML page which was getting a bit large. I have converted it to a Gatsby (React) site based around markdown pages.
One task I wanted to automate was creating well-named markdown files for each blog post.
I wanted to be able to type in bash:
mknewmd "Book review Pro ASP.NET Core MVC 2 by Adam Freeman"
and to have a file named book-review-pro-aspnet-core-mvc-2-by-adam-freeman.md
created for me, with the contents of a template empty.md
for me to paste the content into. So, all lower case, spaces turned into hyphens and no special characters or dots.
I used two inline bash functions for this. First lowerdashed
:
- turns the spaces into hyphens
-
makes all alphabetic chars lower case like this:
lowerdashed() { hyphensInsteadOfSpaces=${1// /-} ; lowerAndHyphenated=$(sed 's/[^a-zA-Z0-9-]//g' <<< "$hyphensInsteadOfSpaces" ) ; echo "$lowerAndHyphenated" ; }
Then
mknewmd
: -
copies the template
empty.md
file into a new file with the appropriately cleaned file name (returned bylowerdashed
) like this:mknewmd() { cleaned=$(lowerdashed "$1") ; cp src/markdown-pages/empty.md "src/markdown-pages/$cleaned.md" ; }