From d18bf7c7d539a54d5b8e7fb540610e7d62ac0b22 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 4 Jul 2022 19:43:37 +0200 Subject: [PATCH] Allow code highlight for notes --- app/scss/main.scss | 4 ++ app/source.py | 2 +- app/templates.py | 94 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/app/scss/main.scss b/app/scss/main.scss index 8527389..febbf72 100644 --- a/app/scss/main.scss +++ b/app/scss/main.scss @@ -33,6 +33,10 @@ color: $primary-color; } } +.codehilite { + margin: 50px 0; +} + .activity-main { a { color: $text-color; diff --git a/app/source.py b/app/source.py index 7ba4af5..70c6e03 100644 --- a/app/source.py +++ b/app/source.py @@ -88,6 +88,6 @@ async def markdownify( # Handle custom emoji tags.extend(emoji.tags(content)) - content = markdown(content, extensions=["mdx_linkify"]) + content = markdown(content, extensions=["mdx_linkify", "fenced_code", "codehilite"]) return content, tags, mentioned_actors diff --git a/app/templates.py b/app/templates.py index 129d70d..174fb42 100644 --- a/app/templates.py +++ b/app/templates.py @@ -3,6 +3,7 @@ from datetime import datetime from datetime import timezone from functools import lru_cache from typing import Any +from typing import Callable from urllib.parse import urlparse import bleach @@ -153,13 +154,104 @@ ALLOWED_TAGS = [ "colgroup", "caption", "img", + "div", + "span", ] -ALLOWED_ATTRIBUTES = { +ALLOWED_CSS_CLASSES = [ + "highlight", + "codehilite", + "hll", + "c", + "err", + "g", + "k", + "l", + "n", + "o", + "x", + "p", + "ch", + "cm", + "cp", + "cpf", + "c1", + "cs", + "gd", + "ge", + "gr", + "gh", + "gi", + "go", + "gp", + "gs", + "gu", + "gt", + "kc", + "kd", + "kn", + "kp", + "kr", + "kt", + "ld", + "m", + "s", + "na", + "nb", + "nc", + "no", + "nd", + "ni", + "ne", + "nf", + "nl", + "nn", + "nx", + "py", + "nt", + "nv", + "ow", + "w", + "mb", + "mf", + "mh", + "mi", + "mo", + "sa", + "sb", + "sc", + "dl", + "sd", + "s2", + "se", + "sh", + "si", + "sx", + "sr", + "s1", + "ss", + "bp", + "fm", + "vc", + "vg", + "vi", + "vm", + "il", +] + + +def _allow_class(_tag: str, name: str, value: str) -> bool: + return name == "class" and value in ALLOWED_CSS_CLASSES + + +ALLOWED_ATTRIBUTES: dict[str, list[str] | Callable[[str, str, str], bool]] = { "a": ["href", "title"], "abbr": ["title"], "acronym": ["title"], "img": ["src", "alt", "title"], + "div": _allow_class, + "span": _allow_class, + "code": _allow_class, }