summary refs log tree commit diff
path: root/markdown.py
diff options
context:
space:
mode:
authorMatt Arnold2025-04-09 15:24:29 -0400
committerMatt Arnold2025-04-09 15:24:29 -0400
commitd1745a9c1e46d43af005ac966cf4170192b76f97 (patch)
tree7de2e583ce0729915ac33dd177099c29ef5d432d /markdown.py
parentd6b7302b791b95b69dd2334e1119e697bd58cab3 (diff)
Supercommit
Diffstat (limited to 'markdown.py')
-rw-r--r--markdown.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/markdown.py b/markdown.py
new file mode 100644
index 0000000..32f9257
--- /dev/null
+++ b/markdown.py
@@ -0,0 +1,44 @@
+# Based on https://gist.github.com/mikefromit/5a6fdfecc9310712f15a872df9f41f03
+
+from jinja2 import pass_environment, nodes
+from jinja2.ext import Extension
+import mistletoe as md
+from mistletoe.contrib.mathjax import MathJaxRenderer
+
+@pass_environment
+def markdown(env, value):
+    """
+    Markdown filter with support for extensions.
+    """
+    output = value
+
+    return md.markdown(output, MathJaxRenderer)
+
+
+class Markdown(Extension):
+    """
+    A wrapper around the markdown filter for syntactic sugar.
+    """
+
+    tags = set(["markdown"])
+
+    def parse(self, parser):  # pragma: no cover
+        """
+        Parses the statements and defers to the callback
+        for markdown processing.
+        """
+        lineno = next(parser.stream).lineno
+        body = parser.parse_statements(["name:endmarkdown"], drop_needle=True)
+
+        return nodes.CallBlock(
+            self.call_method("_render_markdown"), [], [], body
+        ).set_lineno(lineno)
+
+    def _render_markdown(self, caller=None):  # pragma: no cover
+        """
+        Calls the markdown filter to transform the output.
+        """
+        if not caller:
+            return ""
+        output = caller().strip()
+        return markdown(self.environment, output)