使用Vite2+Vue3渲染Markdown文檔的方法實踐
大部分使用 Vite2 的開發(fā)者遇到的一個問題,就是文檔里并沒有相關(guān)支持 Markdown 的介紹,那么如果想要在 Vite 項目中支持 Markdown 引入并渲染,需要怎么操作?這篇文章將介紹兩種方式。
自定義 Vite 插件
如果去網(wǎng)上相關(guān)問題,大部分都是這種方式,就是自定義插件,使得 Vite 支持 Markdown 渲染,具體做法如下:
在項目根目錄創(chuàng)建 md.js 文件,填充如下內(nèi)容:
import path from 'path';
import fs from 'fs';
import marked from 'marked';
const mdToJs = str => {
const content = JSON.stringify(marked(str));
return `export default ${content}`;
};
export function md() {
return {
configureServer: [ // 用于開發(fā)
async ({ app }) => {
app.use(async (ctx, next) => {
// 獲取后綴為 .md 的文件,將他變?yōu)?js 文件
if (ctx.path.endsWith('.md')) {
ctx.type = 'js';
const filePath = path.join(process.cwd(), ctx.path);
ctx.body = mdToJs(fs.readFileSync(filePath).toString());
} else {
await next();
}
});
},
],
transforms: [{ // 用于 rollup
test: context => context.path.endsWith('.md'),
transform: ({ code }) => mdToJs(code)
}]
};
}
接著修改 vite.config.js,引入上面創(chuàng)建的插件。
import {md} from './md';
export default {
plugins: [md()]
};
這樣,在使用時,會將導(dǎo)入的 md 文件轉(zhuǎn)換成 js 文件渲染。具體使用示例:
<template>
<article v-html="md" />
</template>
<script lang="ts">
import md from './xxx.md'
export default {
setup(){
return {md}
}
}
使用 vite-plugin-markdown
這款第三方插件不僅支持引入并渲染 Markdown 文件,并且支持渲染成各種格式,例入 HTML 字符串、React 或 Vue 的組件等。
安裝 vite-plugin-markdown。
npm i vite-plugin-markdown
在 vite.config.js 中修改:
const mdPlugin = require('vite-plugin-markdown')
export default {
plugins: [
mdPlugin.plugin({
mode: ['html'],
})
]
}
配置中傳入一個 options,選項對象,支持以下參數(shù):
mode?: ('html' | 'toc' | 'react' | 'vue')[]
markdown?: (body: string) => string
markdownIt?: MarkdownIt | MarkdownIt.Options
各個模式下的渲染示例:
Import Front Matter attributes
---
title: Awesome Title
description: Describe this awesome content
tags:
- "great"
- "awesome"
- "rad"
---
# This is awesome
Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.
import { attributes } from './contents/the-doc.md';
console.log(attributes) //=> { title: 'Awesome Title', description: 'Describe this awesome content', tags: ['great', 'awesome', 'rad'] }
Import compiled HTML (Mode.HTML)
import { html } from './contents/the-doc.md';
console.log(html)
//=> "<h1>This is awesome</h1><p>ite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.</p>"
Import ToC metadata (Mode.TOC)
# vite
Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.
## Status
## Getting Started
# Notes
import { toc } from './contents/the-doc.md'
console.log(toc)
//=> [{ level: '1', content: 'vite' }, { level: '2', content: 'Status' }, { level: '2', content: 'Getting Started' }, { level: '1', content: 'Notes' },]
Import as a React component (Mode.REACT)
import React from 'react'
import { ReactComponent } from './contents/the-doc.md'
function MyReactApp() {
return (
<div>
<ReactComponent />
</div>
}
Import as a Vue component (Mode.VUE)
<template>
<article>
<markdown-content />
</article>
</template>
<script>
import { VueComponent } from './contents/the-doc.md'
export default {
components: {
MarkdownContent: VueComponent
}
};
</script>
寫在最后
到此這篇關(guān)于使用Vite2+Vue3渲染Markdown文檔的方法實踐的文章就介紹到這了,更多相關(guān)Vite2+Vue3渲染Markdown內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue代理請求數(shù)據(jù)出現(xiàn)404問題及解決
這篇文章主要介紹了Vue代理請求數(shù)據(jù)出現(xiàn)404的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue項目使用Websocket大文件FileReader()切片上傳實例
這篇文章主要介紹了Vue項目使用Websocket大文件FileReader()切片上傳實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Element?UI?Dialog對話框改成固定高度超出部分滾動條滾動
這篇文章主要給大家介紹了關(guān)于Element?UI?Dialog對話框改成固定高度超出部分滾動條滾動的相關(guān)資料,el-dialog默認高度是自由拉伸的,當內(nèi)容超過屏幕時會出現(xiàn)滾動條,按鈕和標題都會隨著滾動,用戶體驗不好,需要的朋友可以參考下2024-05-05

