Last active
September 19, 2024 19:28
-
-
Save larkinwc/ee8a4d0c757ad6b99ae42fc5688c304e to your computer and use it in GitHub Desktop.
Bash script to take a specific obsidian note and copy it along with images into hugo content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -x # Enable debug mode | |
# Check if correct number of arguments are provided | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <obsidian_file> <hugo_content_dir> <hugo_post_name>" | |
exit 1 | |
fi | |
OBSIDIAN_FILE="$1" | |
HUGO_CONTENT_DIR="$2" | |
HUGO_POST_NAME="$3" | |
# Function to format title | |
format_title() { | |
echo "$1" | sed 's/_/ /g; s/\b\(.\)/\u\1/g' | |
} | |
# Format the title | |
FORMATTED_TITLE=$(format_title "$HUGO_POST_NAME") | |
# Derive Hugo root directory from content directory | |
HUGO_ROOT_DIR="${HUGO_CONTENT_DIR%/content/*}" | |
# Get the absolute path of the Obsidian file and convert it to Windows path | |
OBSIDIAN_FILE_ABS=$(wslpath -w "$(readlink -f "$OBSIDIAN_FILE")") | |
# Check if Obsidian file exists | |
if [ ! -f "$OBSIDIAN_FILE" ]; then | |
echo "Error: Obsidian file not found: $OBSIDIAN_FILE" | |
exit 1 | |
fi | |
# Create Hugo post directory | |
HUGO_POST_DIR="$HUGO_CONTENT_DIR/$HUGO_POST_NAME" | |
mkdir -p "$HUGO_POST_DIR" | |
# Function to find and use Hugo archetype | |
use_hugo_archetype() { | |
local archetype_path="$HUGO_ROOT_DIR/archetypes/default.md" | |
if [ -f "$archetype_path" ]; then | |
echo "Using Hugo archetype: $archetype_path" | |
cp "$archetype_path" "$HUGO_POST_DIR/index.md" | |
# Replace placeholders in the archetype | |
sed -i "s|title = \".*\"|title = \"$FORMATTED_TITLE\"|g" "$HUGO_POST_DIR/index.md" | |
sed -i "s|date = \".*\"|date = \"$(date +"%Y-%m-%dT%H:%M:%S%z")\"|g" "$HUGO_POST_DIR/index.md" | |
# Append Obsidian content to the archetype | |
echo "" >> "$HUGO_POST_DIR/index.md" | |
cat "$OBSIDIAN_FILE" >> "$HUGO_POST_DIR/index.md" | |
else | |
echo "Hugo archetype not found. Creating a basic front matter." | |
echo "+++" > "$HUGO_POST_DIR/index.md" | |
echo "title = \"$FORMATTED_TITLE\"" >> "$HUGO_POST_DIR/index.md" | |
echo "date = \"$(date +"%Y-%m-%dT%H:%M:%S%z")\"" >> "$HUGO_POST_DIR/index.md" | |
echo "author = \"Capone\"" >> "$HUGO_POST_DIR/index.md" | |
echo "+++" >> "$HUGO_POST_DIR/index.md" | |
echo "" >> "$HUGO_POST_DIR/index.md" | |
cat "$OBSIDIAN_FILE" >> "$HUGO_POST_DIR/index.md" | |
fi | |
} | |
# Use Hugo archetype | |
use_hugo_archetype | |
# Extract image references from the Markdown file | |
IMAGES=$(grep -oP '!\[\[.*?\]\]' "$HUGO_POST_DIR/index.md" | sed 's/!\[\[//;s/\]\]//') | |
# Function to find image in parent directory | |
find_image() { | |
local parent_dir="$(dirname "$(wslpath -u "$OBSIDIAN_FILE_ABS")")" | |
local grandparent_dir="$(dirname "$parent_dir")" | |
local image_name="$1" | |
echo "Searching for image: $image_name" >&2 | |
echo "In directory: $parent_dir" >&2 | |
echo "And in directory: $grandparent_dir" >&2 | |
if [ -f "$parent_dir/$image_name" ]; then | |
echo "$parent_dir/$image_name" | |
elif [ -f "$grandparent_dir/$image_name" ]; then | |
echo "$grandparent_dir/$image_name" | |
else | |
echo "" | |
fi | |
} | |
# Copy each referenced image and update links | |
while IFS= read -r IMG; do | |
echo "Processing image: $IMG" >&2 | |
# Find the image in the parent directory | |
IMG_PATH=$(find_image "$IMG") | |
echo "Found image path: $IMG_PATH" >&2 | |
# Check if the image file exists | |
if [ -n "$IMG_PATH" ] && [ -f "$IMG_PATH" ]; then | |
# Copy image to Hugo static directory | |
mkdir -p "$HUGO_ROOT_DIR/static/img" | |
cp "$IMG_PATH" "$HUGO_ROOT_DIR/static/img/" | |
echo "Copied: $IMG to $HUGO_ROOT_DIR/static/img/" | |
# Replace Obsidian image syntax with Hugo shortcode in the Markdown file | |
sed -i "s|!\[\[$IMG\]\]|{{< image src=\"/img/$IMG\" alt=\"$IMG\" position=\"center\" style=\"border-radius: 8px;\" >}}|g" "$HUGO_POST_DIR/index.md" | |
else | |
echo "Warning: Image not found: $IMG" | |
fi | |
done <<< "$IMAGES" | |
# Convert any remaining Obsidian-style links to Hugo shortcode | |
sed -i 's/!\[\[\(.*\)\]\]/{{< image src="\/img\/\1" alt="\1" position="center" style="border-radius: 8px;" >}}/g' "$HUGO_POST_DIR/index.md" | |
echo "Post copied to Hugo directory: $HUGO_POST_DIR" | |
echo "Image references updated to Hugo shortcode format." | |
echo "Hugo root directory derived as: $HUGO_ROOT_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment