So Outside Technology Security CMS
AnQiCMS
Start Using Service Price
Security CMS Basic Feature Introduction Problem Exchange Security BLOG
Template Manual AnQi Mall Edition Template Market Video Tutorial
Chinese United States English
Log in
Online Demonstration
Start Using Service Price
User Help
Security CMS Basic Feature Introduction Problem Exchange Security BLOG
Template Manual AnQi Mall Edition Template Market Video Tutorial
GitHub
Home Security BLOG When Markdown-rendered HTML content needs further processing, how to use filters in a chain?

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

Calendar 👁️ 84

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 good 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 an elegant 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 summaries, removing specific tags to keep it concise, or making other format adjustments.At this time, the chained filter usage feature provided by AnQiCMS is particularly important, as it allows us to flexibly control at various stages of content rendering.

Understand the rendering basics of Markdown content

In AnQiCMS, when you use the Markdown editor to write articles, product descriptions, or single-page content, the system will automatically convert the Markdown syntax to standard HTML structures when displayed on the front-end. This process occurs internally during template rendering, especially when you call such asarchive.Content/page.Contentorcategory.Contentsuch content field when.

It is worth noting that when calling the content field, you canrenderThe parameter is used to explicitly control whether to convert Markdown to HTML. For example, inarchiveDetailTagged,render=trueIt will indicate that the system should render Markdown content as HTML, whilerender=falseThe original Markdown text will be retained.

Generally, to ensure that the rendered HTML content is correctly parsed and displayed by the browser, rather than being escaped as plain text, we need to use immediately after the content variable|safeFilter. This is because AnQiCMS (like many modern template engines) defaults to escaping all output content to prevent potential XSS attacks.So, a typical Markdown content output would be something like this:

{% archiveDetail articleContent with name="Content" render=true %}
{{ articleContent|safe }}

HerearticleContentThe variable is initially passed throughrender=trueThe parameter is then converted to HTML, then|safeThe filter informs the template engine that this part of the HTML is safe, no need to escape it again, and it can be output directly.

Why do we need to use filters in a chained manner?

The content rendered by Markdown is HTML, but it is often 'native' HTML, which may not fully meet our final display needs. Imagine the following common scenarios:

  • Generate summary or preview:We hope to display a brief summary of each article on the article list page, rather than the full content of the article.This summary needs to retain some basic HTML formats (such as paragraphs, links), but it cannot be too long.
  • Content purification and simplification:Sometimes Markdown content includes HTML elements (such as images, video embeds, and specific levels of headings) that we do not want to display in certain scenarios, and we need to remove them.
  • Uniform Formatting:It may be necessary to apply some global text processing to the rendered HTML, such as unified link text processing,relattributes, or convert specific text patterns to links.

At this time, the filter chain of AnQiCMS comes into play.By connecting multiple filters in a pipeline, we can perform multi-step processing on the content, each step based on the result of the previous step.

Core filter and its chained application

Further processing of HTML content rendered from Markdown, the following are some commonly used filters:

  1. |safe:As mentioned before, it is the first key link in the chain, ensuring that HTML content is not escaped. It usually follows the variable rendering HTML content.

  2. |truncatechars_html:数字or|truncatewords_html:数字:

    • These filters are specifically used forTruncate HTML content safely. Compared to ordinary|truncatecharsor|truncatewordsDifferent, they can intelligently handle HTML tags, ensuring that the truncated HTML structure remains complete and valid, and avoiding layout issues caused by unclosed tags due to truncation.
    • truncatechars_htmlTruncate by character count (including the HTML tags themselves, but internally it adjusts intelligently).
    • truncatewords_htmlTruncate by word count.
    • Chaining example:Assuming we want to truncate the first 150 characters of the article (safe truncation in HTML), and display it on the list page.
      
      {% archiveDetail articleContent with name="Content" render=true %}
      <div class="article-summary">
          {{ articleContent|safe|truncatechars_html:150 }}
      </div>
      
  3. |striptagsor|removetags:"标签1,标签2":

    • |striptagsIt will remove all HTML tags from the HTML content, leaving only plain text. This is very useful when you need to extract a plain text summary from formatted content.
    • |removetags:"p,h1,img"It allows you to specify the HTML tags to be removed. For example, if you want the article summary to not display images and first-level headings, but keep other formats.
    • Chaining example:In the truncated summary, we also want to ensure that no image tags are displayed, making the content purer.
      
      {% archiveDetail articleContent with name="Content" render=true %}
      <div class="article-summary">
          {{ articleContent|safe|truncatechars_html:150|removetags:"img" }}
      </div>
      
      The meaning of this chain is: first render Markdown to HTML, then ensure the HTML is safe to output, then truncate the HTML content to 150 characters (while maintaining the HTML structure), and finally remove all<img>.
  4. |replace:"旧文本,新文本":

    • This filter can replace specific text in HTML strings.If you need to replace local text in rendered HTML content or insert specific identifiers.
    • Chaining example:Assuming we want to replace the specific keyword “AnQiCMS” with a linked version in all content (although it is more recommended to handle it at the source of Markdown, but this is for demonstration). “`twig {% archiveDetail articleContent with name=“Content” render=true %}
Previous The `safe` filter plays what important role after Markdown content is converted to HTML?
Next How to enable or disable the Markdown editor feature in the AnQiCMS backend?

Related articles

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

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

When using Anqi CMS to manage website content, we sometimes find that carefully written Markdown content is not properly parsed by the browser when displayed on the front-end page, instead of being rendered as HTML tags (such as `<p>`, `<a>`) but rather as `&lt;This form of `p&gt;` and `&lt;a&gt;` is displayed directly.This is usually not the effect we want, it makes the page layout chaotic, and also loses the convenience advantages of the Markdown editor.The reason for this phenomenon

2025-11-08

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 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

How to ensure that mathematical formulas in Markdown content are displayed correctly on the web?

In content creation, especially when involving the fields of science, technology, engineering, or mathematics (STEM), it is crucial to present mathematical formulas clearly and accurately.It is often difficult to directly present complex mathematical symbols in traditional web content publishing, but AnQiCMS, with its powerful Markdown editor, combined with some simple configurations, can perfectly present your mathematical formulas on the web.The design philosophy of AnQiCMS is to provide an efficient, easy-to-use, and customizable content management solution.It supports Markdown syntax natively

2025-11-08

How to implement the front-end display of Mermaid flowcharts embedded in AnQiCMS Markdown content?

In AnQi CMS, the Markdown editor brings great flexibility to content creation, and when we need to embed dynamic and intuitive charts such as flowcharts and sequence diagrams in articles, Mermaid is undoubtedly a powerful tool to enhance the expression of content.How can you elegantly embed Mermaid flowcharts in the Markdown content of Anqi CMS and ensure that they display normally on the front-end page?This is actually the process of extending backend editing capabilities to frontend rendering, the entire configuration process is very intuitive.### Step 1

2025-11-08
So Outside Technology Security CMS
So Outside Technology Security CMS

Built on GoLang, reduces memory usage by about 80% compared to PHP class CMS, supports multi-site, multi-language, AI intelligent dialogue and tool calls, and is the preferred solution for small and medium-sized enterprises to build high-performance websites.

  • GitHub
  • GitCode

Security CMS

  • about
  • Download
  • Price
  • demonstration
  • Version History
  • Authorization & donation
  • Online toolkit

Document

  • User Help
  • Template development
  • API documentation
  • Template Market
  • Problem Exchange
  • Anqiblog

Contact

  • WeChat: websafety

Copyright © 2019-2025 Shenzhen Souwai Technology Co., Ltd., AGPL-3.0 License.Privacy Terms 粤ICP备2024356999号

AnQi Content Management System (AnQiCMS)Make the world a safe website