When rendering Markdown before, I used the preview mode of mavonEditor, which was quite convenient to use. I only needed to import components, but I encountered difficulties in recent development. The main problem is that as a single-page application, the links within the site must be redirected using router-link. If the a tag rendered by mavonEditor is used by default, the page will be reloaded, which will provide a poor user experience. Dynamic RenderingIf you want to dynamically render router-link based on user content on the front end, you need to use dynamic rendering. According to the official documentation, you can directly modify vue.config.js: // vue.config.js module.exports = { runtimeCompiler: true } Rendering MarkdownThe author uses markdown-it, the configuration process is as follows: Installnpm install markdown-it --save # Markdown-it main body npm install markdown-it-highlightjs --save # Code highlight npm install markdown-it-katex --save # Latex support Two additional syntax plugins are installed here. If you need any other plugins, you can search them on npm. Static file import highlight.js <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css" rel="external nofollow" > <script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script> github-markdown-cssMarkdown styles Installnpm install github-markdown-css --save ImportAdd in main.js file import 'github-markdown-css/github-markdown.css' katex Import via CDN and add in <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css" rel="external nofollow" > use First create the <template> <components :is="html" class="markdown-body"></components> </template> <script> import MarkdownIt from 'markdown-it' import hljs from 'markdown-it-highlightjs' import latex from 'markdown-it-katex' export default { name: 'Markdown', props: { content: String }, data: () => ({ md:null }), computed: { // Use computed to dynamically update HTML during dynamic binding: function () { let res = this.md.render(this.content) // Use regular expressions to replace in-site links with router-link tags res = res.replace(/<a href="(?!http:\/\/|https:\/\/)(.*?)" rel="external nofollow" >(.*?)<\/a>/g, '<router-link to="$1">$2</router-link>') // Use regular expressions to open external links in new windows res = res.replace(/<a href="(.*?)" rel="external nofollow" >(.*?)<\/a>/g, '<a href="$1" rel="external nofollow" target="_blank">$2</a>') return { template: '<div>' + res + '</div>' } } }, created () { this.md = new MarkdownIt() this.md.use(hljs).use(latex) } } </script> Then import it wherever you want to use it <template> <div> <Markdown :content="content"/> </div> </template> <script> import Markdown from '@/components/Markdown.vue' export default { name: 'Home', components: Markdown }, data: () => ({ content: '' }), created () { this.content = '# Test' } } </script> The above is the details of implementing Markdown rendering in Vue single-page application. For more information about vue Markdown rendering, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Summary of several important performance index calculation and optimization methods for MySQL
>>: Configure VIM as a C++ development editor in Ubuntu
I don't know if you have ever encountered suc...
Table of contents Technology Stack Effect analyze...
In our daily development work, text overflow, tru...
This article shares the installation and configur...
MySQL is easy to install, fast and has rich funct...
Many friends who have just started to make web pag...
Copy code The code is as follows: <!DOCTYPE ht...
This article shares the specific code of JavaScri...
Preface In JavaScript, this is the function calli...
Table of contents 1. Database master-slave classi...
This article shares the specific code of JavaScri...
The method to solve the problem of forgetting the...
<br />In the first section of this series, w...
Table of contents 1. Download nodejs 2. Double-cl...
<br />Original URL: http://www.lxdong.com/po...