How to convert Markdown to HTML in a template and then remove the tags from the generated HTML?

Calendar 81

In the AnQiCMS template, when processing Markdown content, it is often necessary to first convert it to HTML, and then further filter or remove specific HTML tags on this basis.This is particularly useful when creating content summaries, generating plain text previews, or to achieve specific layout controls.The template tags and filters provided by AnQi CMS can help us complete these operations efficiently and flexibly.

First step: Convert Markdown content to HTML

The Anqi CMS template engine is built with support for Markdown content rendering.When your content is entered through a Markdown editor, it is usually stored as Markdown formatted text.To display it as browsable HTML on the front-end page, you need to instruct the template engine to convert it.

For the document content field (archiveDetailofContent),AnQi CMS is enabled by default in the Markdown editor and will automatically convert it to HTML. But if you need to control the conversion process more explicitly, or convert other custom Markdown fields, you can userender=trueorrenderfilter.

For example, on a document detail page, we usually display the article content in this way:

{# 假设archiveDetail已经获取了当前文档详情 #}
{%- archiveDetail articleContent with name="Content" render=true %}
{{ articleContent|safe }}

here,render=trueExplicitly indicate that the template engine willContentRender the Markdown text in the field as HTML.

Important reminder:Converted HTML string if not processed|safeThe filter may be escaped again by the template engine (about to<to&lt;The page directly displays HTML code instead of rendering effects. Therefore, it is essential torenderafter|safeto ensure that the HTML content is rendered correctly in the browser.

If you have other custom fields that store Markdown content and you want to render it as HTML, you can userenderFilter:

{# 假设这是一个名为'custom_markdown_field'的自定义字段 #}
{% archiveDetail customMarkdownField with name="custom_markdown_field" %}
{{ customMarkdownField|render|safe }}

Step two: Remove tags from the generated HTML

After converting Markdown to HTML, you may need to further modify the generated HTML content, such as removing certain HTML tags, or directly obtaining plain text. Anqi CMS provides two very practical filters to complete this task: striptagsandremovetags.

  1. Remove all HTML tags (striptags)If you need to completely remove all HTML tags, get the plain text form of the content (for example, for generating article summaries or SEO descriptions),striptagsThe filter is your preferred choice. It removes all HTML, XML tags, and HTML comments from the content.

    {# 假设'htmlContent'是Markdown转换后的HTML内容 #}
    {% set pureText = htmlContent|striptags %}
    <p>文章摘要:{{ pureText|truncatechars:100 }}</p>
    

    In this example,htmlContentafterstriptagsAfter filtering, all HTML tags are removed, leaving only plain text, then we usetruncatechars:100to extract the first 100 characters as a summary.

  2. to remove specific HTML tags (removetags)When you need to control more finely, only remove some specific tags from the HTML content (for example, keep paragraphs and headings but remove images and links),removetagsThe filter provides powerful capabilities. You can specify the tag names to be removed after the filter, separated by commas.

    {# 假设'htmlContent'是Markdown转换后的HTML内容 #}
    {% set filteredHtml = htmlContent|removetags:"img,a,script" %}
    <div class="filtered-content">{{ filteredHtml|safe }}</div>
    

    here,filteredHtmlWill be removed.<img>/<a>and<script>Content after the tag. Similarly, since the result is still HTML, it needs to be used again|safeFilter to ensure the browser renders correctly.

Comprehensive Practice: Convert Markdown to HTML and Remove Tags

Now, let's combine these two steps to see how to implement Markdown to HTML in the AnQiCMS template, and then remove the tags according to the requirements:

Assuming you have a Markdown-formatted article content, you want:

  1. Convert it to HTML.
  2. Remove all images in the HTML (<img>) tags.
  3. Remove all hyperlinks (<a>) tags.
  4. The final filtered HTML content will be displayed.
{%- archiveDetail articleContentMarkdown with name="Content" render=true %}
{# 先将Markdown渲染为HTML,并标记为safe,否则removetags无法识别HTML标签 #}
{% set renderedHtml = articleContentMarkdown|safe %}

{# 对渲染后的HTML进行标签移除 #}
{% set finalHtml = renderedHtml|removetags:"img,a" %}

{# 输出最终处理过的HTML内容 #}
<div class="article-body-no-images-links">
    {{ finalHtml|safe }}
</div>

By following these steps, your Markdown content is first rendered into standard HTML, then we applied to this HTML stringremovetagsThe filter removed all<img>and<a>The label. Finally, the content displayed on the page is HTML without images and links.

Summary

The Anqi CMS template engine combines Markdown rendering capabilities with flexible filters, providing great freedom for content display.Whether it is to accurately filter HTML tags to extract plain text summaries or to meet specific design requirements, these tools can easily help you achieve this at the template level. UnderstandingrenderThe parameter withrenderFilter,|safeas well as filtersstriptagsandremovetagsThe role and timing of use is the key to fully utilizing AnQiCMS for content operation.


Frequently Asked Questions (FAQ)

1. Why did I useremovetagsorstriptagsAfter that, HTML tags were not removed?This is usually because you forgot to add, before converting Markdown to HTML, orremovetags/striptagsadd|safeA filter. If the HTML content is not marked as “safe” (i.e. not processed|safe), the template engine may treat it as a plain string and automatically escape it, resulting inremovetagsorstriptagsThe filter cannot recognize and process the HTML tags contained within. Make sure the HTML content converted from Markdown is used|safe.

2. I want to get the plain text summary of Markdown content, **what is the practice??**Practice is to first convert Markdown content to HTML, then usestriptagsthe filter to remove all HTML tags, and then use it lasttruncatecharsFilter text to the required length. For example:

{% archiveDetail articleContent with name="Content" render=true %}
{% set summary = articleContent|safe|striptags|truncatechars:150 %}
<p>{{ summary }}</p>

This ensures that the abstract is based on the rendered HTML and does not contain any formatting tags, only retaining plain text and controlling the length.

3. If I want to preserve certain HTML tags (such as<strong>), but remove other tags (such as<img>), how should I operate?Markdown itself does not directly handle “

Related articles

Does AnQiCMS provide a feature to batch remove all HTML tags from all articles under a specific content model?

In website content operation, we often encounter situations where we need to uniformly process a large number of articles, such as for content standardization, SEO optimization needs, or preparing to export content to other platforms, we need to remove the HTML tags from the articles.AnQiCMS (AnQiCMS) is a powerful content management system that provides an efficient way to solve such problems.The Anqi CMS indeed has the ability to remove all HTML tags from all articles under the specified content model in bulk.

2025-11-08

How to remove HTML tags in AnQiCMS template based on conditions?

In the development and maintenance of website templates, we often encounter such needs: to determine whether a certain HTML element is displayed on the page based on specific data conditions.For example, an article only displays the image tag when there is a thumbnail or when a custom field has content, and renders the corresponding block.This "on-demand display" mechanism not only makes the template code cleaner, avoids the appearance of empty tags or unnecessary visual elements on the page, but also significantly improves user experience and page loading efficiency.

2025-11-08

What are the built-in cleanup tools of AnQiCMS when processing HTML content from external sources?

When processing HTML content collected from external sources, AnQiCMS (AnQiCMS) provides a series of built-in cleaning tools and strategies to help users efficiently manage and optimize content, ensuring the quality, safety, and display effect of website content.These features provide comprehensive support from the automated processing before the content is stored to the refined control during content display.

2025-11-08

How to use AnQiCMS filter to preliminarily sanitize the HTML content uploaded by users?

In website operation, the security and display effect of user uploaded content are crucial.Especially when users have the opportunity to submit content containing HTML tags, how to effectively perform preliminary purification, prevent malicious code injection (XSS attacks), and ensure that the content is presented as expected, is a challenge that each operator needs to face.AnQiCMS as a system focused on efficiency and security, provides us with a variety of tools to meet this need.

2025-11-08

Can the `striptags` filter retain images or links in HTML content, while removing only the formatting tags?

In website content operation, we often need to handle text content that contains HTML tags.Sometimes it needs to be converted to plain text, and sometimes it is desired to retain some key elements while removing redundant formats, such as images and links.AnQiCMS's template engine provides a rich set of filters to help us handle such needs, among which the `striptags` filter is a commonly used one.Then, can this `striptags` filter retain images or links in the content when removing HTML tags?in short

2025-11-08

How to remove whitespace tags from HTML without affecting content display in AnQiCMS?

Manage website content in Anqi CMS, we often pay attention to details, such as the blank tags in HTML code.These seemingly harmless empty tags can sometimes affect the rendering of the page, and even cause slight interference to search engine optimization (SEO).Although Anqi CMS does not have a direct function to "one-click clear blank HTML tags", we can cleverly utilize its powerful content management tools to achieve this goal without affecting the actual display of content.Understanding the trouble with empty tags Empty HTML tags usually refer to `<p></p>`

2025-11-08

How to use a filter to convert HTML content to plain text for word count or SEO analysis?

During the operation of a website, we often need to perform various analyses on the content we publish, among which word count and SEO analysis are crucial.However, rich text editors in content management systems (CMS) often add a large number of HTML tags to text. Although these tags provide rich visual effects when rendered on the front-end page, they often cause interference when performing word counts or when pure text is needed for SEO analysis.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that fully considers the needs of users

2025-11-08

What Markdown syntax features does the `render` parameter of AnQiCMS support when converting Markdown to HTML?

In AnQiCMS, the flexibility of content creation has always been our focus.With the introduction of the Markdown editor, content publishing has become more efficient and convenient.When we enter Markdown format content through the background editor, the system defaults to converting it into beautiful HTML for display on the website front end.The core of this conversion process is the important parameter `render` that we may use in template tags.

2025-11-08