diff --git a/content/nested/sibling.md b/content/nested/sibling.md index 6230c80..bd80784 100644 --- a/content/nested/sibling.md +++ b/content/nested/sibling.md @@ -4,3 +4,7 @@ redirects: --- # Sibling + +{% set this_image = unsplash("MU8w72PzRow") %} + +![{{ this_image.description|default("", true) }}]({{ this_image.urls.regular }}) diff --git a/hooks/unsplash.py b/hooks/unsplash.py new file mode 100644 index 0000000..ff6dfa7 --- /dev/null +++ b/hooks/unsplash.py @@ -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 diff --git a/mkdocs.yml b/mkdocs.yml index 5084d41..cbc793a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -19,6 +19,7 @@ hooks: - hooks/pygments-styles.py - hooks/kroki.py - hooks/include-file.py + - hooks/unsplash.py markdown_extensions: - attr_list