How to extract and display the list of hierarchical titles (H1-H6) in the AnQiCMS document content?

Calendar 👁️ 68

Easily navigate: How AnQiCMS intelligently extracts and displays document分级 titles (H1-H6)

As an experienced website operations expert, I am well aware of the importance of content structure for user experience and search engine optimization.When dealing with a large amount of document content, it is crucial to clearly understand and efficiently utilize the hierarchical title structure (H1-H6) within the document to enhance the value of the website content.AnQiCMS (AnQiCMS) is an efficient enterprise-level content management system that provides very intuitive and powerful functions in this aspect.Today, let's delve deeply into how AnQiCMS helps us extract and display hierarchical titles from document content.

Understand AnQiCMS content structure and Markdown

Firstly, we need to understand that AnQiCMS manages content based on its flexible content model and supports a powerful Markdown editor. This means that when we write articles, product introductions, single-page content, and so on in the background, we can directly use Markdown syntax to define the title levels, for example, using a#Represents H1,##Represents H2, etc., up to######Represents H6.

When this Markdown formatted content is saved to the "Document" (Archive) of AnQiCMS, the system will automatically parse these contents and structure them.And it is this structured processing that provides us with the convenience of extracting and utilizing hierarchical titles.

Core tool:archiveDetailwith the tag andContentTitlesfield

AnQiCMS provides a very practical template tagarchiveDetailIt allows us to get the detailed information of the current or specified document. In the data returned by this tag, there is a field specially designed for extracting hierarchical titles, that is,ContentTitles.

ContentTitlesThe field does not directly return a string, but an array object containing all the document titles. Each element of this array represents a title in the document and provides the following key information:

  • Title: The text content of the title.
  • Tag: The corresponding HTML tag of the title, for example.h1,h2,h3etc.
  • Level: The level number of the title, for example H1 corresponds to 1, H2 corresponds to 2.
  • Prefix: Prefix before the title, such as the automatically generated chapter number.

With this information, we can easily build a dynamic document catalog or sidebar navigation in the template.

For example, to display the table of contents on the sidebar of the document detail page, we can use it like thisarchiveDetailTags andContentTitlesField:

{# 首先,我们使用 archiveDetail 标签获取当前文档的所有标题信息,并将其赋值给一个名为 contentTitles 的变量 #}
{% archiveDetail contentTitles with name="ContentTitles" %}

{# 接下来,我们判断 contentTitles 变量是否包含任何标题,如果有,则开始构建目录 #}
{% if contentTitles %}
    <aside class="document-toc">
        <h3>目录</h3>
        <nav>
            <ul>
                {# 循环遍历 contentTitles 数组中的每一个标题项 #}
                {% for item in contentTitles %}
                    {# 根据标题的层级(item.Level)添加不同的 CSS 类或缩进,以便在视觉上区分标题层级 #}
                    <li class="toc-item level-{{ item.Level }}">
                        {# 标题前缀(如果有的话)和标题文本 #}
                        <a href="#{{ item.Title | urlencode }}">{{ item.Prefix }} {{ item.Title }}</a>
                    </li>
                {% endfor %}
            </ul>
        </nav>
    </aside>
{% endif %}
{# 结束 archiveDetail 标签的调用 #}
{% endarchiveDetail %}

In this code, we have cleverly useditem.Levelto add each table of contents itemlevel-Xthe CSS class, so you can control the style and indentation of different level titles through CSS, making the directory structure clear at a glance. At the same time, we also created a link to#{{ item.Title | urlencode }}The link, this usually requires cooperation with front-end JavaScript to achieve smooth scrolling to the corresponding title on the page (usually we add H tags to the document content)id="{{ item.Title | urlencode }}"Property).

Practical Application: Build Dynamic Document Index

This ability to extract hierarchical titles significantly enhances user reading experience and website content management efficiency.

  1. Automatically generate page directory (Table of Contents, TOC)As shown in the above example, we can according toContentTitlesDynamically generated document internal navigation directory. Users do not need to scroll through long arguments to quickly locate the chapters of interest, greatly improving the readability and accessibility of the content.
  2. Enhanced content navigationIn some long technical documents or tutorials, a clear table of contents can help users better understand the document structure and quickly navigate to relevant content, reducing the cost of searching.
  3. Support SEO optimization: Structured title content is inherently search engine friendly. ThroughContentTitlesThe title data obtained can be used as a basis for generating anchor links within the page, which helps search engines better understand the structure of the page content, and may even be displayed as a "Featured Summary" or "Quick Link" in search results, improving click-through rate.

Optimization and expansion: Improve document readability

In addition to the basic directory construction, we can also further utilizeContentTitlesMore advanced optimization of the data provided by the field:

  • Sidebar "current position" highlighted: Combine with front-end JavaScript, you can listen to user scroll events, and dynamically highlight the corresponding title in the sidebar directory when the user reads a certain chapter, providing real-time feedback to the user.
  • Customization of styles for different level titles: Passitem.Tagoritem.LevelWe can apply different font sizes, colors, or icons to H1, H2, H3, and other hierarchical titles to more clearly present the logical hierarchy of the document.
  • Content Analysis and Quality Inspection:Even operation personnel can preview the title structure before content is published, check if there are any problems such as chaotic, missing, or excessively long title levels, and optimize it before publishing.

In summary, AnQiCMS'sarchiveDetailLabel combinationContentTitlesThe field provides a powerful and flexible tool for content operators, allowing deep mining of the structural value of document content, not only enhancing the user reading experience but also adding to the website's SEO performance.By transforming technical information into practical content operation strategies, AnQiCMS truly empowers small and medium-sized enterprises and content operation teams, making complex content management more efficient and intelligent.


Frequently Asked Questions (FAQ)

1. If my Markdown document content is not published as "Document" (Archive) in the AnQiCMS backend, but is published directly on the server's.mdCan the file extract the title?

Answer:archiveDetailThe tag is designed to retrieve the 'Document' (Archive) content managed by AnQiCMS backend. If your Markdown file is only stored in the server file system and has not been imported or created as a specific document through the AnQiCMS backend, then the AnQiCMS template tags cannot directly parse these external.mdExtract the title from a file. In this case, you may need to go through custom development (for example, writing a Go program to parse Markdown files and store them in a database, or loading and parsing Markdown file content through JavaScript on the front end), or to these.mdImport the file into the AnQiCMS backend to use it as a 'document' and take advantage of its built-in features.

2.ContentTitlesThe extracted title list, does it support applying custom CSS styles to directory items?

Answer: Of course it does. As shown in the article example,ContentTitlesThe field provides for each title,TagandLevelInformation. You can use it according toitem.Leveloritem.Tagdynamically add different CSS classes (such asclass="toc-item level-1"or `class="toc-

Related articles

How to correctly display the rich text content of AnQiCMS documents and support lazy loading of images?

As an experienced website operations expert, I am willing to delve into the strategy of rich text content display and image lazy loading in AnQiCMS.In today's increasingly rich digital content, efficiently and beautifully presenting content while considering user experience and website performance is a focus for every operator.AnQiCMS is an efficient CMS based on the Go language, providing powerful tools to achieve these goals.

2025-11-06

How to generate a permanent link for documents in the AnQiCMS template?

## How to generate a permanent link for documents in AnQiCMS template: A comprehensive guide from backend configuration to frontend display In the operation of a website, a clear, easy-to-understand, and SEO-friendly permanent link (Permalink) is invaluable.It not only improves user experience, allowing visitors to identify page content at a glance, but is also the foundation of website SEO optimization.A stable and semantically meaningful URL structure can effectively help search engines crawl and understand your content, thereby affecting the ranking performance of the website.AnQiCMS knows this

2025-11-06

How to customize the SEO title and description of documents in AnQiCMS?

## The Precision Operation Tool: How does AnQi CMS customize the SEO title and description of documents?In today's highly competitive online environment, whether a website's content can be efficiently indexed by search engines and attract user clicks largely depends on its display effect on the search results page.This plays a crucial role in SEO titles and descriptions.As an experienced website operations expert, I know that every detail can affect the traffic and conversion of the website

2025-11-06

How to call and display the article title on the AnQiCMS document detail page?

As an experienced website operations expert, I have accumulated rich experience in the practice of content management systems (CMS), especially with the powerful functions and flexible application of AnQiCMS (AnQiCMS).Today, let's delve deeply into a seemingly basic but crucial issue in website operation: how to elegantly and accurately call and display the article title on the document detail page of AnQiCMS.This is not only about the basic content presentation of the page, but also directly affects the user experience and SEO optimization effect.

2025-11-06

How to get the ID and name of the current document category?

In the daily operation of AnQi CMS, content classification is a key element for organizing information, enhancing user experience, and optimizing SEO.Whether it is to generate breadcrumb navigation for articles, or to display other content under the current category in the sidebar, or to set dynamic titles for category pages, obtaining the ID and name of the current document's category is a common requirement in template development.The Anqi CMS provides intuitive and powerful template tags, allowing you to easily call this information on the front-end page.

2025-11-06

How to call and display the views (Views) of Anqi CMS documents?

As an experienced website operations expert, I know that every data indicator contains a profound insight into the performance of website content and user behavior.In AnQiCMS (AnQiCMS) such an efficient and practical content management system, the flexible invocation and display of core data such as 'Document Views' is an indispensable ability for content operators.This not only helps us understand the content popularity intuitively, but is also an important basis for optimizing content strategy and improving user experience.

2025-11-06

How to set and call the recommended attribute (Flag) of the document in AnQiCMS?

## AnQiCMS High-efficiency Operation Manual: Mastering the Settings and Calls of Document Recommendation Attributes (Flag) As an experienced website operations expert, I am well aware that how to make your high-quality content stand out in the vast sea of digital content and accurately reach your target users is the key to success.AnQiCMS as a powerful content management system, its built-in "Flag" feature is a powerful tool to help content achieve fine-grained operation.It can not only help you flexibly organize and display content, but also effectively enhance the exposure and user perception of the content. Today

2025-11-06

How does Anqi CMS display the cover image and thumbnail of the document?

As an experienced website operations expert, I know the importance of a high-quality cover image and a proper thumbnail for the attractiveness of the website, user experience, and even search engine optimization (SEO).AnQiCMS (AnQiCMS) deeply understands this, and its design in image processing and display is not only powerful in function but also takes into account the convenience of operation and flexibility of display. Let's delve into how Anqi CMS cleverly displays document cover images and thumbnails.

2025-11-06