Skip to content

Instantly share code, notes, and snippets.

@OhadRubin
Created October 17, 2025 04:25
Show Gist options
  • Save OhadRubin/512f7979f4916c396e91bdebdb58c24b to your computer and use it in GitHub Desktop.
Save OhadRubin/512f7979f4916c396e91bdebdb58c24b to your computer and use it in GitHub Desktop.
md2drive - convert markdown file to docx and upload it to google drive
#!/usr/bin/env bash
set -euo pipefail
# gcloud auth login --enable-gdrive-access
# Usage: ./md2drive <markdown_file>
# Example: ./md2drive example.md
# Requires: export FOLDER_ID=<your_google_drive_folder_id>
MD_FILE="${1:?usage: $0 path/to/file.md}"
# Check if markdown file exists
if [ ! -f "$MD_FILE" ]; then
echo "Error: File '$MD_FILE' not found" >&2
exit 1
fi
# Get the base name and convert to docx
BASENAME=$(basename "$MD_FILE" .md)
DOCX_FILE="${BASENAME}.docx"
echo "Converting $MD_FILE to $DOCX_FILE..." >&2
pandoc "$MD_FILE" -o "$DOCX_FILE"
# Get access token
TOKEN="$(gcloud auth print-access-token)"
# Create daily timestamped folder
TIMESTAMP=$(date +"%Y-%m-%d")
echo "Creating daily folder: $TIMESTAMP..." >&2
FOLDER_RESP="$(curl -s -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"name\":\"$TIMESTAMP\",\"mimeType\":\"application/vnd.google-apps.folder\"}" \
"https://www.googleapis.com/drive/v3/files?fields=id")"
FOLDER_ID="$(printf '%s' "$FOLDER_RESP" | jq -r .id)"
# Upload to Google Drive and convert to Google Docs
echo "Uploading to Google Drive as Google Doc..." >&2
RESP="$(curl -s -X POST \
-H "Authorization: Bearer $TOKEN" \
-F "metadata={name:\"$(basename "$DOCX_FILE" .docx)\",parents:[\"$FOLDER_ID\"],mimeType:\"application/vnd.google-apps.document\"};type=application/json; charset=UTF-8" \
-F "file=@${DOCX_FILE};type=application/vnd.openxmlformats-officedocument.wordprocessingml.document" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink")"
ID="$(printf '%s' "$RESP" | jq -r .id)"
LINK="$(printf '%s' "$RESP" | jq -r .webViewLink)"
# Make link viewable by anyone
echo "Making publicly viewable..." >&2
curl -s -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"role":"reader","type":"anyone"}' \
"https://www.googleapis.com/drive/v3/files/${ID}/permissions" >/dev/null
# Clean up local docx file
rm "$DOCX_FILE"
echo "Local docx file removed." >&2
echo "$LINK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment