How to prevent Markdown content from being double escaped after rendering HTML tags?

Calendar 158

When using Anqi CMS to manage website content, we sometimes find that the carefully written Markdown content, when displayed on the front-end page, generates HTML tags (such as<p>/<a>) was not correctly parsed by the browser, instead it was interpreted as&lt;p&gt;/&lt;a&gt;This format is displayed directly. This is usually not the effect we want, as it makes the page layout messy and loses the convenience of Markdown editors.

The reason for this phenomenon is that content management systems and template engines usually enable automatic escaping by default for the security of the website.When you edit and save Markdown content in the background, the system may store it in its original Markdown text form.When calling and displaying this content on the front-end page, the Anqi CMS template engine (similar to Django template syntax) will first render the Markdown text into HTML, for example, by transforming# 标题to<h1>标题</h1>The issue is that the generated HTML tags are automatically escaped by the template engine again before being output to the page, which will<h1>becomes&lt;h1&gt;This resulted in HTML tags being escaped twice, ultimately displaying as plain text in the browser.

After understanding this mechanism, the solution becomes clear.The core is to inform the template engine that this content is already in HTML format, it is safe, and does not need to be escaped.

Utilize|safeThe filter ensures that the content is rendered correctly.

The Anqi CMS template engine provides a namedsafeThe filter's role is to mark the content of the variable as 'safe HTML', preventing the template engine from escaping it twice.This is the most direct and commonly used method to solve the problem of Markdown content rendering where tags are escaped twice.

Assuming you are on the article detail page (or other content display page) througharchiveDetailtag to obtain the document content, and assign the content toarticleContentVariable:

{%- archiveDetail articleContent with name="Content" %}

by default,articleContentThe Markdown content is rendered into HTML, but its HTML tags may be escaped again. To avoid this second escaping, you just need to outputarticleContentWhen a variable is|safeFilter:

{{ articleContent|safe }}

Then the template engine sees when it receives the rendered HTML string,|safeThe filter will directly output it as HTML code, and the browser can correctly parse and display the proper formatting and style.

Important reminder: |safeThe filter acts like a "key of trust", telling the template engine that you fully trust the safety of this content. This means that if the Markdown content contains malicious scripts (XSS attack code), using|safeIt will be output as is, which may introduce security risks. Therefore, we strongly recommend only using content sources you trust (such as content entered by backend editors)|safe.

Fine-tune Markdown rendering behavior in specific scenarios

In some cases, you may want to control the rendering process of Markdown content more finely. For example, document contentContentThe field will automatically convert Markdown to HTML when the Markdown editor is turned on. However, you can also manually specify whether to perform this conversion, byrenderParameters are controlled.

If you set it when fetching contentrender=true, the system will forcibly convert Markdown to HTML on the server side. At this point, you still need to combine|safeA filter to prevent these generated HTML tags from being escaped twice:

{# 强制在服务器端将Markdown转为HTML,并使用 |safe 阻止二次转义 #}
{% archiveDetail articleContent with name="Content" render=true %}
{{ articleContent|safe }}

On the contrary, if you setrender=falseThen the system will not convert Markdown to HTML on the server side, but will output the original Markdown text directly.In this case, you usually need to process the original Markdown on the frontend through JavaScript libraries (such as libraries that support mathematical formulae or flowchart rendering). At this time,|safeThe filter is no longer a double-escape for HTML tags, but to ensure that the original Markdown text (if it contains similar HTML special characters) itself is not escaped by the template engine. However, it is more common that if you use a frontend renderer, the responsibility of HTML parsing is handed over to the browser and frontend scripts.|safeThe use case will be somewhat different, mainly depending on how the front-end renderer receives and processes the content.

In summary, to avoid the HTML tags generated by Markdown rendering from being escaped twice, the most critical step is always to remember to add when outputting variables containing HTML content in the template|safeFilter. This ensures that your content is displayed in the expected manner while reminding us to pay attention to the safety of the content source.


Frequently Asked Questions (FAQ)

1. Why does my Markdown content show HTML tags instead of rendering effects?

It is usually because the template engine of AnQi CMS, for security reasons, defaults to escaping all output variable content. When Markdown content is rendered into HTML, these HTML tags (such as<p>It will be escaped twice&lt;p&gt;It will result in displaying it as text in the browser. The solution is to use a filter in the template to inform the template engine that it should not be escaped.|safeThe filter informs the template engine that it should not be escaped.

2. Use|safeIs the filter safe? Will it introduce XSS risks?

|safeThe filter does indeed disable HTML escaping, which means that if there is malicious JavaScript code (XSS) in the content, it will also be output as is and executed. Therefore, it is necessary to use|safeBe cautious. It is recommended to only use this filter on content sources you trust (such as content edited by strictly reviewed back-end editors).For content generated by users, you may need to consider more complex filtering and purification measures, or completely avoid using|safe.

3. Besides|safeFilter, are there other methods to prevent automatic escaping?

except|safeFilter, the template engine also provides{% autoescape off %}and{% autoescape on %}tags to control the automatic escaping behavior of specific code blocks. The{% autoescape off %}Put at the beginning of a content block,{% autoescape on %}At the end, it can close the automatic escaping of all variables within the block. But usually,|safeThe filter, due to its finer scope of action, is the preferred choice for handling specific variable content because it only affects the marked variables and not the entire code block.

Related articles

If AnQiCMS backend enabled Markdown editor, will the front-end content be automatically rendered?

After you enable the Markdown editor in the AnQiCMS background, will the front-end content be automatically rendered?The answer is affirmative, and the system also provides flexible configurations to meet your various needs. ### Enable Markdown Editor First, we need to understand how to enable this feature in the AnQiCMS backend.In the Anqi CMS backend management interface, you will find a section named "Global Settings" that contains "Content Settings".Here, you can easily find and check the 'Enable Markdown Editor' option

2025-11-08

How to set the Markdown rendering parameters for the `Content` field in the Tag detail content?

In AnQi CMS, tags (Tag) are an important part of content management, not just simple keyword indexing.Many operators would like to provide a detailed introduction or special topic content for each tag, which involves how to elegantly display the `Content` field on the tag detail page, especially when this content is written in Markdown format.AnQi CMS provides flexible template tags and configuration options, allowing us to accurately control the rendering behavior of Markdown.###

2025-11-08

How to force-render Markdown content in a single-page `Content` field as HTML?

When using AnQi CMS to manage website content, the Single Page (Page) is a very practical feature that allows you to flexibly create static content such as "About Us", "Contact Information", and so on.Many friends like to write the content of these pages using Markdown, because it is simple and efficient, and can be quickly formatted.However, sometimes we find that even if the content is written in Markdown, the frontend of the page still displays as plain text and is not rendered as HTML.This is often because the template does not correctly instruct the system to render. Don't worry.

2025-11-08

How to enable or disable Markdown rendering in the category detail page's `Content` field?

AnQi CMS provides high flexibility for users, especially in terms of content display.For the `Content` field on the category detail page, the system allows us to finely control whether it is rendered in Markdown format.This means that website operators can choose the most suitable processing method according to different content types and display requirements.### Understanding the `Content` field and Markdown rendering on the category detail page In AnQi CMS, each category has a `Content` field

2025-11-08

The `safe` filter plays what important role after Markdown content is converted to HTML?

AnQiCMS (AnQiCMS) as a content management system provides strong support in content publishing and display.For those who are accustomed to writing content in Markdown format, the convenience of Markdown is self-evident.However, when Markdown content is converted to a browser-readable HTML format, a filter named `safe` plays a crucial role.### Markdown and HTML Conversion: Convenience and Default Escaping In AnQiCMS

2025-11-08

When Markdown-rendered HTML content needs further processing, how to use filters in a chain?

The Anqi CMS, with its flexible content management and powerful template functions, helps us build and operate websites efficiently.For operators and developers accustomed to writing content in Markdown, the excellent support for Markdown in the new Anqi CMS is undoubtedly a blessing.It not only makes content creation more convenient, but also allows content to be presented in a graceful HTML form on the front end.However, after the Markdown content is rendered into HTML by the system, we may find that these HTML contents still need further refinement, such as generating an abstract

2025-11-08

How to enable or disable the Markdown editor feature in the AnQiCMS backend?

When managing content in AnQiCMS, flexibly choosing a handy editing tool can greatly improve your content creation efficiency.The Markdown editor, with its concise syntax and support for specific advanced features (such as mathematical formulas and flowcharts), is favored by many content creators.AnQiCMS as a system dedicated to providing efficient and customizable content management solutions naturally considers this point, allowing you to easily enable or disable the Markdown editor feature according to your actual needs.### Easy Switch

2025-11-08

How to apply GitHub style CSS to rendered Markdown content in AnQiCMS?

In website operation, high-quality content is the core to attract users, and the beautiful layout of the content directly affects the reading experience.AnQiCMS provides a powerful Markdown editor that makes content creation efficient and convenient.To display these Markdown contents with a professional and neat visual effect on your website, we can simply configure them to have GitHub-style CSS styles.Why Choose GitHub Style CSS?GitHub style Markdown

2025-11-08