cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

a19b44866fc5917eb5001f856e29d12f773e36fa

verified · cmc

author: Christian Cleberg <hello@cleberg.net> · 2026-08-03T20:16:23Z

serve blog images from root-relative /img/ path

Rewrite absolute img.cleberg.net URLs to /img/ in production builds so the
onion serves images from its own origin instead of fetching them off-onion.
Dev builds keep the absolute URLs so local previews still load images.

Requires the server to serve /var/www/img/ at /img/ on the cleberg.net
vhost (ln -s /var/www/img /var/www/cleberg.net/img).
 build.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/build.py b/build.py
index 0cb8006..28af6ec 100755
--- a/build.py
+++ b/build.py
@@ -317,6 +317,26 @@ def minify_html(src_html, dest_html):
         sys.exit(1)
 
 
+def rewrite_img_urls(build_dir=".build"):
+    """
+    Rewrite absolute img.cleberg.net URLs to root-relative /img/ paths so the
+    onion serves images from its own origin instead of fetching them off-onion.
+    Production only: dev builds keep the absolute URLs so local previews still
+    load images from the live image host.
+
+    Requires the server to serve /var/www/img/ at /img/ on the cleberg.net vhost
+    (e.g. `ln -s /var/www/img /var/www/cleberg.net/img`).
+    """
+    count = 0
+    for html in Path(build_dir).rglob("*.html"):
+        text = html.read_text(encoding="utf-8")
+        new_text = text.replace("https://img.cleberg.net/", "/img/")
+        if new_text != text:
+            count += text.count("https://img.cleberg.net/")
+            html.write_text(new_text, encoding="utf-8")
+    print(f"Rewrote {count} img.cleberg.net references to /img/")
+
+
 def run_emacs_publish(dev_mode=True):
     mode = "development" if dev_mode else "production"
     print(f"Running Emacs publish script ({mode})...")
@@ -579,6 +599,7 @@ def main():
             update_marked_section(html_snippet)
             inject_blog_year_separators()
             generate_tags_page()
+            rewrite_img_urls(build_dir)
             # minify_html("./.build/index.html", "./.build/index.html")
             generate_sitemap()
         if deploy: