When using AnQiCMS for website content operation, we often hope that the URL addresses in articles, product descriptions, or other text content can be automatically recognized and converted into clickable links. This not only improves user experience but also helps in the dissemination of content and is friendly to search engines.AnQiCMS as an efficient and SEO-optimized content management system, of course, takes this need into consideration and provides a very convenient implementation method.This is mainly due to its flexible template engine and built-in filter functions.

To implement automatic URL parsing in article content, there is no need to manually edit published articles or install additional plugins. Just a slight adjustment to the template is required. The core lies in using the AnQiCMS template engine providedurlizeandsafeThese two filters.

urlizeThe filter is a tool specifically designed to identify URL addresses within plain text content. It can intelligently recognize various URL formats within the article content (such ashttp://www.example.com/https://www.example.comevenwww.example.comForms URL and email addresses), then automatically wrap them with HTML tags. According to the AnQiCMS document instructions,<a>tags to make them truly clickable links.urlizeWhen converting links, it will also automatically increaserel="nofollow"Properties, this helps to avoid SEO weight loss and tells search engines that these external links should not be followed. This is a good guarantee for the SEO health of the website.

whilesafeThe filter plays the role of a guardian.urlizeThe filter generates HTML code, in order to ensure that these codes can be correctly parsed by the browser rather than displayed as plain text strings, we must use in conjunction withsafeFilter.safeThe filter informs the template engine that the output content is safe HTML and does not require escaping.

In practical applications, we usually apply these two filters to the article content field in the template file for displaying article details. For example, if your article content is stored inarchive.ContentField within, and througharchiveDetailObtaining and displaying tags, you can do it like this:

{# 假设您正在展示文章详情,文章内容通常由archive.Content字段提供 #}
{% archiveDetail articleContent with name="Content" %}
<div class="article-body">
    {{ articleContent|urlize|safe }}
</div>
{% endarchiveDetail %}

In this code,{{ articleContent|urlize|safe }}The function is: first,articleContentThe plain text URL in will beurlizeIdentified and converted by the filter<a>Labelled HTML links; then,safeThe filter ensures that the generated HTML code is rendered correctly by the browser.

Moreover, when articles contain some particularly long URL addresses, if they are displayed in full, it may affect the aesthetics and layout of the page, AnQiCMS also providesurlizetruncFilter. This filter allows us to set a truncation length while converting links, and the excess part will be replaced with an ellipsis....Replace it, keep the page tidy. For example, if you want the link text to display a maximum of 15 characters, you can use it like this:

{# 使用urlizetrunc过滤器截断长链接文本 #}
{% archiveDetail articleContent with name="Content" %}
<div class="article-body">
    {{ articleContent|urlizetrunc:15|safe }}
</div>
{% endarchiveDetail %}

This automatic parsing feature is not only applicable to article content (ContentField), any text field that may contain a URL, such as an article summaryDescription, custom text fields, can be applied in a similar mannerurlizeorurlizetruncFilter to automatically convert URLs.

It is worth noting that,urlizeandurlizetruncThe filter primarily targets the raw text URLs that have not been formatted in the article. If you have manually inserted links using the rich text editor's link button while editing the article, or have used Markdown syntax (such as[点击这里](https://en.anqicms.com)To create a link, these links themselves already have clickable capabilities and do not require any further action.urlizeorurlizetruncFilter processing. The "anchor text" feature and "full site content replacement" feature provided by AnQiCMS are designed for on-site SEO and batch content management. They focus on global replacement and optimization of specific keywords or links.urlizeThe filter has different immediate conversion for plain text URLs in the article display layer.

Through this simple and powerful way, AnQiCMS enables website operators to easily convert URLs in article content automatically, significantly improving the user experience of the website and avoiding the trouble of users manually copying and pasting links.At the same time, this also indirectly promotes the accessibility of content and the understanding of resources mentioned in the content by search engines, bringing a positive impact on the overall performance of the website.


Common Questions (FAQ)

  1. Q: If I have manually added links in the article editor,urlizeFilter will it also process them? Answer:No.urlizeThe filter mainly targets pure text URL addresses. If you have manually added links to the text through a rich text editor or Markdown syntax, these links are already HTML<a>tags,urlizeThe filter will skip them, without repeating or changing their structure.
  2. Question: Do the links automatically parsed containrel="nofollow"properties, which may affect SEO? Answer:Yes, according to the AnQiCMS filter description,urlizethe filter will automatically addrel="nofollow"Property.This is generally considered to be beneficial for SEO, as it informs search engines that these external links should not pass any weight, helping to concentrate the SEO weight of your website on internal links and avoid unnecessary external weight loss.
  3. 问:Can I control to automatically parse only certain URLs and not others? Answer: urlizeThe filter is processed at the template level, default is to convert all matched plain text URLs. If you need to be very