Skip to content

Commit 3ccee2e

Browse files
add fetch and build functionality
1 parent cdaff4f commit 3ccee2e

12 files changed

Lines changed: 3109 additions & 2 deletions

File tree

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
quote_type = single
7+
8+
[*.{json,yml}]
9+
indent_style = space
10+
indent_size = 2

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
UNSPLASH_ACCESS_KEY=xxxxx
2+
UNSPLASH_COLLECTION_ID=xxxxx

.github/workflows/gh-pages.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: GitHub Pages
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
7+
jobs:
8+
build-deploy:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v6
15+
with:
16+
fetch-depth: 0
17+
persist-credentials: false
18+
- name: Build
19+
run: bash build.sh
20+
- name: Deploy
21+
uses: peaceiris/actions-gh-pages@v3
22+
with:
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
publish_dir: public
25+
cname: samplecontent.automad.org

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
*.jpg
3+
node_modules
4+
response.json
5+
public/images
6+
public/index.html

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# sample-content
2-
A sample content repository that serves as CDN for Automad theme demos
1+
# Sample Content
2+
3+
This repository provides a small, curated collection of size-optimized images used as sample content in theme demos for the Automad CMS. The images are sourced from Unsplash and are made available under the Unsplash License.
4+
5+
The repository also includes a script to fetch images from a specified Unsplash collection, resize and convert them, and generate metadata and a contact sheet.
6+
7+
Processed images are stored in the images directory and committed to the repository. On push, the site is automatically built and deployed, exposing the assets via a CDN endpoint.

build.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
TEMPLATE=$(<template.html)
4+
PUBLIC_DIR=public
5+
IMAGES_DIR=images
6+
7+
shopt -s nullglob
8+
9+
files=(./${IMAGES_DIR}/*.json)
10+
11+
gallery=''
12+
13+
if ((${#files[@]} == 0)); then
14+
echo "No JSON files found"
15+
else
16+
mkdir -p $PUBLIC_DIR/$IMAGES_DIR 2>/dev/null
17+
rsync -a $IMAGES_DIR/*.webp $PUBLIC_DIR/$IMAGES_DIR
18+
19+
for file in "${files[@]}"; do
20+
id=$(jq -r '.id' "$file")
21+
author=$(jq -r '.author' "$file")
22+
profile=$(jq -r '.profile' "$file")
23+
unsplash=$(jq -r '.unsplash_url' "$file")
24+
image=${IMAGES_DIR}/${id}.webp
25+
26+
read -r -d '' item <<-EOF
27+
<figure>
28+
<a
29+
href="${image}"
30+
class="card"
31+
title="by ${author} on Unsplash"
32+
target="_blank"
33+
>
34+
<img src="${image}" />
35+
</a>
36+
<figcaption>
37+
by <a href="$profile" target="_blank">$author</a> on
38+
<a href="$unsplash" target="_blank">Unsplash</a>
39+
</figcaption>
40+
</figure>
41+
EOF
42+
43+
gallery="${gallery}\n${item}\n"
44+
done
45+
fi
46+
47+
echo -e "${TEMPLATE/__ITEMS__/$gallery}" >"$PUBLIC_DIR/index.html"

fetch.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
. .env
6+
7+
OUTPUT_DIR=./images
8+
MAX_HEIGHT=1000
9+
MAX_WIDTH=1600
10+
11+
if [ -z $UNSPLASH_ACCESS_KEY ]; then
12+
echo "Access key missing."
13+
exit 1
14+
fi
15+
16+
if [ -z $UNSPLASH_COLLECTION_ID ]; then
17+
echo "Collection ID missing."
18+
exit 1
19+
fi
20+
21+
mkdir -p "$OUTPUT_DIR" 2>/dev/null
22+
23+
response=$(curl -s "https://api.unsplash.com/collections/$UNSPLASH_COLLECTION_ID/photos?client_id=$UNSPLASH_ACCESS_KEY&page=1&per_page=500")
24+
25+
echo "$response" >response.json
26+
27+
echo "$response" | jq -c '.[]' | while read -r photo; do
28+
29+
id=$(echo "$photo" | jq -r '.id')
30+
31+
# Skip if already downloaded
32+
if [ -f "$OUTPUT_DIR/$id.jpg" ]; then
33+
echo "Skipping $id (already exists)"
34+
continue
35+
fi
36+
37+
raw_url=$(echo "$photo" | jq -r '.urls.raw')
38+
author=$(echo "$photo" | jq -r '.user.name')
39+
username=$(echo "$photo" | jq -r '.user.username')
40+
profile=$(echo "$photo" | jq -r '.user.links.html')
41+
photo_url=$(echo "$photo" | jq -r '.links.html')
42+
download_location=$(echo "$photo" | jq -r '.links.download_location')
43+
44+
download_url="${raw_url}&h=${MAX_HEIGHT}&w=${MAX_WIDTH}&fit=max&fm=jpg&q=100"
45+
46+
echo "Downloading $id..."
47+
48+
# Track download (per Unsplash API guidelines)
49+
curl -s "${download_location}?client_id=${UNSPLASH_ACCESS_KEY}" >/dev/null
50+
51+
curl -fL "$download_url" -o "$OUTPUT_DIR/$id.jpg"
52+
53+
cat >"$OUTPUT_DIR/$id.json" <<EOF
54+
{
55+
"id": "$id",
56+
"author": "$author",
57+
"username": "$username",
58+
"profile": "$profile",
59+
"unsplash_url": "$photo_url",
60+
"downloaded_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
61+
}
62+
EOF
63+
sleep 0.3
64+
done
65+
66+
echo "All images downloaded."
67+
68+
node optimize.js

optimize.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import imagemin from 'imagemin';
2+
import imageminWebp from 'imagemin-webp';
3+
4+
(async () => {
5+
await imagemin(['images/*.jpg'], {
6+
destination: 'images',
7+
plugins: [imageminWebp({ quality: 70 })],
8+
});
9+
10+
console.log('Images optimized');
11+
})();

0 commit comments

Comments
 (0)