Zettelkasten, but in a /bin/bash script

in programming •  4 months ago

    With the help of our wonderful overlords, "I" have composed a small bash script to take notes, tag em and move on.
    Using #ranger https://github.com/ranger/ranger to explore and #helix https://github.com/helix-editor/helix life's a breeze!! Ok, I still have a little bit of work to do for a fully enjoyable helix experience, but ranger helps ^>^

    #!/bin/bash
    
    # Function to create a tag file
    create_tag_file() {
        touch "notes/$1.md"
        echo "Created tag file: $1.md"
    }
    
    
    # Function to prepend content to tag file
    prepend_content_to_tag_file() {
        # Check if the tag file exists
        if [ -f "notes/$1.md" ]; then
            # Temporarily store the existing content
            existing_content=$(cat "notes/$1.md")
            # Write new content to the tag file
            echo "---" > "notes/$1.md"
            echo "title:$2" >> "notes/$1.md"
            echo "tags: [$3]" >> "notes/$1.md"
            echo "file: $5" >> "notes/$1.md"
            echo "---" >> "notes/$1.md"
            # Append the new content to the existing content
            echo "$4" >> "notes/$1.md"
            echo >> "notes/$1.md"
            echo "$existing_content" >> "notes/$1.md"
        else
            # If the tag file doesn't exist, create it and write the new content
            touch "notes/$1.md"
            echo "---" > "notes/$1.md"
            echo "title:$2" >> "notes/$1.md"
            echo "tags: [$3]" >> "notes/$1.md"
            echo "file: $5" >> "notes/$1.md"
            echo "---" >> "notes/$1.md"
            echo "$4" >> "notes/$1.md"
            echo >> "notes/$1.md"
        fi
    }
    
    # Function to format tags
    format_tags() {
        # Remove non-alphanumeric characters and convert to lowercase
        tags=$(echo "$1" | tr -cd '[:alnum:] ' | tr '[:upper:]' '[:lower:]')
        # Convert space-separated tags to comma-separated string enclosed in square brackets
        formatted_tags=$(echo "[$tags]" | sed 's/ /,/g')
        echo "$formatted_tags"
    }
    
    # Create notes directory if it doesn't exist
    mkdir -p notes/zetts
    
    # Prompt user for title
    read -p "Enter title: " title
    
    # Prompt user for content
    read -p "Enter content: " content
    
    # Prompt user for tags
    read -p "Enter tags (separated by spaces): " tags_input
    
    # Format tags
    formatted_tags=$(format_tags "$tags_input")
    
    # Generate filename with milliseconds since epoch time
    filename=$(date +%s%3N).md
    
    # Limit content to 72 characters per line
    formatted_content=$(echo "$content" | fold -s -w 72)
    
    # Create YAML headline and write to file
    echo -e "---\ntitle:$title\ntags: $formatted_tags\n---\n$formatted_content\n" > "notes/zetts/$filename"
    
    echo "Zett created: notes/zetts/$filename"
    
    # Insert content into all relevant tag files
    IFS=',' read -ra tag_array <<< "${formatted_tags:1:-1}"
    for tag in "${tag_array[@]}"; do
        tag=$(echo "$tag" | tr -d '[]')
        if [[ ! -z "$tag" ]]; then
            create_tag_file "$tag"
            prepend_content_to_tag_file "$tag" "$title" "$formatted_tags" "$formatted_content" "$filename"
        fi
    done
    
    echo "Content inserted into tag files."
    
      Authors get paid when people like you upvote their post.
      If you enjoyed what you read here, create your account today and start earning FREE VOILK!