adamghill / coltrane

A minimal app framework for content sites.
https://coltrane.readthedocs.io/en/stable/
MIT License
91 stars 4 forks source link

Escape django template tags in markdown code blocks #46

Closed jimmybutton closed 1 year ago

jimmybutton commented 1 year ago

Hi there, I'm getting a TemplateSyntaxError when I'm trying to use Django template tags in a code block in a markdown file, e.g.

```html
<!-- templates/home.html -->
{% extends 'base.html' %}
~```

Any ideas how I could escape the Django template tag, but still display the correct syntax in the resulting html file?

jimmybutton commented 1 year ago

I've got the same issue when using React code with a style tag , e.g.

```javascript
function App() {
  return (
    <div style={{padding: "16px"}}>  // <- this line causes the issue
      ...
    </div>
  );
}

Error:

TemplateSyntaxError at /articles/my-article.md

Could not parse the remainder: '</span><span class="nx">padding</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;16px&quot;</span><span class="p">' from '</span><span class="nx">padding</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;16px&quot;</span><span class="p">'
Tobi-De commented 1 year ago

@jimmybutton using the built in django template tag verbatim seems to solve the issue, put it around your code block.

{% verbatim %}

<!-- templates/home.html -->
{% extends 'base.html' %}

{% endverbatim %}

{% verbatim %}

function App() {
  return (
    <div style={{padding: "16px"}}>  // <- this line causes the issue
      ...
    </div>
  );
}

{% endverbatim %}

adamghill commented 1 year ago

I wonder if I could automatically put {% verbatim %} when I see code fences (i.e. the three back ticks) when I'm parsing markdown content. Does that seem useful?

jimmybutton commented 1 year ago

@Tobi-De Many thanks, works like a charm!

Tobi-De commented 1 year ago

I wonder if I could automatically put {% verbatim %} when I see code fences (i.e. the three back ticks) when I'm parsing markdown content. Does that seem useful?

yep, absolutely

jimmybutton commented 1 year ago

I wonder if I could automatically put {% verbatim %} when I see code fences (i.e. the three back ticks) when I'm parsing markdown content. Does that seem useful?

Sounds useful, I agree.

adamghill commented 1 year ago

This has been added to 0.20.0 which I just published. Please upgrade, remove your manual verbatim templatetags, and hopefully it should work for you. Closing for now, but feel free to re-open if you run into problems.