How to remove all or specified HTML tags from the HTML content rendered from Markdown?

Calendar 👁️ 70

When managing content in AnQi CMS, we often take advantage of the Markdown editor to conveniently write articles.The power of Markdown lies in its ability to convert simple plain text format into rich HTML structure, which brings great convenience to the styling and expressiveness of content.But sometimes, we do not need or want these HTML tags to be completely displayed on the final page.For example, we may only want to extract the plain text summary of the article, for use in the home page list display, SEO description, or in other scenarios where a specific format is required, we hope to remove certain specific tags.

Fortunately, the template system of Anqi CMS provides a very flexible and powerful Filters feature, which can help us easily remove all or specified HTML tags from HTML content rendered from Markdown.

Understanding the background of content rendering and tag removal

After entering content through the Markdown editor and saving it, Anqi CMS will convert it to HTML format in the background for storage or perform real-time conversion during template rendering. This means that even if you enter Markdown syntax, what is usually displayed on the page is usually includedp/h1/strong/em/a/imgHTML content of tags.

Our goal is to 'clean' the HTML content when outputting it in the template, selectively removing tags according to different requirements.

Remove all HTML tags: usestriptagsFilter

Suppose we want to extract pure text from the full content of an article, without any bold, italic, links, images, and other HTML elements. At this point,striptagsThe filter is our good helper.

When we get the article content field in the template (for examplearchive.Content,it is usually the HTML content rendered by Markdown)when it is simply appended with|striptagsThe filter will automatically strip all HTML tags from the content, leaving only plain text.

The Anqi CMS template system escapes HTML tags by default when outputting content for safety (such as converting <becomes&lt;),to prevent potential XSS attacks. When we need to manipulate actual HTML structures, such as removing tags, we usually append to the content variable.|safeThe filter tells the system that this content is safe HTML, it does not need to be escaped, and can be operated on directly. Therefore, the common usage would be:

{{ archive.Content|safe|striptags }}

Thus,striptagsIt can handle real HTML tags and return plain text.

Remove specific HTML tags: usingremovetagsFilter

Sometimes, we may not want to remove all tags, but selectively retain some tags, removing only specific tags. For example, we may want to retain paragraph tags<p>and newline tags<br>But remove all image tags<img>, link tags<a>And bold tags<strong>.

removetagsThe filter can accurately delete the specified HTML tags. Just specify the name of the tag to be removed (without brackets)

Related articles

How to safely truncate a Markdown-rendered HTML content by words?

In content operation, we often need to display a brief version of the content on list pages, aggregation pages, or article summary areas.This not only optimizes the page layout and improves user experience, but also helps search engines better understand the content theme to some extent.However, when content is written in Markdown format and finally rendered as HTML, if you need to truncate it, you may encounter some challenges.It is easy to truncate HTML content by characters or bytes, which can easily lead to incomplete tags, disordered page structure, and even display errors.

2025-11-08

How does the `truncatechars_html` filter precisely control the character truncation length of HTML content?

In website operation, how to effectively display content is an eternal topic.We hope users can quickly browse information and also be attracted by the精彩的 abstract, and then click to view the full text.However, when the original content is long and contains complex HTML structures, how to elegantly reduce it has become a challenge that template designers and content operators often encounter.Bluntly cutting a segment of text with HTML tags by character count may destroy the original HTML structure.

2025-11-08

How to extract the HTML content rendered from Markdown without destroying the tag structure?

In content operation, we often encounter such needs: on a list page of articles or a special topic page, it is necessary to display the summary content of the articles.These articles are usually written using a Markdown editor, which may contain images, links, bold text, and other rich HTML structures.If simply truncating the HTML string rendered by Markdown, it often breaks the original tag structure, causing the page layout to become chaotic, even resulting in unclosed tags, which seriously affects the user experience.AnQi CMS as an efficient

2025-11-08

How to automatically generate an article table of contents (TOC) based on Markdown content?

How to effectively organize the structure of long articles while managing website content with Anqi CMS, which is a worthy issue to pay attention to.Automatically generate an article table of contents (Table of Contents, abbreviated as TOC) is a very practical solution.It not only allows readers to quickly understand the outline of the article, but also makes it convenient for them to jump to the parts of interest, while also helping search engines better understand the structure of the article.

2025-11-08

How to convert newline characters to `<br/>` in Markdown rendered plain text content?

In website content management, we often encounter such a situation: after hard work in the background editor, we press the enter key between each line of text, hoping that they will maintain the same line break effect on the front page.However, after the content was published, it was found that all the line breaks had disappeared and the text was squeezed into a ball.This is because web browsers default to treating consecutive newline characters as a single space and do not automatically convert them into visually apparent line breaks.For friends using AnQiCMS, solving this problem is actually very simple and elegant

2025-11-08

How does the `urlize` filter automatically recognize and beautify URL links in Markdown content?

In daily content creation and website operation, we often need to cite external resources or provide links to more information.It takes time and is prone to errors to manually convert these links into clickable hyperlinks, especially when dealing with large amounts of content or Markdown-formatted text.AnQiCMS (AnQiCMS) understands the pain points of content operation, providing us with an elegant solution through its powerful template filter function - the `urlize` filter, which can automatically identify and beautify URL links in Markdown content.

2025-11-08

How to shorten the display text of a URL link in Markdown while keeping it clickable?

In content creation, we often insert various links, whether referencing external materials or pointing to related pages within the site.Especially in Markdown format, if the complete URL is displayed directly, it often appears long, occupying a lot of screen space, seriously affecting the overall beauty and readability of the article.This is a problem that cannot be ignored for websites that pursue high-quality content presentation.Imagine when a user reads a detailed article and encounters a long string of unprocessed links, it not only breaks the rhythm of reading but may also make the page look disorganized

2025-11-08

How to prevent malicious script (XSS) injection after Markdown content is rendered into HTML?

In daily content creation, Markdown is favored by content operators for its concise and efficient syntax.It allows us to focus on the content itself without paying too much attention to the complex layout details.However, when we render Markdown content into HTML and present it on the website, a potential security risk——cross-site scripting (XSS) emerges.Effectively prevent XSS attacks is the key to ensuring website security and maintaining user trust.### Understanding Markdown Rendering and XSS

2025-11-08