How to introduce third-party JavaScript libraries (such as Markdown, mathematical formulas, flowcharts) in templates to enhance content display?

Calendar 👁️ 77

In AnQiCMS, introducing a third-party JavaScript library is a very effective means to make your content display more expressive and professional.For example, writing structured text with Markdown, displaying complex mathematical formulas with MathJax, or using Mermaid to draw intuitive flowcharts can greatly enrich the user's reading experience.The powerful template system of AnQiCMS provides us with a flexible integration method.

AnQiCMS template system overview

Let's briefly review the operation mechanism of AnQiCMS templates. The template files of AnQiCMS are.htmlsuffix and stored in/templateUnder the directory. You will find that the template uses a syntax similar to Django, through double curly braces{{变量}}to output content, while conditional judgment and loop control use{% 标签 %}The form. Static resources (such as custom JS scripts, CSS styles, images) are usually placed in/public/static/In the catalog.

On the template structure, AnQiCMS encourages the use ofextendsThe tag defines a basic skeleton template (usuallybase.html), and other page templates inherit this skeleton. At the same time,includeTags allow you to encapsulate commonly used code snippets (such as headers, footers, sidebars, etc.) into independent files and reference them where needed, which provides convenience for centralized management and the introduction of third-party resources.

The core strategy of introducing a third-party JavaScript library

Introducing third-party JavaScript libraries usually involves two main methods: loading through a content delivery network (CDN) or downloading the library file to a local server for reference. Regardless of the method, the core operation is to add it in the template file.<script>or<link>.

For libraries that need to take effect throughout the entire site or on multiple pages, the most recommended practice is to include them in your website'sBasic framework template (for examplebase.html)of<head>Inside the tag or</body>Before the closing tag. So, as long as the page inherits this basic template, the libraries introduced will be loaded automatically.

Choose the appropriate loading location

  • <head>Inside the tag:Suitable for CSS style libraries (such as Markdown styles) and those JavaScript libraries that need to be initialized or rendered before the page DOM is built (such as some mathematical formula rendering libraries and flowchart drawing libraries, which may need to process specific tags immediately upon page loading).However, it should be noted that too many scripts placed here may block page rendering and affect the loading speed of the first visit.
  • </body>Before the closing tag:JavaScript libraries that are suitable for most operations depending on DOM elements, which ensures that the DOM elements are fully loaded without blocking the initial rendering of the page, thus enhancing the perceived loading speed of the user.

Practical Guide: Enhanced Content Display

AnQiCMS has built-in support for Markdown editors and provides relevant settings options in the background, making it particularly convenient to introduce third-party libraries for Markdown rendering, mathematical formulas, and flowcharts.

Step 1: Enable Markdown editor

Before starting, please make sure to go to the AnQiCMS backend and click sequentiallyGlobal settings -> Content settings, find andEnable Markdown editor. This is the prerequisite for all Markdown-related features to take effect.

Introduce Markdown style: improve reading experience

Markdown itself is just a markup language, to make it display beautifully on the web page, we need to introduce the corresponding CSS.github-markdown-cssIt is a popular choice that allows your Markdown content to look like GitHub's Markdown.

You can use it in yourbase.htmltemplate 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 will automatically apply a style similar to GitHub when your article content uses Markdown syntax.

Beautifully render mathematical formulas: MathJax

For content that includes complex mathematical formulas, MathJax is an indispensable tool.It can render mathematical formulas in LaTeX, MathML, and other formats into high-quality, easy-to-read images or fonts.

Similarly, inbase.htmltemplate file<head>Inside the tag, add the MathJax JavaScript reference:

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

asyncThe attribute indicates that the script will load asynchronously without blocking the page parsing. After MathJax is loaded, it will automatically scan the mathematical formula tags on the page and render them.

Dynamic drawing of flowcharts: Mermaid

Mermaid is a JavaScript-based chart drawing tool that allows you to generate flowcharts, sequence diagrams, Gantt charts, and more using simple text descriptions.This is very useful for demonstrating business processes, technical architectures, and so on.

The introduction of the Mermaid library is usually placed inbase.htmlof<head>and simple initialization is required:

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

This code passestype="module"Introduce the Mermaid module and set it up during initializationstartOnLoad: true, indicating that Mermaid automatically searches for and renders charts after the page is loaded.

Key points and注意事项

  1. The modification path of the template file:You can/templateFind the template you are currently using in the directory, and edit the files in it. If your template uses.html, it is usually necessary toincludeorextends,base.htmlOr in a public header file (such aspartial/header.htmlAdd the above code in it.
  2. CDN and local hosting:The example above used CDN services (such as cdnjs, jsdelivr) to load third-party libraries.The advantage of CDN lies in its fast loading speed and saving server bandwidth.If you have concerns about network dependency or have specific version control requirements, you can also download these library files and place them in/public/static/您的模板名/jsor/public/static/您的模板名/cssIn the directory, then replace the CDN link in the template with the local path.
  3. Compatibility and version:When introducing third-party libraries, please pay attention to the compatibility of their versions with your existing page scripts. Try to choose stable and widely used versions.
  4. Content writing:After introducing these libraries, you can use the corresponding syntax to write content in the AnQiCMS backend editor (in Markdown mode). For example:
    • Markdown:Write Markdown normally.
    • Mathematical formula:Use$$...$$or$...$Enclose the formula (depending on MathJax configuration).
    • Flowchart:Use Mermaid's specific syntax, for example:
      graph TD
          A[Start] --> B{Decision};
          B -- Yes --> C[Perform Action];
          C --> D[End];
          B -- No --> D;
  5. Clear the cache:After modifying the template file, the browser may sometimes cache the old page content.Please try clearing the browser cache or force refresh the page (Ctrl+F5) to view the latest effect.If it still does not work, please check if there are any error messages in the browser console (F12).

By following these steps, you will be able to easily introduce third-party JavaScript libraries into the AnQiCMS template, adding more vitality and professionalism to your website content, thereby providing a richer and more attractive user experience.


Frequently Asked Questions (FAQ)

Q1: I have added the script according to the steps, but the mathematical formulas or flowcharts on the page are not rendered correctly. How should I troubleshoot?

A1: First, please check if your AnQiCMS background is already set upGlobal settings -> Content settingsinEnable Markdown editorNext, open the browser's developer tools (usually press F12), and check the 'Console' tab for any JavaScript error messages, as these are often the direct cause of rendering failures.At the same time, check the "Network" tab to confirm that all CDN resources (MathJax, Mermaid, github-markdown-css) have been successfully loaded (status code 200).Finally, check whether the mathematical formula or flowchart syntax you use in the content editor is correct, as these libraries have strict syntax requirements.

Q2: Can I download these third-party library files to host them on a local server? If so, where should the files be placed, and how should they be referenced in the template?

A2: Absolutely. After downloading the library files to your local machine, it is recommended that you place them in the static resource directory of the current template, for example/public/static/您的模板名/jsand/public/static/您的模板名/css. Then, when referencing in the template, replace the CDN link with the local path relative to your website root. For example, if your template is nameddefault, you can placegithub-markdown-cssplaced in/public/static/default/css/github-markdown.min.css, then inbase.htmlreferenced as<link rel="stylesheet" href="/static/default/css/github-markdown.min.css" />This helps reduce external dependencies and improve website stability.

**Q3: I only want to load on specific types of article pages (such as technical blogs)

Related articles

Does AnQiCMS support displaying the document list under a specific Tag on the front end?

In AnQi CMS, you can of course easily display the document list under a specific Tag (label) on the frontend.This is not only a basic function of a content management system, but AnQi CMS also enables you to achieve this in a very fine and user-friendly manner through its flexible template tag system, thus better organizing content and improving the user experience and SEO performance of the website.Starting from version 2.1.0 of AnQi CMS, the system introduced powerful article and product Tag label features, which means you can now add one or more tags to your content (whether articles or products)

2025-11-09

How to correctly display the current page path information in the breadcrumb navigation of a website?

The website is operational, a clear navigation path is crucial for user experience and search engine optimization (SEO).When a visitor enters a deep page of the website, the breadcrumb navigation (Breadcrumb Navigation) is like a trail of breadcrumbs that guides them back, helping them understand their position in the website structure and making it convenient to return to the upper-level page.In AnQiCMS (AnQiCMS), implementing an efficient and practical breadcrumb navigation to correctly display the path information of the current page is simpler than you imagine.

2025-11-09

How do related document tags intelligently display content similar to the current article topic?

How to guide visitors to more related content naturally after they finish reading an article in content operation, which can not only significantly improve user experience, but also effectively extend the visitors' stay on the website, reduce the bounce rate, and thus have a positive impact on search engine optimization (SEO).AnQi CMS is well-versed in this, providing intelligent and flexible mechanisms that allow us to easily implement this "You might also like" content recommendation.### Content Tag: The First Step of Building Associations AnQi CMS realizes smart content association through its powerful "tag" feature

2025-11-09

How to display "Previous" and "Next" articles on the article detail page to enhance user experience?

In website content operation, continuously optimizing user experience is the key to enhancing website value.How to guide users to find more related content while browsing an article on your website, to maintain their reading interest, is a problem that every operator needs to think about.Add "Previous" and "Next" article navigation at the bottom or side of the article detail page, which is a very effective and common strategy.It not only allows users to conveniently explore the website content, reduces the trouble of returning to the list page to find things, but also extends the time users stay on the website unobtrusively, and reduces the bounce rate

2025-11-09

How are the "recommended properties" (such as headlines, slides) used to control the special display positions on the homepage or list?

In content operation, we often need to highlight specific content on the website, such as the latest news, popular articles, or important product recommendations.AnQi CMS provides a very practical feature - "Document Recommendation Attribute", which can help us easily achieve these diverse content display needs, making the website more attractive. ### Flexible and diverse document recommendation attributes When we enter the Anqi CMS backend to edit or publish documents, we will notice an option called "recommendation attributes".This provides a variety of preset identifiers, as if tagging the content with different labels

2025-11-09

How to view the content that has been crawled and accessed in the website traffic statistics and crawler monitoring data?

Manage a website, what we care most about is whether our content is seen by users and whether it is indexed by search engines.This data is like the pulse of the website, directly reflecting the health of content operation and the activity of the website on the Internet.AnQi CMS knows these core needs and has built practical traffic statistics and spider monitoring functions, allowing us to easily master the key performance indicators of the website.Why are we so concerned about traffic and crawler data?In short, paying attention to these data is to better understand and optimize our website. First

2025-11-09

How to optimize the display format of text and numbers using AnQiCMS filters (such as truncatechars, floatformat)?

How to make information concise, clear, and professional in website content operation?This is often a challenge faced by many operators. Whether it is a news summary, product description, or price data, statistics, if displayed in a disorganized manner, it not only affects user experience but may also lead to deviations in information transmission.AnQiCMS (AnQiCMS) fully understands this pain point, its powerful template engine built-in a series of practical filters, which can help us easily optimize the display format of text and numbers, making your website content look brand new.Refined text

2025-11-09

How to correctly render Markdown document content as HTML in a template?

Content creators often choose Markdown as their preferred tool because it can organize content in a concise and clear manner, while also easily achieving formatting effects.However, simply inputting content in Markdown format in the background is not enough to allow the website front end to display it correctly. We need some additional steps to ensure that the content is rendered correctly as HTML.This article will introduce in detail how to make Markdown content turn beautiful in AnQiCMS templates, presenting it to users in the form of HTML.### One

2025-11-09