1
Fork 0

Download images with unsplash

This commit is contained in:
Jake Howard 2023-09-17 16:15:03 +01:00
parent ce7bb446d6
commit 34acb96ec1
Signed by: jake
GPG Key ID: 57AFB45680EDD477
3 changed files with 21 additions and 0 deletions

View File

@ -4,3 +4,7 @@ redirects:
---
# Sibling
{% set this_image = unsplash("MU8w72PzRow") %}
![{{ this_image.description|default("", true) }}]({{ this_image.urls.regular }})

16
hooks/unsplash.py Normal file
View File

@ -0,0 +1,16 @@
from mkdocs.utils.cache import download_and_cache_url
from datetime import timedelta
import os
import json
import jinja2
UNSPLASH_CACHE_TIME = timedelta(days=14)
def get_unsplash_image(image_id):
client_id = os.environ["UNSPLASH_CLIENT_ID"]
url = f"https://api.unsplash.com/photos/{image_id}?client_id={client_id}"
return json.loads(download_and_cache_url(url, UNSPLASH_CACHE_TIME))
def on_env(env: jinja2.Environment, config, files):
env.globals["unsplash"] = get_unsplash_image
return env

View File

@ -19,6 +19,7 @@ hooks:
- hooks/pygments-styles.py
- hooks/kroki.py
- hooks/include-file.py
- hooks/unsplash.py
markdown_extensions:
- attr_list