How to correctly render Markdown content as HTML and display mathematical formulas and flowcharts in the Anqi CMS template?

Calendar 👁️ 72

The content is the soul of the website, and how to present these contents efficiently and beautifully is a topic that every operator cares about.In Anqi CMS, using Markdown to write content can not only greatly improve writing efficiency, but also easily achieve complex formatting needs, such as inserting mathematical formulas and drawing flowcharts. This is undoubtedly a powerful feature for technical blogs, educational websites, or corporate websites that need to display complex business processes.

Next, we will explore how to correctly render Markdown content as HTML in the Anqi CMS template, and elegantly display your mathematical formulas and flowcharts.

First, enable the Markdown editor

In Anqi CMS, enabling Markdown is the first step to achieving these cool effects.You just need to log in to the back end, go to the "Global Settings" under the "Content Settings" page, and here you can find and enable the Markdown editor.After completing this setting, you can directly use Markdown syntax to create articles, pages, and other content when publishing or editing.

Prepare the template for rendering

Markdown content, especially involving mathematical formulas and flowcharts, needs to be parsed and rendered on the front-end page through additional JavaScript libraries. The Anqi CMS template system is based on the Django template engine syntax, files are usually named with.htmlwith suffix, and stored uniformly in/templateIn the directory. You may have a file namedbase.htmlwhich contains the common header of the website (<head>) and the footer area. We will be working on the page's<head>Introduce necessary external resources within the tag to ensure that all pages can correctly render these special content.

  1. Add styles to Markdown content

    To make the HTML content rendered by Markdown have good readability and aesthetics, we can introduce a universal Markdown style library, such asgithub-markdown-cssThis style library can make your Markdown content display in a style similar to GitHub on the web.

    Inbase.htmlof<head>Add the following code inside the tag:

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
    
  2. Let mathematical formulas leap off the page.

    If you need to display complex mathematical formulas in content, MathJax is a very powerful solution that can render LaTeX, MathML, and other formats of mathematical expressions into high-quality typesetting effects.

    Similarly, inbase.htmlof<head>Add the MathJax CDN reference within the tag:

        <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    

    Now, you can use LaTeX syntax to write formulas in Markdown content, such as inline formulas$\alpha + \beta = \gamma$or a standalone formula block$$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$$They will all be correctly rendered by MathJax.

  3. Draw a clear flow chart.

    Flowcharts are an excellent way to demonstrate business logic and algorithm steps.Mermaid is a Markdown-based flowchart and diagram tool that allows you to create various charts using simple text syntax.

    Inbase.htmlof<head>Within the tag, add the Mermaid CDN reference and its initialization script immediately after MathJax:

        <script type="module">
            import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
            mermaid.initialize({ startOnLoad: true });
        </script>
    

    After configuration, use the Mermaid syntax block (for examplemermaid` ...`) to draw a flowchart in your Markdown content.

Display Markdown content correctly in the template

Now, when your backend and frontend templates are ready, let's focus on how to convert Markdown content into HTML that can be rendered by the browser on specific pages (such as article detail pages, single pages, etc.).

Template tags of Anqi CMS, such asarchiveDetail/categoryDetailorpageDetailUsed to obtain detailed information about different types of content. Where,ContentThe field usually carries Markdown formatted text.

By default, if you have enabled the Markdown editor in the background, the system is retrievingContentWhen a field is rendered, it will attempt to automatically convert Markdown to HTML. But for more precise control, or when you need to render Markdown in other custom fields, Anqi CMS provides two flexible methods:

  1. Userender=trueParameter (recommended for main content fields)When you callContentwhen a field is added, it can be annotatedrender=trueParameter, explicitly indicating the system to perform Markdown to HTML conversion. For example:

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

    HerearticleContentThe variable will contain HTML converted from Markdown.

  2. Use|renderFilter (for any variable containing Markdown)If your Markdown content is stored in a custom field, or if you want to render only specific content while globally disabling the Markdown editor, then|renderThe filter will be your helpful assistant. It can convert any variable containing Markdown syntax to HTML. For example, suppose you have a variable namedintroductionThe custom field contains Markdown:

    {% archiveDetail introduction with name="introduction" %}
    {{ introduction|render|safe }}
    

An important reminder:Whether it is usingrender=trueIs the parameter|renderFilter, the converted content is all in HTML format. To prevent the template engine from escaping HTML special characters again (for example, turning<to&lt;), resulting in the HTML code not being displayed correctly. You need to add it in the output so that the browser can recognize it as normal HTML structure and render it properly|safeFilter. In this way, the browser can recognize it as a normal HTML structure and render it.

Practice case: Display Markdown content on the page

Assuming you are editing a namedarticle_detail.htmlThe article detail page template. By combining the steps mentioned earlier, your template might look like this:

{# 假设这是您的文章详情页模板:/template/default/article_detail.html #}

{# 继承基础模板,基础模板中已引入必要的CDN资源 #}
{% extends 'base.html' %}

{% block title %}
    <title>{% tdk with name="Title" siteName=true %}</title>
{% endblock %}

{% block head_extra %}
    {# 如果您的CDN资源是直接在base.html中引入,这里可以省略。
       如果是在block中引入,确保其在<head>内。 #}
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <script type="module">
        import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
        mermaid.initialize({ startOnLoad: true });
    </script>
{% endblock %}

{% block content %}
<article class="markdown-body"> {# 添加 markdown-body 类以应用 GitHub Markdown 样式 #}
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="article-meta">
        <span>分类: <a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a></span>
        <span>发布日期: {% archiveDetail with name="CreatedTime" format="2006-01-02" %}</span>
        <span>浏览量: {% archiveDetail with name="Views" %}°</span>
    </div>

    <div class="article-content">
        {# 获取文章内容并确保渲染Markdown为HTML且安全输出 #}
        {% archiveDetail articleContent with name="Content" render=true %}
        {{ articleContent|safe }}
    </div>

    {# 如果您有自定义Markdown字段,例如一个叫 'supplementary_info' 的字段 #}
    {% archiveDetail supplementaryInfo with name="supplementary_info" %}
    {% if supplementaryInfo %}
    <div class="supplementary-section">
        <h2>补充信息</h2>
        {{ supplementaryInfo|render|safe }}
    </div>
    {% endif %}

</article>
{% endblock %}

In this code block,{% archiveDetail articleContent with name="Content" render=true %}The statement retrieves the Markdown format content from the current article,render=trueEnsure the parameter is converted to HTML then{{ articleContent|safe }}Output it securely to the page. While external CSS and JavaScript libraries will be responsible for styling the HTML content, rendering mathematical formulas, and flowcharts.

Some practical suggestions

  • Stability of CDN resources:The CDN link used in the above example (cdnjs.cloudflare.comandcdn.jsdelivr.netIt is usually stable and reliable, but in rare cases, network issues may affect the loading of resources.If your website has very high requirements for loading speed and stability, you can consider downloading these JavaScript and CSS files to the local server and distributing them through the static resource management function of Anqi CMS.
  • Content writing and testing:When writing mathematical formulas and flowcharts in Markdown, it is recommended that you first preview them in some online Markdown editors.

Related articles

How to use the `archiveDetail` tag to display images, view counts, and publishing time on the document detail page?

In website operation, the document detail page is the core area that attracts users and conveys information.A rich content, intuitive detail page, which can not only significantly improve user experience, but also has a positive push on search engine optimization (SEO).AnQiCMS provides a powerful `archiveDetail` tag that allows us to easily obtain and display various document information, such as images, view counts, and publication time.Next, we will explore how to skillfully use the `archiveDetail` tag in the document detail page

2025-11-07

How to accurately retrieve and display the content of a specified ID or URL alias on the document detail page?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides users with a rich set of tools to build and manage websites.During website operations, we often need to precisely display the detailed content of a document on a specific page, whether it is the document being viewed on the current page or specified by a particular identifier (such as ID or URL alias).The template tag system of Anqi CMS, especially the `archiveDetail` tag, is the key to meeting this requirement.

2025-11-07

How to display thumbnails, descriptions, and custom fields on the document list page?

In website operation, the content list page is a key link for users to obtain information and decide whether to delve deeper into browsing.A richly informative, well-structured list page that can greatly enhance user experience and content appeal.It is particularly important to display thumbnails, descriptions, and custom fields reasonably in the document list page of Anqi CMS to achieve this goal.Through the powerful and flexible template tag system provided by Anqi CMS, you can easily meet this requirement without complex programming knowledge, just need to make simple modifications and configurations to the template file.

2025-11-07

How to manage and display all documents in AnqiCMS and support filtering by title, model, and category?

In the era of explosive digital content, efficiently managing and displaying website documents is a core challenge for every operator.When you have a vast amount of articles, product descriptions, service instructions, or any form of site content, how to ensure that these contents are organized, easy to find, and can be filtered according to different needs, is directly related to user experience and operational efficiency.AnqiCMS, as a system specially designed for enterprise content management, provides a powerful and flexible solution in this aspect.

2025-11-07

How to use the `archiveList` tag to display a document list according to specified conditions (such as latest, most popular, recommended attributes)?

AnQiCMS is an efficient and flexible content management system that provides a variety of content display methods, among which the presentation of the document list is a very core part of website operation.We hope to dynamically display the latest articles, the most popular content, or carefully recommended high-quality documents on the homepage, category pages, or thematic aggregation pages according to different operational objectives.All of this can be easily achieved through the powerful `archiveList` tag of AnQiCMS

2025-11-07

How to implement a paginated document list display in `archiveList`?

When managing website content, we often encounter such situations: there are many articles, products, or news under a certain category. If all the content is piled on a single page, it not only causes the page to load slowly and affects user experience, but also makes it difficult for visitors to find what they need in a sea of information.At this point, introducing pagination becomes particularly important. Pagination can break a large amount of content into several small pages, allowing users to browse page by page, greatly enhancing the usability and access efficiency of the website.In AnQiCMS, implementing a document list display with pagination is very intuitive and flexible

2025-11-07

How to display the previous article and

In website content operation, the user experience of the document detail page is crucial.A well-designed page not only effectively displays content but also guides visitors to delve deeper into more related information.Among them, providing the "Previous" and "Next" navigation functions for users is a commonly used and efficient strategy to improve the internal link structure of the website, reduce the bounce rate, and optimize the user's browsing path.This not only helps users conveniently explore more content, but also conveys the relevance and depth of the website's content to search engines, which has a positive impact on SEO.

2025-11-07

How does AnQiCMS display the latest article list on the homepage?

The homepage is the first impression of visitors arriving at the website, and the timely update of the latest article list can effectively enhance the activity and user stickiness of the website.For users using AnQiCMS, displaying the latest list of published articles on the homepage is a basic and important feature, which can be easily achieved through the flexible template tags provided by the system.AnQiCMS with its efficient and customizable features provides users with an intuitive template system.You do not need to delve deeply into the complex backend code, just master its powerful template tag usage

2025-11-07