How to render Markdown content into HTML in AnQiCMS?

Calendar 👁️ 84

In a content management system, Markdown is a lightweight markup language that allows content creators to write in plain text format and use simple symbols for formatting, which can be easily converted into structured HTML.For AnQiCMS users, using Markdown to write content can not only improve efficiency, but also ensure consistency and maintainability of the content format.

How do we render Markdown content into beautiful HTML pages in AnQiCMS?This involves several key steps and some practical skills.

Step 1: Enable Markdown editor support in the background

Firstly, to let AnQiCMS recognize and process Markdown content, we need to make corresponding settings in the background.Please go to the administration interface of AnQiCMS, find the 'Content Settings' option under 'Global Settings'.Here, you will see an option named 'Enable Markdown Editor'.Check and save this setting, the content editing interface of AnQiCMS will support Markdown syntax, which means you can directly use Markdown to write in the editor for articles, products, pages, and other content.

When you enter Markdown text in content fields (such as article details, category descriptions, single-page content, Tag content, etc.), AnQiCMS will automatically convert it to HTML format when rendering the front-end page by default.This means that in most cases, you do not need to perform any additional operations, the system will intelligently convert your Markdown markup into HTML tags that the browser can recognize.|safeFilter. For example,{{archive.Content|safe}}The article content will be safely rendered as HTML.

Step 2: Enhance Markdown Rendering (optional but recommended)

Although AnQiCMS defaults to converting Markdown to HTML, in order to provide a richer visual experience, especially when the content includes code, mathematical formulas, or flowcharts, we may need to use some front-end libraries to further beautify or enable rendering of specific features.These enhanced features are usually implemented by introducing external CSS and JavaScript libraries in the template file.

1. Optimize Markdown Style: GitHub Markdown CSS

If you want your Markdown content to have a concise and professional style on the web like GitHub, you can introducegithub-markdown-cssThis will make your Markdown converted HTML page look more beautiful and easy to read.

You can find the public template files of the website (usuallybase.html)的<head>Add the following CSS reference 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" />

After adding, you may also need to add a class name to the parent element containing the Markdown content:markdown-bodyto apply these styles.

2. Display mathematical formula: MathJax

For content that needs to display complex mathematical formulas, AnQiCMS combined with the MathJax library can perfectly solve it.Just include the MathJax JavaScript script in the template, and your LaTeX or AsciiMath formatted formulas can be rendered as clear mathematical expressions.

Similarly, inbase.htmlthe file<head>Add the following script inside the tag:

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

After that, use the MathJax syntax supported in your Markdown content (such as$$...$$or$....$)to do so.

3. Draw a flowchart: Mermaid

If you want to create flowcharts, sequence diagrams, or other charts directly in Markdown content, Mermaid is an excellent choice.AnQiCMS integrates the Mermaid library, allowing you to directly generate these charts through text descriptions.

Inbase.htmlat the bottom of the file (</body>Before the label, or in<head>asdeferAdd the following Mermaid script:

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

Ensuremermaid.initialize({ startOnLoad: true });Called to automatically render the chart when the page is loaded. In Markdown content, you can use Mermaid syntax to create charts, for example:

```mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;

### 第三步:使用 `|render` 过滤器进行精细控制

AnQiCMS 模板引擎提供了一个名为 `|render` 的过滤器,它赋予我们更细致地控制 Markdown 到 HTML 转换的能力。虽然对于文章、页面等的 `Content` 字段,开启 Markdown 编辑器后会默认自动渲染,但对于一些自定义字段或其他并非默认 Markdown 类型的文本,`|render` 过滤器就显得尤为重要。

例如,如果您创建了一个名为 `introduction` 的自定义字段,并在其中填写了 Markdown 格式的简介。为了确保它也能被渲染成 HTML,您可以在模板中这样调用:

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

Here|renderThe filter will explicitly tell the AnQiCMS template engine tointroduction_textconvert Markdown text in variables to HTML.

In addition,archiveDetail/categoryDetail/pageDetail/tagDetailtags, when you retrieveContentWhen a fieldrendercan also be specified manually to indicate whether Markdown rendering should be performed. For example:

  • <div>文档内容:{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>:强制进行 Markdown 渲染。
  • <div>文档内容:{% archiveDetail archiveContent with name="Content" render=false %}{{archiveContent|safe}}</div>:Force no Markdown rendering, even if the global Markdown editor is enabled, the content will be output as raw Markdown text.

This flexibility allows you to decide, based on your actual needs, which text blocks need to be rendered in Markdown and which do not, thus achieving precise control over the content of the page.

Summary

AnQiCMS provides a concise and powerful Markdown rendering mechanism. From enabling globally in the backend, to the automatic rendering of content fields in front-end templates, to implementing style beautification, mathematical formulas, and flow chart display through the introduction of third-party libraries, as well as|renderThe fine control provided by the filter greatly enhances the efficiency and presentation of content operations.Master these methods and you will be able to fully utilize the advantages of AnQiCMS in content creation, presenting high-quality and structured content to users.


Frequently Asked Questions (FAQ)

1.I have enabled the Markdown editor in the background, but why is the Markdown syntax not rendered on the front-end page, instead of showing the original text symbols?

This is usually because your template file does not use|safefilters. For example, if your article contentarchive.ContentContains Markdown, you should use it in the template{{archive.Content|safe}}instead of{{archive.Content}}.|safeThe filter tells the template engine that this content is safe HTML, which can be parsed and displayed directly without escaping processing.

2. Why is my mathematical formula or Mermaid flowchart not displayed correctly and only the original text code is shown?

The enablement of the Markdown editor only affects the input of content, while advanced rendering features such as MathJax and Mermaid require the manual introduction of the corresponding JavaScript libraries in the front-end template. Please check yourbase.htmlOr has the script for MathJax and Mermaid been correctly introduced according to the instructions in the article.In addition, make sure that the formula and chart syntax you use in the Markdown content is the correct syntax supported by these libraries.mermaid...code block.

3. I have also used Markdown syntax in the custom model field, but it is not automatically rendered as HTML. What should I do?

For custom fields other than articles, pages, and other core content fields, AnQiCMS does not automatically perform Markdown rendering. You need to manually use it in the template.|renderA filter is used to explicitly tell the template engine to perform a transformation. For example, if your custom field name iscustom_description, you should write it in the template as{{archive.custom_description|render|safe}}This ensures that the Markdown content is correctly rendered inside it.

Related articles

How to remove empty lines generated by conditional judgment or loop tags in the template to optimize the display cleanliness of the page source code?

In website operation, we all hope that the website is not only powerful in function, rich in content, but also the source code behind it should be neat and orderly.However, when developing with a flexible template system like AnQiCMS, you may sometimes find that the generated HTML source code contains many blank lines, which are mainly caused by conditional judgment or loop tags in the template.Although these blank lines usually do not have a substantial impact on the functionality of the website, they do indeed reduce the readability of the source code, and bring inconvenience to subsequent maintenance and debugging.

2025-11-08

How to display the view count of articles in the article list or detail page?

In website operation, the number of article views is a very intuitive and important indicator, which not only reflects the popularity of the content but also provides data support for the optimization of subsequent content strategies.For websites built using AnQiCMS, displaying the number of views on article lists or detail pages is a relatively simple operation. With the powerful template tag features provided by AnQiCMS, we can easily achieve this requirement.Understanding the "Page Views" feature of AnQiCMS AnQiCMS is built with powerful traffic statistics functionality

2025-11-08

How to display a custom Banner image group on the category page or single page?

It is crucial that visual content is attractive in website operation.Especially for category pages and standalone single pages, displaying a set of carefully designed banner images can effectively attract visitors' attention, strengthen the brand image, and convey specific information.AnQiCMS provides a straightforward and flexible mechanism that allows you to easily configure and display custom Banner image groups on these key pages.

2025-11-08

How to display recommended attributes (such as headlines, recommendations, sliders) in article lists or detail pages?

In AnQi CMS, in order to make your website content more attractive and able to flexibly display articles of different importance levels or display forms, the system provides a "recommended attribute" feature.By cleverly utilizing these properties, you can easily add "headlinesThis guide will take you in-depth to understand how to set and use these recommended properties in AnQiCMS, making your website content management more convenient.

2025-11-08

What is the specific usage of the `render` filter in Markdown content rendering?

In Anqi CMS, we often encounter the need to convert text into colorful web elements.Especially when content is written in Markdown format, how to ensure that it can be presented correctly and beautifully to the user is a problem that requires a deep understanding.Today, let's talk about how the `render` filter works in the Anqi CMS Markdown content rendering.

2025-11-08

How to manually control the Markdown to HTML rendering of the `Content` field of the document?

In a content management system, Markdown, as a lightweight markup language, is widely popular for its simplicity and efficiency.AnQiCMS (AnQiCMS) fully understands the importance of content flexibility to users and therefore provides a flexible mechanism for handling the Markdown rendering of document content.When you need to manually control the Markdown to HTML rendering of the `Content` field in the document, the system provides an intuitive and powerful method.### Understanding the AnQi CMS Markdown processing mechanism First

2025-11-08

How to enable or disable Markdown rendering in the category detail page's `Content` field?

AnQi CMS provides high flexibility for users, especially in terms of content display.For the `Content` field on the category detail page, the system allows us to finely control whether it is rendered in Markdown format.This means that website operators can choose the most suitable processing method according to different content types and display requirements.### Understanding the `Content` field and Markdown rendering on the category detail page In AnQi CMS, each category has a `Content` field

2025-11-08

How to force-render Markdown content in a single-page `Content` field as HTML?

When using AnQi CMS to manage website content, the Single Page (Page) is a very practical feature that allows you to flexibly create static content such as "About Us", "Contact Information", and so on.Many friends like to write the content of these pages using Markdown, because it is simple and efficient, and can be quickly formatted.However, sometimes we find that even if the content is written in Markdown, the frontend of the page still displays as plain text and is not rendered as HTML.This is often because the template does not correctly instruct the system to render. Don't worry.

2025-11-08