How to insert Markdown formatted text in website content and render it correctly as HTML?

Calendar 👁️ 90

In AnQiCMS, inserting Markdown-formatted text and rendering it correctly into HTML is an important way for content operators to improve efficiency and produce high-quality pages.AnQiCMS has built-in support for Markdown editing and rendering, allowing you to enjoy the simplicity and efficiency of Markdown while ensuring that the final page output is beautiful and well-structured HTML.

Enable Markdown editor

To start using Markdown in AnQiCMS, first make sure that its editor feature is enabled. This setting is usually located in the core configuration of the system:

You can visitAnQiCMS backendEnterGlobal Settingsand then findContent settingsOption. Here, you will see an option to enable or disable Markdown editor.After checking the option, when you publish or edit articles, single pages, and other content, the content editor will switch to Markdown mode, making it convenient for you to create content using Markdown syntax.

Enable Markdown editor after, you can directly use Markdown syntax for formatting when editing content in the background. For example, using**粗体**to indicate bold,# 标题Indicates a first-level heading,- 列表项Indicates an unordered list, as well as Markdown common elements such as code blocks, links, and images.

Insert Markdown text in the content

AnQiCMS supports inserting Markdown text in various content types, including:

  • Article content:When publishing a new article or editing an existing one, you can directly input Markdown text in the 'Document Content' area.
  • Single page content:For pages such as “About Us” and “Contact Us”, the content can also be written using the Markdown editor.
  • Category description/content:When managing articles or product categories, the "category introduction" or "category content" fields also support Markdown format.
  • Tag description/content:If you want to add a more detailed description to a label, you can also use Markdown in the "Label Description" or "Label Content" field.

By writing content in Markdown, you can not only focus on the text itself, but also avoid cumbersome mouse operations, and ensure that the content structure is clear, laying a good foundation for subsequent style rendering.

Ensure Markdown is rendered correctly as HTML

When you enable the Markdown editor in the background and write content using Markdown syntax, AnQiCMS will usually automatically render it into HTML to display correctly on the front-end page.

For article, category and single page main content fields (such asarchiveDetail/categoryDetail/pageDetailin the tags ofContentfield), when the Markdown editor is enabled, the system will automatically convert the Markdown text to HTML.This means that when you call these fields directly in the template, you do not need any additional operations, and Markdown content can be displayed in HTML format.

For example, in the template of the article detail page, if your article content uses Markdown, the following call method is as follows:

<div>
    {%- archiveDetail articleContent with name="Content" %}
    {{articleContent|safe}}
</div>

Here{{articleContent|safe}}The HTML that has been safely converted by the AnQiCMS backend will be output to the page.|safeThe filter is crucial here, it indicates that the template engine should process the content as safe HTML, avoiding double escaping.

However, in certain specific scenarios, you may need to manually control the conversion process from Markdown to HTML. This includes:

  1. Markdown editor is not enabled, but a field contains Markdown text:If you disable the Markdown editor in the background but a certain content field still contains Markdown text, the system will not automatically render.
  2. Custom fields contain Markdown text:Fields added through the "Content Model Custom Fields" may not be rendered in Markdown by default.
  3. Need to explicitly control the rendering time:You want to explicitly Markdown render the content of a certain field.

In this case, you can use the AnQiCMS providedrenderFilter配合safeFilter,to manually convert Markdown text to HTML and safely output. For example,if your custom fieldintroductionContains Markdown text:

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

Here|renderThe filter will perform Markdown to HTML conversion,|safeMake sure the converted HTML can be correctly parsed by the browser without being escaped.

Enhance rendering effects: Support mathematical formulas and flowcharts

The new version of AnQiCMS also supports inserting mathematical formulas and flowcharts in Markdown content.These advanced features require the use of third-party libraries on the front-end to render correctly.You need to fill in the website template'sbase.htmlthe file<head>In the tag, introduce the corresponding CDN resources:

  1. Markdown default style:To make Markdown content on the page have better visual effects, you can introduce GitHub-style Markdown CSS.

    <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. Math formula rendering:If your content contains LaTeX and other mathematical formulas, you need to introduce the MathJax library for rendering.

    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    
  3. Flowchart rendering:For flowcharts using Mermaid syntax, you need to include the Mermaid library.

    <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 website template (usuallybase.html)的<head>After the area, the mathematical formulas and flowcharts you write in Markdown can be displayed professionally and beautifully on the front-end page.

Summary

AnQiCMS offers an efficient, convenient, and powerful content creation experience through its built-in Markdown editor and flexible rendering mechanism.Whether it is simple text formatting or complex mathematical formulas and flowcharts, AnQiCMS can help you easily handle them and ensure that the content is presented in a **state to the visitors.Just as long as you enable the editor according to the above steps, write the content correctly, and use it reasonably when necessaryrender|safeThe filter and external resources can fully utilize the potential of Markdown in AnQiCMS.


Frequently Asked Questions (FAQ)

  1. Ask: I have enabled the Markdown editor in the background and used Markdown syntax in the article, but why is the article still displayed as raw Markdown text on the front end and not converted to HTML?Answer: This is usually due to incorrect content output method in the template. Please check your template code to ensure that you have used|safeFilter. For example, it should be used{{articleContent|safe}}, rather than just{{articleContent}}. If the content is not passed throughContentThis field will be automatically rendered by default, or you need to add before if the Markdown editor is not enabled|safeadd before|rendera filter such as{{customField|render|safe}}.

  2. Ask: I want to add a 'Product Feature' field to the custom content model and allow Markdown text to be entered in this field.How should I ensure it renders correctly on the front end?Answer: For custom fields, even if the Markdown editor is enabled, the system may not automatically convert Markdown to HTML. Therefore, when calling this custom field in your template, it is recommended to always use it explicitly|render|safeFilter. For example, if your custom field name isproduct_features, when calling it in the template, it should be written as{{item.product_features|render|safe}}.

  3. Ask: I have added the CDN code for MathJax and Mermaid according to the document, but the mathematical formulas and flowcharts in the Markdown are still not displayed or displayed incorrectly. What is the matter?Answer: Please check if you have placed the CDN code correctly inbase.htmlthe file<head>Within the tags. Secondly, ensure that your Markdown syntax is correct, for example, a Mermaid flowchart needs to be in the format ofgraph TD...This structure begins. Sometimes, the browser's cache may also cause problems, try clearing the browser cache or accessing the page in incognito mode.If the problem still exists, it may be due to a CDN loading failure or a conflict with other JavaScript code on your website. You can check the console for error messages in the browser developer tools.

Related articles

How to dynamically display the website's logo image in the template of AnQiCMS?

The website's logo is the core of the brand image, it not only enhances the professionalism of the website, but also facilitates users in quickly identifying and remembering your brand.For AnQiCMS users, dynamically displaying the logo image in the website template is a basic and important operation.Luckyly, AnQiCMS provides a very convenient way to achieve this, allowing you to easily manage and display the website logo without writing complex code.### Website Logo Management: Backend settings are crucial Display the Logo on your AnQiCMS website

2025-11-07

How to display a user comment list on the front end and support review status display?

User comments are an important manifestation of website vitality, they not only enhance the interactivity of content but also provide valuable reference for other visitors.AnQi CMS understands the importance of comment management, providing simple and powerful features that allow you to flexibly display comment lists on the website front end and clearly identify the review status of comments.The core of displaying the comment list on the front-end: `commentList` tag To display user comments on your website's front-end page, you need to use the `commentList` template tag provided by AnQiCMS

2025-11-07

How to display a list of website friend links in the front-end template?

In website operation, friendship links are not only a bridge between websites, but also an important strategy to enhance website authority, increase external traffic, and optimize search engine rankings.Reasonably configure and display friendship links, which can effectively improve the website's SEO performance and provide users with more valuable external resources.AnQiCMS as an efficient content management system, fully considers this need, and provides a simple and intuitive way to manage and display friend links in the front-end template.### Backend Management: Setting and Maintaining Friend Links Display Friend Links on the Front-end Page

2025-11-07

How to extract the article summary and add an ellipsis in AnQiCMS template?

In website operation, how to make the article list page both beautiful and informative is a common concern.Generally, we do not display the full content of the article on the list page, but instead use a short 'abstract' to attract readers to click.To maintain the cleanliness and professionalism of the page, these summaries often need to be automatically truncated when exceeding a certain length and appended with an elegant ellipsis.AnQiCMS is a powerful content management system that provides flexible tools in the template to help us meet this need.Below, we will discuss in detail how to use AnQiCMS

2025-11-07

How to correctly display mathematical formulas and flowcharts in the template of AnQiCMS?

Today, with the increasing refinement of content management, websites not only carry text and images but also need to present complex information in a professional and understandable way, such as mathematical formulas and flowcharts.AnQiCMS is dedicated to providing efficient and flexible content management solutions. When we deal with technical articles, academic content, or business process diagrams on the website, how to ensure that these special elements are displayed correctly and beautifully becomes a topic worth discussing.AnQiCMS itself provides good support for Markdown editors, which means we can use concise

2025-11-07

Enhance interactive security: A practical guide to integrating captcha in the Anqi CMS frontend comments or messages

The website's comment section and message board are important channels for user interaction with the website.However, these interactive areas are often plagued by spam and automated programs, which not only affect the cleanliness of the website but also reduce the reading and communication experience of users.(CAPTCHA) is the effective barrier against such problems, it distinguishes between human users and malicious robots by requiring users to complete a task that is easy for humans but difficult for computers.For a website built with Anqi CMS, integrating captcha is not a complex matter.The Anqi CMS system provides a simple and clear path

2025-11-07

How to automatically parse URL addresses into clickable links in article content for AnQiCMS?

When using AnQiCMS for website content operation, we often hope that URLs in articles, product descriptions, or other text content can be automatically identified and converted into clickable links. This not only improves user experience but also helps in the dissemination of content and is friendly to search engines.AnQiCMS as a content management system focusing on efficiency and SEO optimization has of course considered this requirement and provided a very convenient implementation method.This is mainly due to its flexible template engine and built-in filter functions.To implement automatic URL parsing in article content

2025-11-07

How to display website traffic statistics and spider crawling information on the front end?

We often care about the number of visits to our website, and which search engine spiders have crawled our content.This data can not only help us understand the health status of the website, optimize the content strategy, but also improve user trust.The Anqi CMS provides detailed traffic statistics and crawler monitoring functions in the background, allowing us to have a thorough understanding of the website's operational status.If you want to present valuable data such as today's visitor count and yesterday's spider visit count directly on the website front end, how can you achieve this?### AnQi CMS data statistics capability First

2025-11-07