Skip to content

Instantly share code, notes, and snippets.

@selfire1
Last active December 30, 2021 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selfire1/f9ddddedc19a59eb6b3103ab51dec11d to your computer and use it in GitHub Desktop.
Save selfire1/f9ddddedc19a59eb6b3103ab51dec11d to your computer and use it in GitHub Desktop.
A script that creates twelve monthly overview notes in Markdown for use in Obsidian
#!/bin/bash
# Author: Joschua
# Gist script link: https://gist.github.com/selfire1/f9ddddedc19a59eb6b3103ab51dec11d
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# This script is adapted from the following script:
# Author of the original script: Daniel Brandenburg
# Original script link: https://gist.github.com/jdbrice/3b7395d7f1ff724a744a26ab9bfd078b
######################################################################
# Set the year to be created for here:
year=2022
monthCount=1
while [ $monthCount -le 12 ]
do
if (( $monthCount < 10 )); then
month="0${monthCount}"
else
month="${monthCount}"
fi
# use `date` on linus, for OSX set this to `gdate` you may need to install it (brew install coreutils)
date_cmd="gdate"
# OPTIONS
lastDay=$(cal $(${date_cmd} +"${monthCount} ${year}") | awk 'NF {DAYS = $NF}; END {print DAYS}')
# Date range to generate
startDate=${year}-${month}-01
endDate=${year}-${month}-${lastDay}
monthName=$(${date_cmd} +%B --date "${startDate}")
#relative or full path to output file
outputFile="${year}-${month}-${monthName}.md"
# navigation
if test $monthCount -eq 1; then
# January Note
((prevMonthNo=12))
((nextMonthNo=2))
nextMonthPad="02"
prevMonthName="December"
nextMonthName="February"
((prevYear=$year-1))
navigation="[[${prevYear}-${prevMonthNo}-${prevMonthName}|← ${prevMonthName}]] | [[${year}-${nextMonthPad}-${nextMonthName}|${nextMonthName} →]]"
elif test $monthCount -eq 12; then
# December Notes
((prevMonthNo=11))
((nextMonthNo=1))
nextMonthNoPad="01"
prevMonthName="November"
nextMonthName="January"
((nextYear=$year+1))
navigation="[[${year}-${prevMonthNo}-${prevMonthName}|← ${prevMonthName}]] | [[${nextYear}-${nextMonthNoPad}-${nextMonthName}|${nextMonthName} →]]"
else
# All other months
((prevMonthNo=$monthCount-1))
((nextMonthNo=$monthCount+1))
if (( $prevMonthNo < 10 )); then
prevMonthPad="0${prevMonthNo}"
else
prevMonthPad="${prevMonthNo}"
fi
if (( $nextMonthNo < 10 )); then
nextMonthPad="0${nextMonthNo}"
else
nextMonthPad="${nextMonthNo}"
fi
prevMonthName=$(${date_cmd} +%B --date "${year}-${prevMonthNo}-01")
nextMonthName=$(${date_cmd} +%B --date "${year}-${nextMonthNo}-01")
navigation="[[${year}-${prevMonthPad}-${prevMonthName}|← ${prevMonthName}]] | [[${year}-${nextMonthPad}-${nextMonthName}|${nextMonthName} →]]"
fi
# controls the link format for each day:
# [[ $linkFormat | $displayFormat ]]
# Note: you can include `XX` immediately after the day number to get the ordinal suffix
linkFormat="%Y-%m-%d"
displayFormat="%e"
# marker for empty days in calendar view
emptydays="--"
monthHeadingLvl="# " # the heading used for the months
monthHeadingFormat="%B, %Y"
# table headers
hWeek="Week"
hMon="Mon"
hTue="Tue"
hWed="Wed"
hThu="Thu"
hFri="Fri"
hSat="Sat"
hSun="Sun"
#####################################
# should we check for file existance?
# if false the following options wont work
# checking all the files is a little slow,
# so you can turn it off if you want
checkFileExists=false
#no trailing slash please
dailyNotePath="~/Dropbox/Vault/Journals"
# Should non-existing notes get linked?
linkNonExisting=true
#highlight the link for (non)existing
# link is wrapped with this e.g. ** [[ link | display ]] **
hle="" #exsting or used for all if checkFileExists=false
hlne="" # not existing
hlt="==" #highlight for today's date (in addition to others)
frontmatter="---
alias: ${monthName} ${year}
tags: monthlyjournals
---
up:: [[${year}]]"
# END OPTIONS
######################################################################
# Count the number of daily notes that you have
# TODO:
# - breakdown by day / week / month
# - consider metrics on files themselves (word count etc.)
countExistingNotes=0
countNonExistingNotes=0
# initialize the output file
echo "${frontmatter}" > "$outputFile"
curr="$startDate"
DaySuffix() {
case `$date_cmd +%-d --date "$curr"` in
1|21|31) echo "st";;
2|22) echo "nd";;
3|23) echo "rd";;
*) echo "th";;
esac
}
function print_day_wiki_link {
if [ "$checkFileExists" != true ]; then
printf "| $hle[[ %s \| %s ]]$hle " "`$date_cmd +$linkFormat --date "$curr"`" "`$date_cmd +$displayFormat --date "$curr"`" >> "$outputFile"
return
fi
day_suffix=$(DaySuffix)
# replace the string 'XX' with the day suffix if it is found within the linkFormat string
linkTemplate=${linkFormat/XX/$day_suffix}
# Main link output
testFile="$dailyNotePath/`$date_cmd +$linkTemplate --date "$curr"`.md"
hil="" #no highlight by default
if [ `$date_cmd +%Y-%m-%d --date "$curr"` == `$date_cmd +%Y-%m-%d` ]; then
hil="$hlt"
fi
if [ -e "$testFile" ]; then # Daily note file exists
printf "| $hil$hle[[ %s \| %s ]]$hle$hil " "`$date_cmd +$linkTemplate --date "$curr"`" "`$date_cmd +$displayFormat --date "$curr"`" >> "$outputFile"
countExistingNotes=$((countExistingNotes+1))
elif [ "$linkNonExisting" == true ]; then # Daily note file does not exists, but still make a link
printf "| $hil$hlne[[ %s \| %s ]]$hlne$hil " "`$date_cmd +$linkTemplate --date "$curr"`" "`$date_cmd +$displayFormat --date "$curr"`" >> "$outputFile"
countNonExistingNotes=$((countNonExistingNotes+1))
elif [ "$linkNonExisting" == false ]; then # Daily note DNE and also do not link
printf "| $hil$hlne%s$hlne$hil " "`$date_cmd +$displayFormat --date "$curr"`" >> "$outputFile"
countNonExistingNotes=$((countNonExistingNotes+1))
fi
}
while true; do
# echo "$curr"
[ "$curr" \< "$endDate" ] || break
# Print the table header on the first day of the month
if [ `$date_cmd +%d --date "$curr"` == "01" ]; then
# pad the end of the last month
if [ `$date_cmd +%a --date "$curr -1 day"` != "Sun" ] && [ $curr != $startDate ]; then
# day in the week
ndays=`$date_cmd +%u --date "$curr"`
# loop from last day to the end of the week
for i in $(seq $ndays 7); do
printf "|$emptydays" >> "$outputFile"
done
# close the last column
printf "|" >> "$outputFile"
fi
# start a new month
printf "\n" >> "$outputFile"
printf "$monthHeadingLvl %s\n" "`$date_cmd +"$monthHeadingFormat" --date "$curr"`" >> "$outputFile"
printf "\n" >> "$outputFile"
printf "${navigation}" >> "$outputFile"
printf "\n" >> "$outputFile"
printf "\n" >> "$outputFile"
printf "| $hWeek | $hMon | $hTue | $hWed | $hThu| $hFri | $hSat | $hSun |\n" >> "$outputFile"
printf "|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n" >> "$outputFile"
# If the months first week doesnt start on Monday, pad it
# and print the week number
if [ `$date_cmd +%a --date "$curr"` != "Mon" ]; then
# print the week number
printf "|%s" "`$date_cmd +%W --date "$curr"`" >> "$outputFile"
# day in the week
ndays=`$date_cmd +%u --date "$curr"`
# minus one for padding
ndays=`echo "$ndays-1" | bc`
# loop from Monday to the first day of the month
for i in $(seq 1 $ndays); do
printf "|$emptydays" >> "$outputFile"
done
fi
fi
# start the week row with the week number
if [ `$date_cmd +%a --date "$curr"` == "Mon" ]; then
printf "|%s" "`$date_cmd +%W --date "$curr"`" >> "$outputFile"
fi
# Main link output - no arguments, only uses global vars
print_day_wiki_link
# End the row on Sunday
if [ `$date_cmd +%a --date "$curr"` == "Sun" ]; then
printf "|\n" >> "$outputFile"
fi
# increment the date by one day
curr=$( $date_cmd +%Y-%m-%d --date "$curr +1 day" )
done
# print the last day and finish the month off
print_day_wiki_link
# pad the end of the last month
if [ `$date_cmd +%a --date "$curr"` != "Sun" ] ; then
# day in the week
ndays=`$date_cmd +%u --date "$curr"`
# loop from last day to the end of the week
for i in $(seq $ndays 7); do
printf "|$emptydays" >> "$outputFile"
done
# close the last column
printf "|" >> "$outputFile"
fi
echo "${monthName} ${year} created ✅"
((monthCount++))
done
echo "All notes for ${year} created. Happy New Year! 🥳"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment