Skip to content

Instantly share code, notes, and snippets.

@wazeerc
Created March 6, 2025 12:25
Show Gist options
  • Select an option

  • Save wazeerc/2d9321d1e5382ac0c864e9c81e6d51e0 to your computer and use it in GitHub Desktop.

Select an option

Save wazeerc/2d9321d1e5382ac0c864e9c81e6d51e0 to your computer and use it in GitHub Desktop.

Revisions

  1. wazeerc created this gist Mar 6, 2025.
    51 changes: 51 additions & 0 deletions excalidraw.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # Create new Excalidraw file in a "designs" folder
    function new_excalidraw() {
    local filename="${1:-diagram}" # Use 'diagram' as default if no name provided

    # Validate filename
    if [[ ! "$filename" =~ ^[a-zA-Z0-9_-]+$ ]]; then
    echo -e "\033[0;31mError: Filename can only contain letters, numbers, underscores, and hyphens\033[0m"
    return 1
    fi

    # Initial Excalidraw JSON content
    local initial_content='{
    "type": "excalidraw",
    "version": 2,
    "source": "https://excalidraw.com",
    "elements": [],
    "appState": {
    "viewBackgroundColor": "#ffffff"
    }
    }'

    # Determine path: inside Git repo or current directory
    if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    local designs_path="$(git rev-parse --show-toplevel)/designs"
    else
    local designs_path="$(pwd)/designs"
    fi

    # Create "designs" folder if it doesn't exist
    if ! mkdir -p "$designs_path"; then
    echo -e "\033[0;31mError: Failed to create designs directory\033[0m"
    return 1
    fi

    # Define file path and create file
    local path="$designs_path/$filename.excalidraw"
    echo "$initial_content" > "$path"

    if [ ! -f "$path" ]; then
    echo -e "\033[0;31mError: Failed to create file\033[0m"
    return 1
    fi

    # Print success message
    echo -e "\033[0;32mSuccessfully created $path\033[0m"
    echo -e "\033[0;33m[!] Consider adding 'designs/' to .gitignore.\033[0m"
    echo -e "\033[0;34mOpen the file in VS Code with Excalidraw extension\033[0m"
    }

    # Add alias for the new_excalidraw function
    alias excalidraw='new_excalidraw'