diff --git a/cookbook/helper/template_helper.py b/cookbook/helper/template_helper.py new file mode 100644 index 000000000..7cabc7d8a --- /dev/null +++ b/cookbook/helper/template_helper.py @@ -0,0 +1,19 @@ +import bleach +import markdown as md +from bleach_whitelist import markdown_tags, markdown_attrs +from jinja2 import Template + +from cookbook.helper.mdx_attributes import MarkdownFormatExtension +from cookbook.helper.mdx_urlize import UrlizeExtension + + +def render_instructions(step): # TODO deduplicate markdown cleanup code + + template = Template(step.instruction) + instructions = template.render(ingredients=step.ingredients.all()) + + tags = markdown_tags + ['pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'] + parsed_md = md.markdown(instructions, extensions=['markdown.extensions.fenced_code', 'tables', UrlizeExtension(), MarkdownFormatExtension()]) + markdown_attrs['*'] = markdown_attrs['*'] + ['class'] + + return bleach.clean(parsed_md, tags, markdown_attrs) diff --git a/cookbook/models.py b/cookbook/models.py index cdfd2fd51..5d3b29045 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -180,6 +180,10 @@ class Step(models.Model): order = models.IntegerField(default=0) show_as_header = models.BooleanField(default=True) + def get_instruction_render(self): + from cookbook.helper.template_helper import render_instructions + return render_instructions(self) + class Meta: ordering = ['order', 'pk'] diff --git a/cookbook/templates/recipe_view.html b/cookbook/templates/recipe_view.html index c9f950d15..418c1b7a4 100644 --- a/cookbook/templates/recipe_view.html +++ b/cookbook/templates/recipe_view.html @@ -376,7 +376,7 @@