Allow escaping markdown from external data
This commit is contained in:
parent
c826bda91b
commit
4d36f4f1e3
2 changed files with 14 additions and 2 deletions
|
@ -5,6 +5,6 @@ redirects:
|
||||||
|
|
||||||
# Sibling
|
# Sibling
|
||||||
|
|
||||||
{% set this_image = unsplash("MU8w72PzRow") %}
|
{% set this_image = unsplash("yCdPU73kGSc") %}
|
||||||
|
|
||||||
![{{ this_image.description|default("", true) }}]({{ this_image.urls.regular }})
|
![{{ this_image.description|default("", true)|escape_markdown }}]({{ this_image.urls.regular }})
|
||||||
|
|
|
@ -3,14 +3,26 @@ from datetime import timedelta
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import jinja2
|
import jinja2
|
||||||
|
import re
|
||||||
|
|
||||||
UNSPLASH_CACHE_TIME = timedelta(days=14)
|
UNSPLASH_CACHE_TIME = timedelta(days=14)
|
||||||
|
|
||||||
|
MD_ESCAPE_CHARS = r"\_*[]()~`>#+-=|{}.!"
|
||||||
|
MD_ESCAPE_RE = re.compile(f"([{re.escape(MD_ESCAPE_CHARS)}])")
|
||||||
|
|
||||||
def get_unsplash_image(image_id):
|
def get_unsplash_image(image_id):
|
||||||
client_id = os.environ["UNSPLASH_CLIENT_ID"]
|
client_id = os.environ["UNSPLASH_CLIENT_ID"]
|
||||||
url = f"https://api.unsplash.com/photos/{image_id}?client_id={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))
|
return json.loads(download_and_cache_url(url, UNSPLASH_CACHE_TIME))
|
||||||
|
|
||||||
|
|
||||||
|
def escape_markdown(value):
|
||||||
|
return MD_ESCAPE_RE.sub(r"\\\1", value)
|
||||||
|
|
||||||
|
|
||||||
def on_env(env: jinja2.Environment, config, files):
|
def on_env(env: jinja2.Environment, config, files):
|
||||||
env.globals["unsplash"] = get_unsplash_image
|
env.globals["unsplash"] = get_unsplash_image
|
||||||
|
|
||||||
|
env.filters["escape_markdown"] = escape_markdown
|
||||||
|
|
||||||
return env
|
return env
|
||||||
|
|
Loading…
Reference in a new issue