How to use Markdown formatting in document content and ensure it is correctly rendered as HTML?

Calendar 👁️ 89

AnQi CMS provides flexible content editing methods for content creators, and the support for Markdown formats greatly enhances the efficiency and expressiveness of content creation.Understanding how to use Markdown correctly in document content and ensure that it is perfectly rendered as HTML on the website front-end is the key to leveraging this functional advantage.

Enable and use the Markdown editor

To start using Markdown in AnQiCMS, you first need to make simple configurations in the background. This is usually inGlobal settings -> Content settingsChinese is completed. After enabling the Markdown editor, when you edit documents, single pages, or categorized content, the content input box will switch from the traditional rich text editor to a pure text editor that supports Markdown syntax.

Once enabled, you can use Markdown syntax in the content editing area. For example, you can use#Indicates the title,**粗体**to indicate bold text,[链接](URL)to create a hyperlink,插入图片,以及-*`Create a list and other. These basic syntax will be automatically parsed and converted into corresponding HTML structure by AnQiCMS template engine.

For more advanced Markdown applications, such as mathematical formulas and flowcharts, AnQiCMS also provides support.To make these advanced elements parse and display correctly in the browser, you need to include some third-party JavaScript libraries and style files.These code snippets are usually placed in the public header file of your website template (such asbase.htmlorheader.html)的<head>or within tags.</body>Before the end tag.

For example, if you want to display Markdown styles, mathematical formulas, and flowcharts correctly on a website:

  • Basic Markdown style: To make the rendered Markdown content have clear formatting and style, you can introduce Markdown CSS similar to GitHub.

    <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" />
    
  • Math formula displayFor mathematical formulas written in LaTeX or MathML, the MathJax library can be introduced to parse and display them beautifully.

    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    
  • Flowcharts and charts: Mermaid is a popular JavaScript library that can convert Markdown-style text descriptions into flowcharts, sequence diagrams, and other charts.

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

Add this code to your template file and you will be able to write the corresponding syntax in Markdown, for example, using$$...$$Enclose mathematical formulas or use Mermaid code block syntax (such asmermaid) to create charts.

Ensure the content is rendered correctly as HTML

In AnQiCMS, the final display effect of content is determined by template tags and filters.Understand how to use them correctly is the key to ensuring that Markdown content is rendered correctly.

After you enable the Markdown editor in the background, througharchiveDetail/categoryDetail/pageDetailThe label getsContentField content, the AnQiCMS template engine will usually automatically convert it from Markdown to HTML. This means that you can directly output it in the template{{ archive.Content }}In most cases, you can see the rendered HTML.

However, in certain specific scenarios, you may want to manually control the rendering process of Markdown content, or ensure that the content is not interfered with by the default escaping mechanism of the template engine. AnQiCMS provides this feature.renderParameters and|safefilter.

  • renderParameterInarchiveDetail/categoryDetail/pageDetailto getContentWhen specifying a field, you can addrendera parameter to explicitly specify whether to perform Markdown to HTML conversion.render=trueit will enforce the conversion, andrender=falseIt will prevent conversion and directly output the original Markdown text. For example:

    {# 强制将Markdown内容渲染为HTML #}
    <div>文档内容:{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>
    {# 不进行Markdown渲染,显示原始Markdown文本 #}
    <div>原始Markdown:{% archiveDetail archiveContent with name="Content" render=false %}{{archiveContent}}</div>
    

    This parameter is very useful when you need to process the same Markdown content differently.

  • |safeFilterIn the AnQiCMS template system, to prevent potential cross-site scripting (XSS) attacks, all content output to the page via variables is default HTML entity encoded. This means that, like<Such HTML special characters will be converted to&lt;, to ensure that the browser displays it as text instead of parsing it as an HTML tag.

    Therefore, for content that has been rendered in Markdown or you wish to output as HTML (such as throughrender=trueprocessed content), you must use|safeThe filter explicitly informs the template engine that the content is safe HTML code and does not require further escaping. If missing|safeFilter, you might see the original HTML code (for example<p>我的段落</p>) rather than the rendered effect in the browser (an actual paragraph).

    The correct approach is:

    {# 确保渲染后的HTML内容被浏览器正确解析 #}
    <div>文档内容:{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>
    

    |safeThe filter is a critical step to ensure that Markdown content is ultimately parsed and displayed correctly by the browser as HTML.

By following these steps, that is, enabling Markdown in the background, introducing the necessary third-party libraries in the template, and using them as needed when outputting contentrenderParameters and|safeFilter, you can fully utilize the AnQiCMS Markdown function, efficiently create and display high-quality document content.

Frequently Asked Questions (FAQ)

  1. Why did I enable the Markdown editor, but the content is displayed as raw Markdown syntax on the front end rather than rendered HTML?This is usually because the output content in the template is missing|safeFilter. AnQiCMS defaults to escaping HTML special characters. If Markdown content is converted to HTML and output without|safeFilter, these HTML tags will also be escaped, causing them to be displayed as raw code on the page. Make sure to output the Markdown converted content (such as{{ archiveContent }}when added}|safeFor example{{ archiveContent|safe }}.

  2. How do I display my mathematical formula or flowchart and not as code block instead of graphics?The Markdown editor itself is responsible for parsing the text syntax of mathematical formulas and flowcharts, but browsers do not natively support their rendering. You need to include the template file for the website (usuallybase.htmlor import the corresponding third-party JavaScript library in the public header file.For mathematical formulas, MathJax needs to be included; for flowcharts, Mermaid needs to be included.Please refer to the code snippet provided in the article to ensure that these libraries are correctly included in your website.

  3. Can AnQiCMS's Markdown editor and rich text editor be used at the same time?Can I use Markdown in some content types and rich text in others?Currently, the Markdown editor and rich text editor in AnQiCMS are globally switched, that is, after you enable the Markdown editor in “Global Settings -> Content Settings”, all modules that support content editing (such as documents, single pages, category descriptions, etc.) will use the Markdown editor uniformly.You cannot switch independent editors for different content types directly in the background.However, even if you have enabled the Markdown editor globally, through the templaterender=falseParameters can also be optional

Related articles

How to add multiple Tag tags to documents and associate them for display and filtering on the front end?

In AnQiCMS, adding Tag tags to documents and associating them for display and filtering on the front end is an effective way to enhance the organization of website content and user experience.Tag labels can not only help users find the content they are interested in faster, but also optimize the keyword layout of the website from the SEO level, increasing the exposure of the content.Next, we will discuss in detail how to implement this feature in AnQiCMS. ## One, add Tag label to document in the background Adding a Tag label to the AnQiCMS backend is a straightforward and convenient process. First

2025-11-08

How to use the scheduled publishing feature to automatically go live and display documents at specified times?

## Precisely Control Content Rhythm: A Practical Guide to AnQi CMS Scheduled Publishing Function In today's increasingly refined content operation, how to ensure that content reaches the target audience at the most appropriate time is a core challenge faced by every operator.Manually publishing is inefficient and is prone to delays or errors due to temporary affairs or time differences.Fortunately, AnQi CMS provides a powerful and practical feature - scheduled publishing, which can make content going live automated and precise, bringing unprecedented flexibility and efficiency to website operations.The timing release function of Anqi CMS, as the name implies

2025-11-08

How to customize the document URL alias to optimize its display in browsers and search results?

In website operation, a URL (Uniform Resource Locator) is not only the address of the content, but also the first step for users and search engines to recognize the website.A clear, meaningful, and friendly URL alias that can greatly enhance the display effect of content in the browser and achieve better visibility in search results.AnQiCMS provides flexible and powerful features, allowing us to easily customize documents, categories, tags, and even the URL aliases of the page itself, thereby optimizing the overall performance of the website.Why is it important to use custom URL aliases?Firstly, for search engines

2025-11-08

How to set the recommendation attribute to affect the display priority of the front-end content when publishing a document?

How to effectively guide visitors to focus on key information when managing content in AnQi CMS, and enhance the visibility and interactivity of the content, is a common concern for operators.By setting the recommended properties of the document reasonably, we can finely control the display priority of the front-end content, thereby achieving this goal.This not only helps to optimize the user experience, but also better serves the overall operation strategy of the website.### Backend settings recommended attribute: clear content positioning When we publish or edit a document, the content editing interface of Anqi CMS provides a feature area named "Recommended Attribute".

2025-11-08

How to set a custom template for categories to change the overall display style of the category page?

AnQi CMS provides great flexibility for website content management, and customizing the display style of category pages is a very practical feature.By setting custom templates for different categories, you can easily allow news, products, cases, and other types of category pages to present unique design and content layouts, thereby better meeting users' browsing needs and brand display strategies.Why do you need a custom category template? By default, AnQi CMS uses a generic list template for all categories (usually `{modeltable}/list.html`)

2025-11-08

Can the settings of the category template be inherited by subcategories to unify the display style of subcategories?

It is crucial to maintain the consistency of page style in website content management to enhance user experience and brand image.Especially for websites with multi-level categories, how to efficiently manage the display styles of these category pages is a concern for many content operators.AnQiCMS (AnQiCMS) provides very practical functions in this aspect, which can help us easily achieve the uniformity of the display style of subcategories.Understanding the importance of category templates and subcategory inheritance in Anqi CMS

2025-11-08

How can I specify a display template uniformly for all documents under a specific category?

When using AnQiCMS to manage website content, we often encounter such a need: we hope that all articles, products, or other documents under a specific category can be unified to use the same display template, rather than setting each document separately.This not only ensures the consistency of the website style, but also greatly improves the efficiency of content operation.AnQiCMS knows the pain points of users in this regard, and therefore provides a very flexible and intuitive setting method to meet this need.### Target Classification First, we need to enter the AnQiCMS admin interface

2025-11-08

How to display banner carousel images or thumbnails on the category page to enrich the visual effects?

In website operation, the category page is not just a content directory, but also a key entry for users to explore and discover your website's services or products.A beautifully designed, visually rich category page can significantly enhance user experience, inspire their interest, and guide them to explore deeper.AnQiCMS (AnQiCMS) provides flexible and powerful features, allowing you to easily display eye-catching banner carousel images or beautiful thumbnails on the category page, thereby enriching the visual effects and attracting visitors' attention.###

2025-11-08