From fa6daee4e192083fd22165553b3be86cc5fb2d26 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 10 Mar 2023 01:47:44 +0000 Subject: [PATCH] markdown: Fix use of pure_markdown for non-pure markdown rendering. `render_markdown_path` renders Markdown, and also (since baff121115a1) runs Jinja2 on the resulting HTML. The `pure_markdown` flag was added in 0a99fa2fd669, and did two things: retried the path directly in the filesystem if it wasn't found by the Jinja2 resolver, and also skipped the subsequent Jinja2 templating step (regardless of where the content was found). In this context, the name `pure_markdown` made some sense. The only two callsites were the TOS and privacy policy renders, which might have had user-supplied arbitrary paths, and we wished to handle absolute paths in addition to ones inside `templates/`. Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the logic -- it changed it, by making `pure_markdown` only do the former of the two behaviors. Passing `pure_markdown=True` after that commit still caused it to always run Jinja2, but allowed it to look elsewhere in the filesystem. This set the stage for calls, such as the one introduced in dedea237456b, which passed both a context for Jinja2, as well as `pure_markdown=True` implying that Jinja2 was not to be used. Split the two previous behaviors of the `pure_markdown` flag, and use pre-existing data to control them, rather than an explicit flag. For handling policy information which is stored at an absolute path outside of the template root, we switch to using the template search path if and only if the path is relative. This also closes the potential inconsistency based on CWD when `pure_markdown=True` was passed and the path was relative, not absolute. Decide whether to run Jinja2 based on if a context is passed in at all. This restores the behavior in the initial 0a99fa2fd669 where a call to `rendar_markdown_path` could be made to just render markdown, and not some other unmentioned and unrelated templating language as well. --- templates/zerver/documentation_main.html | 12 +++---- zerver/lib/markdown/fenced_code.py | 2 +- zerver/lib/templates.py | 46 +++++++++--------------- zerver/tests/test_email_notifications.py | 34 +++++++++++------- 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/templates/zerver/documentation_main.html b/templates/zerver/documentation_main.html index 9e5358f8eb..98336a695e 100644 --- a/templates/zerver/documentation_main.html +++ b/templates/zerver/documentation_main.html @@ -16,11 +16,11 @@ {% endif %} {% if page_is_policy_center %} - {{ render_markdown_path(sidebar_index, pure_markdown=True) }} + {{ render_markdown_path(sidebar_index) }} {% elif page_is_help_center %} - {{ render_markdown_path(sidebar_index, pure_markdown=True) }} + {{ render_markdown_path(sidebar_index) }} {% else %} - {{ render_markdown_path(sidebar_index, context=api_uri_context, pure_markdown=True) }} + {{ render_markdown_path(sidebar_index, context=api_uri_context) }} {% endif %} {% if not page_is_policy_center %} @@ -36,11 +36,11 @@
{% if page_is_policy_center %} - {{ render_markdown_path(article, pure_markdown=True) }} + {{ render_markdown_path(article) }} {% elif page_is_help_center %} - {{ render_markdown_path(article, context=api_uri_context, help_center=True, pure_markdown=True) }} + {{ render_markdown_path(article, context=api_uri_context, help_center=True) }} {% else %} - {{ render_markdown_path(article, context=api_uri_context, pure_markdown=True) }} + {{ render_markdown_path(article, context=api_uri_context) }} {% endif %}