White space stripped from Django Template tags in PrismJS code blocks

huangapple go评论76阅读模式
英文:

White space stripped from Django Template tags in PrismJS code blocks

问题

当我在prismjs块中呈现Django模板代码时,它会去掉空格{{}}和{%%}。

例如,在预渲染之前,代码可能是这样的:

{% image self.search_image thumbnail-400x200 as img %}
<img src="{{ img.url }}" alt="{{ img.title }}">

但渲染后的代码块将是这样的:

{%image self.search_image thumbnail-400x200 as img%}
<img src="{{img.url}}" alt="{{img.title}}">

这不是CSS的问题,空格从HTML中消失了。我可以将语言设置为HTML,甚至是Python等,问题仍然存在。

有人知道如何防止这种情况吗?

英文:

When I render Django template code in a prismjs block, it strips white space {{}} and {%%}.

For example, pre-render, the code might be

{% image self.search_image thumbnail-400x200 as img %}
<img src="{{ img.url }}" alt="{{ img.title }}">

But the rendered code block will be

{%image self.search_image thumbnail-400x200 as img%}
<img src="{{img.url}}" alt="{{img.title}}">

It's not a case of css, the space is missing from the html. I can set the language to HTML, or even python etc, the same issue remains.

Does anyone know of a way to prevent this?

答案1

得分: 0

PrismJS 正试图将你的 Django 模板标签视为所选择的语言,我们可以告诉 PrismJS 只忽略 <code> 块内部的内容,例如:

<pre><code class="language-none">{% image self.search_image thumbnail-400x200 as img %}
<img src="{{ img.url }}" alt="{{ img.title }}"></code></pre>

现在,如果我们想为你的情况找到一种解决方法,我们可以在显示它们在 PrismJS 中之前,将你的 Django 标签中的空格替换为 &amp;nbsp;

{%&amp;nbsp;image self.search_image thumbnail-400x200 as img&amp;nbsp;%}
<img src="{{&amp;nbsp;img.url&amp;nbsp;}}" alt="{{&amp;nbsp;img.title&amp;nbsp;}}">
英文:

PrismJS is trying to handle your Django template tags as the chosen language, we could just tells PrismJS to ignore the content inside the <code> block, for example:

&lt;pre&gt;&lt;code class=&quot;language-none&quot;&gt;{% image self.search_image thumbnail-400x200 as img %}
&lt;img src=&quot;{{ img.url }}&quot; alt=&quot;{{ img.title }}&quot;&gt;&lt;/code&gt;&lt;/pre&gt;

Now if we want to find a workaround for your case we could replace the spaces in your Django tags with &amp;nbsp; before you display them in PrismJS.

{%&amp;nbsp;image self.search_image thumbnail-400x200 as img&amp;nbsp;%}
&lt;img src=&quot;{{&amp;nbsp;img.url&amp;nbsp;}}&quot; alt=&quot;{{&amp;nbsp;img.title&amp;nbsp;}}&quot;&gt;

答案2

得分: 0

以下是您要翻译的内容:

Not so much of an answer as a workaround. I've tacked on the following function to put those whitespaces back in:

const processDjangoCodeBlock = (element) => {
    const content = element.innerHTML;

    // Step 1: Insert whitespace after "{%" and "{{" 
    //         if the next character is not whitespace
    const step1Content = content.replace(/({%)(?!\s)/g, "$1 ")
        .replace(/({{)(?!\s)/g, "$1 ");

    // Step 2: Insert whitespace before "%}" and "}}" 
    //         if the previous character is not whitespace
    const step2Content = step1Content.replace(/(?<!\s)(%})/g, " $1")
        .replace(/(?<!\s)(}})/g, " $1");

    // Update the inner HTML of the element with the processed content
    element.innerHTML = step2Content;
};

Not very satisfactory as I would prefer to just stop PrismJS stripping these in the first place. That issue is going to be lurking in the core code somewhere, another century when I have more time.

The calling template is a Wagtail block, which goes like:

<pre><code class="prism-block language-{{ self.language }}" 
      id="code-block-{{ block.id }}">{{ self.code }}</code></pre>

{% if self.language == "django" %}
    <script>
    document.addEventListener("DOMContentLoaded", () => {
        processDjangoCodeBlock(
            document.getElementById("code-block-{{ block.id }}")
        );
    });
    </script>
{% endif %}
英文:

Not so much of an answer as a workaround. I've tacked on the following function to put those whitespaces back in:

const processDjangoCodeBlock = (element) =&gt; {
    const content = element.innerHTML;

    // Step 1: Insert whitespace after &quot;{%&quot; and &quot;{{&quot; 
    //         if the next character is not whitespace
    const step1Content = content.replace(/({%)(?!\s)/g, &quot;$1 &quot;)
        .replace(/({{)(?!\s)/g, &quot;$1 &quot;);

    // Step 2: Insert whitespace before &quot;%}&quot; and &quot;}}&quot; 
    //         if the previous character is not whitespace
    const step2Content = step1Content.replace(/(?&lt;!\s)(%})/g, &quot; $1&quot;)
        .replace(/(?&lt;!\s)(}})/g, &quot; $1&quot;);

    // Update the inner HTML of the element with the processed content
    element.innerHTML = step2Content;
};

Not very satisfactory as I would prefer to just stop PrismJS stripping these in the first place. That issue is going to be lurking in the core code somewhere, another century when I have more time.

The calling template is a Wagtail block, which goes like:

&lt;pre&gt;&lt;code class=&quot;prism-block language-{{ self.language }}&quot; 
      id=&quot;code-block-{{ block.id }}&quot;&gt;{{ self.code }}&lt;/code&gt;&lt;/pre&gt;

{% if self.language == &quot;django&quot; %}
    &lt;script&gt;
    document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
        processDjangoCodeBlock(
            document.getElementById(&quot;code-block-{{ block.id }}&quot;)
        );
    });
    &lt;/script&gt;
{% endif %}

huangapple
  • 本文由 发表于 2023年6月16日 10:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定