How to correctly render Markdown content (including formulas, flowcharts) as HTML display in AnQiCMS?

Calendar 👁️ 94

AnQiCMS has always been committed to providing efficient and flexible solutions in content display. For the increasingly popular Markdown format in modern content creation, especially for complex content that includes mathematical formulas and flowcharts, AnQiCMS also provides a comprehensive support mechanism to ensure that these contents are correctly parsed and beautifully presented on the web.

To implement the correct rendering of Markdown format (including formulas and flowcharts) content, it mainly involves two aspects: Firstly, the internal HTML conversion of Markdown text in the AnQiCMS system, and secondly, the client browser further parses and renders these special elements (such as mathematical formulas and flowcharts) by introducing specific JavaScript libraries.

First step: Make sure the Markdown editor is enabled

Firstly, we need to ensure that the Markdown editor function of AnQiCMS backend is enabled.This is the foundation of content operation.You can log in to the AnQiCMS backend and find the 'Content Settings' option under 'Global Settings.'In here, there is usually an option to 'Enable Markdown Editor', make sure it is checked.After turning on, you can use Markdown syntax to create content when creating or editing documents (articles, products, single pages, etc.).

Second step: Markdown to HTML conversion within AnQiCMS

When you enter content in the Markdown editor on the AnQiCMS backend and save it, the system is responsible for converting this Markdown text into standard HTML structure when the content is published. This means that titles like#)、list(-), link ([]())、image(”)et cetera basic Markdown syntax, AnQiCMS will automatically process and output the corresponding HTML tags.

For document content fields, for examplearchive.Content/category.Contentorpage.ContentThe template engine of AnQiCMS provides|renderA filter specifically used to ensure that content is correctly parsed as HTML when output. Although the system usually automatically handles most Markdown conversions after enabling the Markdown editor, it is explicitly used in certain custom fields or specific scenarios|render|safeThe filter ensures that the content is correctly parsed and safely output.|safeThe filter here is used to inform the template engine that the output HTML content is safe and does not require further HTML entity escaping.

For example, in your template file (such asdetail.html), you might call the document content in this way:

<div>
    {{ archive.Content|render|safe }}
</div>

Or for custom fields:

<div>
    {{ params.introduction.Value|render|safe }}
</div>

Step 3: Client-side script support for rendering special elements

Although AnQiCMS has already converted Markdown to HTML, special markers such as mathematical formulas (such as LaTeX syntax) and flowcharts (such as Mermaid syntax) still need to be further parsed and rendered with front-end JavaScript libraries to be displayed normally in the browser as expected visual effects.These libraries usually need to be manually imported into the template files of the website.

AnQiCMS template files are based on the Django template engine syntax, usually there will be onebase.htmlOr similar public template files, which carry the common header, footer, and resources that need to be globally introduced on the website.We will add the required JavaScript and CSS references here.

  1. Markdown default style:To make the rendered Markdown content more visually consistent and beautiful, you can introduce a set of GitHub-style Markdown styles.This is done by adding an external CSS file.base.htmlthe file<head>Within the area, add the following code:

    <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. Correct display of mathematical formulas (via MathJax):Mathematical formulas are usually written in LaTeX or AsciiMath syntax, for example$$ E=mc^2 $$。MathJax is a powerful JavaScript rendering engine used to display mathematics in web browsers. In yourbase.htmlthe file<head>Within the area, add the following script:

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

    After the configuration is complete, for example, the mathematical formulas you write in Markdown content:$$ \int_a^b f(x) dx $$or inline formulas$\alpha + \beta = \gamma$All will be rendered as beautiful mathematical symbols in the browser by MathJax.

  3. The correct display of flowcharts (via Mermaid):Process diagrams usually use a simple text syntax like Mermaid to describe, for example:

    graph TD
        A[Start] --> B{Operation};
        B -- Yes --> C[Result 1];
        B -- No --> D[Result 2];
        C --> E[End];
        D --> E;

    Mermaid is a JavaScript-based library that can generate charts and flowcharts from text. In yourbase.htmlthe file<head>Within the area, add the following script:

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

    When you insert the code block marked with "`" in Markdown contentmermaidThe Mermaid library will recognize and render it as an interactive flowchart.

An example of Markdown syntax when creating content

In the AnQiCMS backend Markdown editor, you can write content that includes formulas and flowcharts in the following way:

  • Inline formula:Use a single dollar sign to enclose, for example这是行内公式 $E=mc^2$ 。
  • Block-level formula:Use double dollar signs to enclose, displayed on a separate line, for example:
    
    $$
    \sum_{i=1}^n i = \frac{n(n+1)}{2}
    $$
    
  • Flowchart:Usemermaid` 代码块包裹 Mermaid 语法,例如:
    graph LR
        A[Submit document] --> B{Approved?};
        B -- Yes --> C[Publish];
        B -- No --> D[Return for modification];
        C --> E[Update website];
        D --> A;
    ""

By following these steps, AnQiCMS combines the power of client-side scripts to perfectly present your Markdown content (including complex mathematical formulas and flowcharts) on the web page, providing readers with a richer visual experience and clearer information delivery.


Frequently Asked Questions (FAQ)

1. Why does AnQiCMS already support Markdown, and do I still need to manually introduce external CDN scripts to display formulas and flowcharts?AnQiCMS is responsible for converting Markdown text (including$$...$$or “`mermaidThis special mark) converts to the basic HTML structure.However, these special markers are not standard HTML tags, and browsers cannot directly understand and render them into complex formula graphics or flowcharts.Therefore, we need to introduce client-side JavaScript libraries such as MathJax and Mermaid.These libraries will scan the specific HTML structure (converted by AnQiCMS) in the page after the browser loads, and then dynamically parse and render it into the final visual effect.AnQiCMS is responsible for content management and initial conversion, while these external libraries are responsible for advanced client-side rendering.

2.I have followed the steps to introduce the script and enabled the Markdown editor, but the formula or flowchart is still displayed as plain text, or an error occurs on the page. How should I troubleshoot?First, please check yourbase.htmlIs the CDN link included in the file complete and correct, ensuring there are no typing errors. Next, confirm that the script is placed in<head>the tag andmermaidOf the scripttype="module"the attributes as wellmermaid.initialize({ startOnLoad: true });The code is complete. Please double-check the formula (such as$$...$$) or flowchart (such asmermaid ...Does the syntax conform to MathJax or Mermaid standards.The wrong syntax will cause them not to be recognized correctly.Finally, check the browser developer tools (F12) console for any error messages, as this usually helps you locate whether the script failed to load, there are syntax errors, or other issues.|render|safeA filter to ensure that the HTML content converted by AnQiCMS can be processed correctly by the browser and allows script execution.

3. Can I use locally deployed MathJax and Mermaid libraries instead of loading through CDN links?Absolutely. CDN links provide a convenient and efficient way to introduce, but if you consider factors such as website loading speed, security, or offline access, you can also choose to download the MathJax and Mermaid libraries to your server and then modifybase.htmlof<script>and<link>Label itsrcorhrefThe attribute points to the path of your local deployment. For example, if you place the library file in/public/static/js/directory, you can modify it as follows:

<script id="MathJax-script" async src="/static/js/mathjax/tex-mml-chtml.js"></script>
<script type="module">
    import mermaid from '/static/js/mermaid/mermaid.esm.min.mjs';
    mermaid.initialize({ startOnLoad: true });
</script>

Please note that you need to manage the updates and maintenance of these library files yourself for local deployment.

Related articles

How do custom fields of the content model (such as radio, checkbox, text) display on the front-end page?

AnQi CMS: Let the custom content model fields shine in the front-end When using AnQi CMS to manage website content, we often encounter such needs: the standard content structure can no longer fully meet the display of specific information.For example, a product page may need to display its "color options", "size range", or "stock status", etc.At this time, the flexible content model and custom field function of Anqi CMS are particularly important, as it allows us to expand content attributes based on the uniqueness of our business.

2025-11-08

How to dynamically display content on the AnQiCMS document list page through filtering conditions?

How to make users more efficiently find and browse content in website operation is the key to improving user experience and website value.Especially for document list pages, if a flexible filtering function can be provided, allowing users to dynamically adjust the display of content according to their needs, it will undoubtedly greatly enhance the interactivity and usability of the website.AnQiCMS with its flexible content model and powerful template tag features can easily meet this requirement.

2025-11-08

How does the inheritance mechanism of the category template affect the content display of the subcategory page?

The inheritance mechanism of AnQi CMS category templates: How to balance the consistency of page content and personalized displayAnQi CMS deeply understands this, and its inheritance mechanism of classification templates is specifically designed to help us cleverly solve this problem.

2025-11-08

How to configure and display custom Banner images and thumbnails for the AnQiCMS category page?

AnQiCMS (AnQiCMS) provides us with very flexible content management capabilities, one very practical feature is that it can configure and display custom Banner images and thumbnails for category pages.This not only makes our website look more professional and beautiful, but also effectively improves the user experience and the efficiency of content organization.Why does the category page need a custom Banner image and thumbnail?

2025-11-08

How to display a list of user-submitted comments on a website and handle the review status?

In website operation, user comments are an important element for building a community, obtaining feedback, and enhancing content interaction.For friends who use AnQiCMS (AnQiCMS) to build and manage websites, efficiently displaying user comments and managing their review status is a key factor in ensuring the quality of website content and user experience.The AnQiCMS provides very flexible and practical functions for this.

2025-11-08

How to display the user submitted comment form and its custom fields in AnQiCMS template?

In website operation, collecting user feedback and inquiries is an important link to improve service quality and understand user needs.AnQiCMS (AnQiCMS) provides a flexible and powerful message management function, allowing you to easily display a message form in the front-end template and support custom fields to meet the information collection needs of different business scenarios.To implement dynamic display of user submitted comment forms and custom fields in AnQiCMS templates, we mainly use a core tag: `guestbook`

2025-11-08

How to display the friend link list on the AnQiCMS front-end page?

Friendship links are a way for websites to recommend each other, not only can they increase the number of external links to the website, but also be beneficial for search engine optimization (SEO), but also provide visitors with more entry points to related resources, enhancing the user experience.AnQiCMS provides a very convenient function for us to manage and display friend links. Next, we will learn together how to set up friend links in AnQiCMS and display them on the website's frontend page.### One, manage the friendship link in AnQiCMS background In AnQiCMS, managing friendship links is very intuitive

2025-11-08

How to use AnQi CMS to realize personalized display of different content models (such as articles, products)?

AnQi CMS is an efficient and customizable content management system, one of its core advantages is the flexible content model.This provides website operators with great freedom, allowing us to design unique structures and display methods for various content types such as 'articles', 'products', etc., thus realizing the personalized presentation of website content.Next, we will delve into how to take advantage of the powerful functions of AnQi CMS to achieve personalized display of content models.Understanding the content model of Anqi CMS

2025-11-08