Security CMS Markdown content: How to perfectly present on the front-end page?

In content creation, Markdown, with its concise and efficient syntax, has won the favor of countless content creators.Aqiqi CMS understands this point and therefore provides strong Markdown editor support.But how can you ensure that the Markdown content you edit in the background is rendered correctly and beautifully as HTML on the website front page?This involves several key stages, understanding them can help you manage website content more skillfully.

Backend processing: Conversion from Markdown to HTML

To allow AnQi CMS to recognize and process the Markdown content you input, the first step is to make simple settings in the background. You need to go to安企CMS后台 -> 全局设置 -> 内容设置Find and enable the Markdown editor feature.Once enabled, the system will intelligently convert Markdown syntax to standard HTML format when you save content (whether it is an article, product details, or a single-page content), store it in the database, and lay the foundation for the display of the front-end page.

On the front-end page, when you usearchiveDetail/categoryDetailorpageDetailTag call contentContentWhen the field is edited and saved in a Markdown editor, the system will automatically convert it to HTML.This means that in most cases, you do not need to perform any additional operations, and the content will be displayed in HTML format on the page.

However, in order to provide more flexible control, these tags also providerenderparameters. For example, when calling the document content:

{# 默认用法,自动获取当前页面文档内容并转换为HTML #}
<div>文档内容:{% archiveDetail with name="Content" %}</div>
{# 显式指定进行Markdown到HTML的转换 #}
<div>文档内容:{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>
{# 强制不进行Markdown转换,显示原始Markdown文本 #}
<div>文档内容:{% archiveDetail archiveContent with name="Content" render=false %}{{archiveContent|safe}}</div>

Byrender=trueYou can explicitly tell the system to perform Markdown conversion; andrender=falseIt allows you to retain the original Markdown format of the content in certain specific scenarios, without converting it to HTML.This is very useful when it is necessary to display Markdown source code or custom frontend rendering logic.|safeA filter to ensure that HTML tags can be parsed by the browser correctly, rather than displayed as plain text.

Front-end rendering: style beautification and advanced feature support

Just converting Markdown to HTML is not enough. To make the content present beautifully on your website and support advanced features like mathematical formulas, flowcharts, and more, you still need some frontend resources.

  1. Markdown basic style application:Content converted to HTML, although the structure is correct, it may lack aesthetic formatting by default. At this point, introducing a CSS library is **practical. We recommend usinggithub-markdown-cssIt allows your Markdown content to have a concise and readable style similar to GitHub. You just need to use the template on the website'sbase.htmlthe file<head>the tag the followinglinkTags:

    <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" />
    

    This code will load the stylesheet from CDN, providing a unified and beautiful display effect for your Markdown content.

  2. Correct display of mathematical formulas:If you have inserted a LaTeX-formatted mathematical formula in the Markdown content, it is usually not recognized and rendered directly by the browser after being converted to HTML. At this time,MathJaxThis third-party JavaScript library comes in handy.MathJaxIt can parse mathematical formulas in web pages and render them into high-quality layouts. Similarly, inbase.htmlthe file<head>Add within the tag:

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

    EnsureasyncThe property exists, so the script will not block the loading of other content on the page.

  3. Dynamic presentation of flowcharts:For Markdown that includes Mermaid flowcharts, sequence diagrams, and the like, they are only simple text code blocks after being converted to HTML. To render them dynamically as graphics, you need to introduceMermaidLibrary. Atbase.htmlAt the bottom of the file,</body>Before the tag, add the following JavaScript code block:

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

    Heretype="module"It is very important, it indicates that this is an ES module, andmermaid.initialize({ startOnLoad: true });Make sure the Mermaid chart is scanned and rendered automatically after the page is fully loaded.

Summary

By following these steps, you can create content using the Markdown editor on the Anqi CMS backend, which can not only be correctly converted to HTML by the system but also achieve beautiful styles on the frontend page, render complex mathematical formulas, and dynamic flowcharts.The entire process is smooth and flexible, allowing your content to present more professionalism and expressiveness.


Frequently Asked Questions (FAQ)

Q1: Why am I using a Markdown editor on the backend, but the content on the front page is displayed as raw Markdown text and not converted to HTML?

A1: This is usually due to the configuration issue of the template tags. Please check whether you have used the content called in the template file (for example{{archive.Content}}or{% archiveDetail with name="Content" %}) when using|safeFilter. If the system did not automatically convert Markdown to HTML, and you did not specify it manuallyrender=trueThe content will be output in its original text form. Make sure your content calls similar{{archive.Content|safe}}or{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}Also, please confirm again安企CMS后台 -> 全局设置 -> 内容设置Whether the Markdown editor has been successfully enabled in it.

Q2: My mathematical formula or flowchart is written correctly in the Markdown content, but the front-end page only displays code blocks and does not render the graphics. What is the reason for this?

A2: This is usually because you have not properly introduced the corresponding JavaScript rendering library in the website template. After the Markdown content is processed by the Anqin CMS, although it is converted to HTML containing specific tags, mathematical formulas requireMathJaxThe library needs to be rendered, the flowchart doesMermaidlibrary. Make sure you have followed the steps mentioned in the article toMathJaxandMermaidadded the CDN link and its initialization script tobase.htmlthe file<head>or</body>Before the label, and there are no spelling errors or network loading failures.Check the browser developer tools (F12) console (Console) and network (Network) tabs to see if there are any JS loading errors or rendering error messages.

Q3: I have some content that I want to edit using Markdown, but I want some parts to display as plain text or HTML that I write manually. Can Anqi CMS do that?

A3: Can be.The Anqi CMS provides flexible control methods.ContentWhen the field is explicitly setrender=falsethe parameters. For example,{% archiveDetail customContent with name="Content" render=false %}{{customContent|safe}}. So, the system will skip the Markdown to HTML conversion process and directly output the content you enter. Of course, if you enter HTML, you still need to work with|safeThe filter ensures that it is parsed correctly.