markdown: Rewrite /static/ images to use staticfiles_storage.url.

This commit is contained in:
Alex Vandiver 2023-02-03 19:53:58 +00:00 committed by Alex Vandiver
parent ccecc8eb84
commit a1967a7f2d
3 changed files with 36 additions and 0 deletions

View File

@ -17,6 +17,7 @@ PREPROCESSOR_PRIORITES = {
"tabbed_sections": -500, "tabbed_sections": -500,
"nested_code_blocks": -500, "nested_code_blocks": -500,
"emoticon_translations": -505, "emoticon_translations": -505,
"static_images": -510,
} }
BLOCK_PROCESSOR_PRIORITIES = { BLOCK_PROCESSOR_PRIORITIES = {

View File

@ -0,0 +1,33 @@
from typing import Any
from xml.etree.ElementTree import Element
import markdown
from django.contrib.staticfiles.storage import staticfiles_storage
from markdown.extensions import Extension
from zerver.lib.markdown.priorities import PREPROCESSOR_PRIORITES
class MarkdownStaticImagesGenerator(Extension):
def extendMarkdown(self, md: markdown.Markdown) -> None:
md.treeprocessors.register(
StaticImageProcessor(md),
"static_images",
PREPROCESSOR_PRIORITES["static_images"],
)
class StaticImageProcessor(markdown.treeprocessors.Treeprocessor):
"""
Rewrite img tags which refer to /static/ to use staticfiles
"""
def run(self, root: Element) -> None:
for img in root.iter("img"):
url = img.get("src")
if url is not None and url.startswith("/static/"):
img.set("src", staticfiles_storage.url(url[len("/static/") :]))
def makeExtension(*args: Any, **kwargs: str) -> MarkdownStaticImagesGenerator:
return MarkdownStaticImagesGenerator(*args, **kwargs)

View File

@ -22,6 +22,7 @@ import zerver.lib.markdown.help_relative_links
import zerver.lib.markdown.help_settings_links import zerver.lib.markdown.help_settings_links
import zerver.lib.markdown.include import zerver.lib.markdown.include
import zerver.lib.markdown.nested_code_blocks import zerver.lib.markdown.nested_code_blocks
import zerver.lib.markdown.static
import zerver.lib.markdown.tabbed_sections import zerver.lib.markdown.tabbed_sections
import zerver.openapi.markdown_extension import zerver.openapi.markdown_extension
from zerver.lib.cache import dict_to_items_tuple, ignore_unhashable_lru_cache, items_tuple_to_dict from zerver.lib.cache import dict_to_items_tuple, ignore_unhashable_lru_cache, items_tuple_to_dict
@ -127,6 +128,7 @@ def render_markdown_path(
zerver.lib.markdown.help_settings_links.makeExtension(), zerver.lib.markdown.help_settings_links.makeExtension(),
zerver.lib.markdown.help_relative_links.makeExtension(), zerver.lib.markdown.help_relative_links.makeExtension(),
zerver.lib.markdown.help_emoticon_translations_table.makeExtension(), zerver.lib.markdown.help_emoticon_translations_table.makeExtension(),
zerver.lib.markdown.static.makeExtension(),
] ]
if "api_url" in context: if "api_url" in context:
# We need to generate the API code examples extension each # We need to generate the API code examples extension each