From 0a017c66a0c6824106ce9a73d020c717f214724b Mon Sep 17 00:00:00 2001 From: rebecca Date: Mon, 2 Nov 2026 22:41:19 +0000 Subject: add wikilinks --- apioforum/__init__.py | 4 ++-- apioforum/mdrender.py | 17 ++++++++++++++++- apioforum/templates/common.html | 6 +++--- apioforum/templates/view_forum.html | 2 +- apioforum/templates/view_user.html | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 8163e45..673bb0b 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -70,8 +70,8 @@ def create_app(): from .mdrender import render @app.template_filter('md') - def md_render(s): - return render(s) + def md_render(s, forum_id): + return render(s, forum_id) @app.errorhandler(404) def not_found(e): diff --git a/apioforum/mdrender.py b/apioforum/mdrender.py index 2d58f5c..0d75b2f 100644 --- a/apioforum/mdrender.py +++ b/apioforum/mdrender.py @@ -1,6 +1,8 @@ import bleach from bleach._vendor.html5lib.filters.base import Filter from .csscolors import csscolors +from .wiki import normalize_page_name +from flask import url_for allowed_tags = [ 'p', @@ -86,7 +88,17 @@ cleaner = bleach.sanitizer.Cleaner( css_sanitizer=IFrameCSS(), filters=[IFrameSandboxFilter]) +g_forum_id = None +def wikilink_url(label, base, end): + if g_forum_id is None: + return "#" + else: + normalized = normalize_page_name(label) + return url_for('forum.view_wiki_page', forum_id=g_forum_id, path=normalized) + + import markdown +from markdown.extensions.wikilinks import WikiLinkExtension md = markdown.Markdown(extensions=[ 'pymdownx.tilde', 'pymdownx.caret', @@ -94,9 +106,12 @@ md = markdown.Markdown(extensions=[ 'tables', 'pymdownx.details', 'pymdownx.fancylists', + WikiLinkExtension(build_url=wikilink_url), ]) -def render(text): +def render(text, forum_id): + global g_forum_id #blegh + g_forum_id = forum_id text = md.reset().convert(text) text = cleaner.clean(text) return text diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index ee31507..b64630b 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -63,9 +63,9 @@
{% if not preview %} - {{ post.content|md|safe }} + {{ post.content|md(forum)|safe }} {% else %} - {{ post.content[:2500]|md|safe }} + {{ post.content[:2500]|md(forum)|safe }} {% endif %}
{% if footer %} @@ -90,7 +90,7 @@
- {{ message|md|safe }} + {{ message|md(None)|safe }}
{% endmacro %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index bc2aa44..2e1fb0d 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -8,7 +8,7 @@ {%endblock%} {%block content%} -{{forum.description|md|safe}} +{{forum.description|md(forum.id)|safe}}
diff --git a/apioforum/templates/view_user.html b/apioforum/templates/view_user.html index efaea6b..fa0d194 100644 --- a/apioforum/templates/view_user.html +++ b/apioforum/templates/view_user.html @@ -17,7 +17,7 @@
-
{{(user.bio or "hail GEORGE")|md|safe}}
+
{{(user.bio or "hail GEORGE")|md(None)|safe}}

— {{user.username|e}}

-- cgit v1.2.3