Skip to content

Instantly share code, notes, and snippets.

@herrkaefer
Last active October 18, 2016 03:10
Show Gist options
  • Save herrkaefer/8c4b84b07e565d8e2ff5e649e55d8f95 to your computer and use it in GitHub Desktop.
Save herrkaefer/8c4b84b07e565d8e2ff5e649e55d8f95 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Add a new note (format: markdown with Jekyll-like front matter)
# Usage: addnote.sh title category
# Author: herrkaefer (gloolar@gmail.com)
# Root directory of notes
notesdir="/Users/herrk/Dropbox/Notes"
# title is required
if [ -z "$1" ]
then
echo "No title specified. Usage: addnote.sh title category"
exit 1
fi
title=$1
filename=$1.md
# If category is not specified, it is "uncategorized"
if [ -z "$2" ]
then
echo "No category specified. Note will be put into uncategorized directory."
category="uncategorized"
# Create uncategorized subfolder if it does not exist
if [ ! -d "$notesdir/""$category" ]
then
mkdir "$notesdir/""$category"
fi
else
category=$2
fi
# New category subfolder is forced to be created manually to avoid misspelling
if [ ! -d "$notesdir/""$category" ]
then
echo "Specified category does not exist. If you are sure, make a category subfolder manually first."
exit 1
fi
file="$notesdir/""$category/""$filename"
echo "title: "$title
echo "file: ""$file"
echo "category: "$category
# Open note if it exists
if [ -f "$file" ]
then
echo "Note exists."
open "$file"
exit 1
fi
# Create new note file and fill with initial front matter
touch "$file"
echo --- >> "$file"
echo layout: post >> "$file"
echo title: $title >> "$file"
echo category: $category >> "$file"
echo tags: >> "$file"
echo date: $(date +%Y-%m-%d) >> "$file"
echo last: $(date +%Y-%m-%d) >> "$file"
echo published: false >> "$file"
echo comments: true >> "$file"
echo summary: >> "$file"
echo --- >> "$file"
open "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment