@@ -76,11 +76,58 @@ jobs:
7676 cat "$mdfile" > "$slidev_dir/step1.md"
7777 fi
7878
79- # Copy images directory FIRST, before processing content
80- if [ -d "images" ]; then
81- cp -r images "$slidev_dir/"
82- echo "Copied images directory to $slidev_dir"
83- fi
79+ # Smart image handling - find and copy only referenced images
80+ echo "=== Finding referenced images in markdown ==="
81+
82+ # Create images directory in slidev project
83+ mkdir -p "$slidev_dir/images"
84+
85+ # Extract all image references from the markdown file
86+ # This handles: , , and YAML image: path
87+ mdfile_dir=$(dirname "$mdfile")
88+
89+ # Find all image references and process them
90+ grep -E '!\[.*\]\(.*\)|image:\s*[^[:space:]]+' "$mdfile" | while IFS= read -r line; do
91+ # Extract image path from markdown syntax  or YAML image: path
92+ if echo "$line" | grep -q '!\[.*\]'; then
93+ # Markdown image syntax: 
94+ img_path=$(echo "$line" | sed -n 's/.*!\[.*\](\([^)]*\)).*/\1/p' | sed 's/[[:space:]]*".*$//')
95+ elif echo "$line" | grep -q 'image:'; then
96+ # YAML image property: image: path
97+ img_path=$(echo "$line" | sed -n 's/.*image:[[:space:]]*\([^[:space:]]*\).*/\1/p')
98+ else
99+ continue
100+ fi
101+
102+ if [ -n "$img_path" ]; then
103+ echo "Found image reference: $img_path"
104+
105+ # Determine source path based on reference type
106+ if [[ "$img_path" == ../* ]]; then
107+ # Relative to parent: ../images/file.png
108+ src_path="${img_path#../}"
109+ elif [[ "$img_path" == ./* ]]; then
110+ # Relative to current: ./images/file.png
111+ src_path="$mdfile_dir/${img_path#./}"
112+ elif [[ "$img_path" == /* ]]; then
113+ # Absolute from root: /images/file.png
114+ src_path="${img_path#/}"
115+ else
116+ # Relative path: images/file.png
117+ src_path="$mdfile_dir/$img_path"
118+ fi
119+
120+ # Copy the image if it exists
121+ if [ -f "$src_path" ]; then
122+ # Get just the filename for destination
123+ img_filename=$(basename "$img_path")
124+ cp "$src_path" "$slidev_dir/images/$img_filename"
125+ echo "Copied: $src_path → $slidev_dir/images/$img_filename"
126+ else
127+ echo "WARNING: Image not found: $src_path (referenced as: $img_path)"
128+ fi
129+ fi
130+ done
84131
85132 # Convert markdown content for Slidev format and fix image paths
86133 echo "=== Processing markdown content ==="
@@ -89,10 +136,16 @@ jobs:
89136
90137 # Test the sed commands manually first
91138 echo "=== Testing sed commands ==="
92- echo "Test 1 - ../images/:"
93- echo "" | sed 's|\.\.\/images\/|\.\/images\/|g; s|(\s*/images/|(./images/|g'
94- echo "Test 2 - /images/:"
95- echo "" | sed 's|\.\.\/images\/|\.\/images\/|g; s|(\s*/images/|(./images/|g'
139+ echo "Test 1 - ../images/file.png:"
140+ echo "" | sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g'
141+ echo "Test 2 - /images/file.png:"
142+ echo "" | sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g'
143+ echo "Test 3 - ./images/file.png:"
144+ echo "" | sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g'
145+ echo "Test 4 - YAML image property:"
146+ echo "image: ../images/ErrorModelWithBoth.png" | sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g'
147+ echo "Test 5 - Complex path:"
148+ echo "" | sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g'
96149
97150 # Process content step by step using temp files
98151 echo "=== Step 1 - Original content ==="
@@ -102,8 +155,9 @@ jobs:
102155 echo "=== Step 2 - After header conversion ==="
103156 grep "images/" "$slidev_dir/step2.md" || echo "No images found"
104157
105- # Fix image paths: both ../images/ and /images/ should become ./images/
106- sed 's|\.\.\/images\/|\.\/images\/|g; s|(\s*/images/|(./images/|g' "$slidev_dir/step2.md" > "$slidev_dir/step3.md"
158+ # Normalize all image paths to ./images/filename since we copied all images there
159+ # This handles: ../images/file.png, ./images/file.png, /images/file.png, images/file.png
160+ sed -E 's|!\[([^\]]*)\]\(([^/]*/)*(([^/)]+\.(png|jpg|jpeg|gif|svg|webp)))\)||g; s|(image:[[:space:]]*)([^/]*/)*(([^/[:space:]]+\.(png|jpg|jpeg|gif|svg|webp)))|\1./images/\4|g' "$slidev_dir/step2.md" > "$slidev_dir/step3.md"
107161 echo "=== Step 3 - After image path conversion ==="
108162 grep "images/" "$slidev_dir/step3.md" || echo "No images found"
109163
0 commit comments