Creating this website

This website is created using mdbook and is hosted on netlify

Here's the tutorial I followed during the creation process.

Publishing Workflow

I wanted to make the process of publishing as easy as possible so it doen't obstruct my flow of idea. Since mdbook uses the srcs files to build the book files, I used github submodules to seperate the builds and the src files. This allow me to save drafts that are not published, but also easily publish my builds when i need to.

I also create a script on my local machine to automate these processes. The script looked something like this:

#!/bin/bash

save_path="path1"
publish_path="$save_path/path2"
build_path="path3"

if [ $1 == "save" ]; then
  read -p "Enter commit message: " commit_message
  cd "$save_path"
  git add .
  git commit -m "$commit_message"
  git push
elif [ $1 == "build" ]; then
  cd "$save_path"
  ./mdbook-summary-maker
  if [ $# -eq 2 ] && ([ $2 == "open" ] || [ $2 == "-o" ]); then
	mdbook build --open --dest-dir $build_path
  else
	mdbook build --dest-dir $build_path
  fi
elif [ $1 == "create" ]; then
  read -p "Enter file name: " file_name
  file_path=$(find "$save_path" -name "$file_name.md")
  if [ -z "$file_path" ]; then
    echo "File not found."
  else
    # process the file path that contains README.md
    replaced_file_path="${file_path/\/$file_name.md/\/README.md}"
    replaced_file_path="./${replaced_file_path#*src/}"
    summary_path="$save_path/src/SUMMARY.md"
    in_file_path="./${file_path#*src/}"
    processed_file_name=$(echo $file_name | sed 's/-/ /g' | sed 's/\b\(.\)/\u\1/g')
    new_entry="- [$processed_file_name]($in_file_path)"
    found=false
    while IFS= read -r line; do
        temp_line=$line
        if [[ $line == *"$replaced_file_path"* ]]; then          
            tab_count=0
            while [[ $line == $'\t'* ]]; do
                line=${line#?}
                tab_count=$((tab_count+1))
            done
            for i in $(seq 1 $tab_count); do echo -n -e "\t" >> temp.md; done
            echo "$line" >> temp.md
            tabs=""
            # if $2 is not empty and is equal to "notab", then don't add tabs
            if [ $# -eq 2 ] && [ $2 == "root" ]; then
                for i in $(seq 1 $((tab_count))); do tabs+="\t"; done
            else
                for i in $(seq 1 $((tab_count+1))); do tabs+="\t"; done
            fi
            # for i in $(seq 1 $((tab_count))); do tabs+="\t"; done
            echo -e $tabs"$new_entry" >> temp.md
            found=true
        else
            echo "$line" >> temp.md
        fi
    done < "$summary_path"
    if [ "$found" = false ]; then
      echo "$new_entry" >> temp.md
    fi
    mv temp.md "$summary_path"
    echo "Successfully added new entry to SUMMARY.md"
  fi
elif [ $1 == "summarize" ]; then
  target_file_name=$2
  # search for the target_file_name in $save_path and store it in a variable
  target_file_path=$(find "$save_path" -name "$target_file_name.md")
  # read the file line by line
  subtitle_list=()
  while IFS= read -r line; do
    # if the line starts with "## "
    if [[ $line == "## "* ]]; then
      # get the content line without the "## "
      subtitle=${line#"## "}
      # put it in a list subtitle_list
      subtitle_list+=("$subtitle")
    fi
  done < "$target_file_path"

  # create a string variable table_of_content
  table_of_content="## Table of Contents\n"
  # for each element in the list subtitle_list
  for subtitle in "${subtitle_list[@]}"; do
    # create a copy of element and add "#" in front of the element. Then replace the spaces in the element to "-" and the name the copy of element "element_link"
    element_link="${subtitle// /-}"
    # make the element_link lowercase
    element_link="${element_link,,}"
    element_link="#${element_link}"
    # put the original element in a line "- [element](element_link)/n"
    line="- [$subtitle]($element_link)\n"
    # add that line into table_of_content
    table_of_content="${table_of_content}${line}"
  done

  # Insert table_of_content at the top of the target file
  temp_file=$(mktemp)  # create a temporary file
  # put the content of target file into the temporary file
  cat "$target_file_path" > "$temp_file"
  # save target file into a target file backup
  cp "$target_file_path" "$target_file_path.bak"
  # put the content of table_of_content into the target file
  echo -e "${table_of_content}$(cat "$target_file_path")" > $temp_file  # write to the temporary file
  mv $temp_file "$target_file_path"  # move the temporary file to the target file path


elif [ $1 == "publish" ]; then
  read -p "Enter commit message: " commit_message
  cd "$publish_path"
  git add .
  git commit -m "$commit_message"
  git push
else
  echo "Invalid argument. Please use 'save', 'build', 'create', 'summarize' or 'publish'"
fi

Afterwards, you must run

sudo cp script.sh /usr/local/bin/<command name>
sudo chmod +x /usr/local/bin/<command name>

Note that when using cd in bash scripts, you have to excute the script via . script.sh or source script.sh. This is becuase when running a bash script, the file run its own shell, according to this post

So I added this in my .bashrc file:

alias <command name>=". <command name>"

Also, when dealing with muti-word directory name, you must change from cd $path to cd "$path". (It must be double quotes)

ChatGPT assistence.

This script is made with the assist of chatGPT. Here's my main prompt:

prompt 1

I want to automate my git operations using a script. This script to be able to run anywhere in a ubuntu linux enviroment using the keywork "(command name)". This script should take three different argument:

  • "save", which ask user for a (commit message), and excute:
  1. cd (pre-defined path)
  2. git add .
  3. git commit -m "(commit message)"
  4. git push
  • "build" which executes:
    1. cd (pre-defined path)
    2. mdbook build --dest-dir (pre-defined path)
  • "publish" which is the same as "save", but i will change the (pre-defined path) later.

prompt 2

I also want make the script edit a SUMMARY.md file in a (pre-defined path).

The user should be able to input "antzed create" and be asked a (file name).

The script will then:

  1. goto a (pre-defined path)

  2. search for the (file name).'s (file path) in reletive to (pre-defined path)

  3. replace the the (file name) part of the (file path) with README.md, for example, if the (file path) is "./knowledge/(file name)", than change it to "./knowledge/README.md". Lets calls this processed file path (replaced file path)

  4. Go to SUMMARY.md, which should be in (pre-defined path) and read line by line to search for (replaced file path). Note that the SUMMARY.md should be in a list structure of markdown list, for example: - [some name](some file path). So should be in (some file path)

  5. create the string "- [ (file name) ]((file path))" and place it a line under the (replaced file path) and add a tab at the start of the line.

prompt 4

fill the todo in this code "elif [ $1 == "summarize"]; then target_file_name = $2

TODO: implement code using given prompt" using this prompt: "Prompt:

write the code that do the following:

  • search for the target_file_name in $save_path and store it in a variable
  • read the file line by line
    • if the line starts with "## "
      • get the content line without the "## "
      • put it in a list subtitle_list create table of content by doing the following:
  • create a string variable table_of_content
  • for each element in the list subtitle_list
    • create a copy of element and add "#" infront of the element. Then replace the spaces in the element to "-" and the name the copy of element "element_link"
    • put the original element in a line "- element/n"
    • add that line into table_of_content Insert table_of_content at the top of the target file"