improved markdown rendering of tables and images

This commit is contained in:
vabene1111
2020-02-28 21:53:27 +01:00
parent b8f16b50a7
commit a9952b8f57
2 changed files with 30 additions and 5 deletions

View File

@ -0,0 +1,24 @@
import markdown
from markdown.treeprocessors import Treeprocessor
class StyleTreeprocessor(Treeprocessor):
def run_processor(self, node):
for child in node:
if child.tag == "table":
child.set("class", "table table-bordered")
if child.tag == "img":
child.set("class", "img-fluid")
self.run_processor(child)
return node
def run(self, root):
self.run_processor(root)
return root
class MarkdownFormatExtension(markdown.Extension):
def extendMarkdown(self, md, md_globals):
md.treeprocessors.register(StyleTreeprocessor(), 'StyleTreeprocessor', 10)

View File

@ -1,7 +1,9 @@
from django import template
import markdown as md
import bleach
from bleach_whitelist import markdown_tags, markdown_attrs
from bleach_whitelist import markdown_tags, markdown_attrs, all_styles, print_attrs
from cookbook.helper.mdx_attributes import MarkdownFormatExtension
register = template.Library()
@ -13,7 +15,6 @@ def get_class(value):
@register.filter()
def markdown(value):
return bleach.clean(md.markdown(value, extensions=['markdown.extensions.fenced_code']), markdown_tags, markdown_attrs)
tags = markdown_tags + ['pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead']
test = md.markdown(value, extensions=['markdown.extensions.fenced_code', 'tables', MarkdownFormatExtension()])
return bleach.clean(test, tags, print_attrs, markdown_attrs)