improved markdown rendering of tables and images
This commit is contained in:
24
cookbook/helper/mdx_attributes.py
Normal file
24
cookbook/helper/mdx_attributes.py
Normal 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)
|
@ -1,7 +1,9 @@
|
|||||||
from django import template
|
from django import template
|
||||||
import markdown as md
|
import markdown as md
|
||||||
import bleach
|
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()
|
register = template.Library()
|
||||||
|
|
||||||
@ -13,7 +15,6 @@ def get_class(value):
|
|||||||
|
|
||||||
@register.filter()
|
@register.filter()
|
||||||
def markdown(value):
|
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)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user