Bash Shell Script to create new post with Markdown using Typora

desire-kranenburg-fo-stOdtZLs-unsplash

#!/usr/bin/env bash
tag_candidates=( 'Mac' 'Linux' 'Windows' 'Raspberry' 'Raspberry Pi' 'Ubuntu' 'Arduino' 'AzatAI' 'Books' 'Turkey' 'Trading' 'Finance' 'Quant' 'Backend' 'Frontend' 'Python' 'Django' 'Flutter' 'C' 'C++' 'STM32' 'HTML' 'CSS' 'Java' 'JavaScript' 'JavaScript' 'typescript' 'Database' 'SQL' 'SQL Server' 'Dart' 'Map' 'Go' 'PostgreSQL' 'Postgres' 'Unix' 'AI' 'ML' 'Learning' 'UNEPG' 'UniSat' 'Satellite' 'Nuxt' 'Vuetify' 'Docker' 'Kubernets' 'UI' 'UX' 'Design' 'Typora' 'Shell' 'Bash' 'Markdown')

# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')
current_dir=$(pwd)
posts_dir="$current_dir/_posts"

# template strings for new markdown file 
break="---"
toc="toc: true"
mathjax="mathjax: true"
modify_date="modify_date: $date"

# read arguments from the command line
args="$*"


do_tags(){
    # let's check whether the title includes the tag candidates
    empty_tag=""
    length=${#tag_candidates[@]}
    for i in "${tag_candidates[@]}"; do
    if [[ $args =~ $i ]]; then
        empty_tag="$empty_tag $i"
    fi
    done
    return 0
}

# conditions while we might not have passed any arguments
if [[ -z "$args" ]]; then
  file_name="$posts_dir/$date-new.md"
  title="title: Post title"
  key="key: k_$(echo $RANDOM | head -c 5; echo;)"
  tags="tags: Some Tags"
  
elif [[ -n "$args" ]]; then
  file_name_new=${args//' '/'-'} #replace all spaces with dash
  lower=$(echo "$file_name_new" | tr '[:upper:]' '[:lower:]')
  file_name="$posts_dir/$date-$lower.md"
  title="title: $args"
  key="key: $lower"
  do_tags
  tags="tags: $empty_tag"
fi


# generate the file
# first line is the break

create_file(){
    echo "$break" > $file_name #will creates the file
    echo "$title" >> $file_name
    echo "$key" >> $file_name
    echo "$tags" >> $file_name
    echo "$toc" >> $file_name
    echo "$mathjax" >> $file_name
    echo "$modify_date" >> $file_name
    echo "$break" >> $file_name
    return 0
}

# create the file on the disk
if create_file; then
  echo "success"
else
  echo "failure"
fi
open -a typora $file_name