vue3中展示markdown格式文章的三種形式
一、安裝
# 使用 npm npm i @kangc/v-md-editor -S # 使用yarn yarn add @kangc/v-md-editor
二、三種實(shí)現(xiàn)形式
1、編輯器的只讀模式
main.ts文件中配置:
import VMdEditor from '@kangc/v-md-editor'; import '@kangc/v-md-editor/lib/style/base-editor.css'; const app = createApp(/*...*/); app.use(VMdEditor);
使用的組件中:
<template> <v-md-editor v-model="text" mode="preview"></v-md-editor> </template>、 //text為要傳入的markdown格式的內(nèi)容
2、預(yù)覽組件
main.ts中配置:
import VMdPreview from '@kangc/v-md-editor/lib/preview'; import '@kangc/v-md-editor/lib/style/preview.css'; const app = createApp(/*...*/); app.use(VMdPreview);
使用的組件中:
<template> <v-md-preview :text="text"></v-md-preview> </template> //text為要傳入的markdown格式的內(nèi)容
3、html預(yù)覽組件
main.ts中配置:
import VMdPreviewHtml from '@kangc/v-md-editor/lib/preview-html'; import '@kangc/v-md-editor/lib/style/preview-html.css'; // 引入使用主題的樣式 import '@kangc/v-md-editor/lib/theme/style/vuepress'; const app = createApp(/*...*/); app.use(VMdPreviewHtml);
使用的組件中:
<template> <!-- preview-class 為主題的樣式類名,例如vuepress就是vuepress-markdown-body --> <v-md-preview-html :html="html" preview-class="vuepress-markdown-body"></v-md-preview-html> </template>
三、實(shí)現(xiàn)其他功能
1、設(shè)置外觀
import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js'; import '@kangc/v-md-editor/lib/theme/style/vuepress.css'; //這個(gè)css文件,它與 vuepressTheme 主題相關(guān),定義了主題的顏色、字體、間距等樣式。 // 使用 vuepress 主題 VueMarkdownEditor.use(vuepressTheme);
2、對(duì)代碼進(jìn)行語(yǔ)法高亮并顯示代碼語(yǔ)言
import Prism from 'prismjs';
VueMarkdownEditor.use({
Prism,
});
3、代碼塊顯示行號(hào)
//main.ts中 import VueMarkdownEditor from '@kangc/v-md-editor'; import createLineNumbertPlugin from '@kangc/v-md-editor/lib/plugins/line-number/index'; VueMarkdownEditor.use(createLineNumbertPlugin());
4、高亮代碼行
import VueMarkdownEditor from '@kangc/v-md-editor'; import createHighlightLinesPlugin from '@kangc/v-md-editor/lib/plugins/highlight-lines/index'; import '@kangc/v-md-editor/lib/plugins/highlight-lines/highlight-lines.css'; VueMarkdownEditor.use(createHighlightLinesPlugin());
5、快捷復(fù)制代碼
main.ts中配置:
import VueMarkdownEditor from '@kangc/v-md-editor'; import createCopyCodePlugin from '@kangc/v-md-editor/lib/plugins/copy-code/index'; import '@kangc/v-md-editor/lib/plugins/copy-code/copy-code.css'; VueMarkdownEditor.use(createCopyCodePlugin());
組件中使用:
<template>
<v-md-editor v-model="text" height="500px" @copy-code-success="handleCopyCodeSuccess" />
</template>
//使用@copy-code-success
<script>
export default {
methods: {
handleCopyCodeSuccess(code) {
console.log(code);
},
},
};
</script>
四、注意
如果按正常流程配置后,內(nèi)容出不來(lái),可以選擇自己新開一個(gè)項(xiàng)目再操作一遍,如果這個(gè)時(shí)候是正常的,那可能就是原來(lái)的項(xiàng)目配置的版本過低,可以選擇更新一下項(xiàng)目中的各項(xiàng)配置
五、方法補(bǔ)充
Vue 3中如何實(shí)現(xiàn)Markdown展示
使用Vue 3中的markdown-it或vue-markdown等庫(kù)來(lái)實(shí)現(xiàn)類似React代碼中的Markdown渲染功能。以下是一個(gè)完整的Vue 3實(shí)現(xiàn)方案:
步驟一:安裝必要的依賴
npm install markdown-it vue-demi markdown-it-katex markdown-it-breaks markdown-it-mathjax3 markdown-it-highlightjs highlight.js
步驟二:創(chuàng)建Markdown組件
<template>
<div class="markdown-body" v-html="renderedContent"></div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import MarkdownIt from 'markdown-it'
import mk from 'markdown-it-katex'
import breaks from 'markdown-it-breaks'
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-light.css'
import 'katex/dist/katex.min.css'
const props = defineProps({
content: {
type: String,
required: true
}
})
// 創(chuàng)建markdown-it實(shí)例并配置插件
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre class="hljs"><code>${hljs.highlight(str, { language: lang, ignoreIllegals: true }).value}</code></pre>`;
} catch (__) {}
}
return `<pre class="hljs"><code>${md.utils.escapeHtml(str)}</code></pre>`;
}
})
.use(mk) // 啟用KaTeX數(shù)學(xué)公式支持
.use(breaks); // 啟用換行支持
// 配置鏈接在新窗口打開
const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
tokens[idx].attrPush(['target', '_blank']);
return defaultRender(tokens, idx, options, env, self);
};
// 計(jì)算渲染后的HTML
const renderedContent = computed(() => {
return md.render(props.content || '');
});
</script>
<style>
.markdown-body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
line-height: 1.6;
padding: 20px;
color: #24292e;
}
.markdown-body code {
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
background-color: rgba(27, 31, 35, 0.05);
border-radius: 3px;
padding: 0.2em 0.4em;
}
.markdown-body pre {
background-color: #f6f8fa;
border-radius: 3px;
padding: 16px;
overflow: auto;
}
.hljs {
padding: 0;
background: transparent;
}
</style>步驟三:使用組件
<template>
<div>
<h1>Markdown 預(yù)覽</h1>
<MarkdownView :content="markdownContent" />
</div>
</template>
<script setup>
import MarkdownView from '@/components/MarkdownView.vue'
import { ref } from 'vue'
const markdownContent = ref(`
# 標(biāo)題
這是一段普通文本
## 代碼示例
\`\`\`javascript
const hello = 'world';
console.log(hello);
\`\`\`
## 數(shù)學(xué)公式
$E = mc^2$
## 表格
| 姓名 | 年齡 |
| ---- | ---- |
| 張三 | 20 |
| 李四 | 25 |
`)
</script>
功能對(duì)照表
以下是React示例和Vue實(shí)現(xiàn)的功能對(duì)照:
| React功能 | Vue實(shí)現(xiàn)方式 |
|---|---|
| ReactMarkdown | markdown-it |
| remark-math | markdown-it-katex |
| remark-breaks | markdown-it-breaks |
| rehype-katex | markdown-it-katex (內(nèi)置支持) |
| remark-gfm | markdown-it (內(nèi)置支持GFM) |
| SyntaxHighlighter | highlight.js |
補(bǔ)充說(shuō)明
1.功能:
- Markdown渲染
- 數(shù)學(xué)公式支持
- 代碼高亮
- 表格支持
- 自動(dòng)鏈接
- 在新窗口打開鏈接
2.如果需要更多功能,可以添加其他markdown-it插件,例如:
npm install markdown-it-emoji markdown-it-sub markdown-it-sup markdown-it-footnote
3.要實(shí)現(xiàn)與GitHub樣式一致的顯示效果,可以添加:
npm install github-markdown-css
4.然后在main.js中導(dǎo)入:
import 'github-markdown-css/github-markdown.css'
到此這篇關(guān)于vue3中展示markdown格式文章的三種形式的文章就介紹到這了,更多相關(guān)vue3展示markdown內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue vantUI tab切換時(shí) list組件不觸發(fā)load事件的問題及解決方法
這篇文章主要介紹了vue vantUI tab切換時(shí) list組件不觸發(fā)load事件的解決辦法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
vue?+elementui?項(xiàng)目登錄通過不同賬號(hào)切換側(cè)邊欄菜單的顏色
這篇文章主要介紹了vue?+elementui?項(xiàng)目登錄通過不同賬號(hào)切換側(cè)邊欄菜單的顏色,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
Vue實(shí)現(xiàn)前后端完全分離的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了Vue實(shí)現(xiàn)前后端完全分離的項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
詳解關(guān)于表格合并span-method方法的補(bǔ)充(表格數(shù)據(jù)由后臺(tái)動(dòng)態(tài)返回)
這篇文章主要介紹了詳解關(guān)于表格合并span-method方法的補(bǔ)充(表格數(shù)據(jù)由后臺(tái)動(dòng)態(tài)返回) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-05-05
解決vue3項(xiàng)目中el-menu不兼容SSR問題
這篇文章主要介紹了解決vue3項(xiàng)目中el-menu不兼容SSR問題,需要的朋友可以參考下2023-12-12

