使用Vite2+Vue3渲染Markdown文檔的方法實(shí)踐
大部分使用 Vite2 的開(kāi)發(fā)者遇到的一個(gè)問(wèn)題,就是文檔里并沒(méi)有相關(guān)支持 Markdown 的介紹,那么如果想要在 Vite 項(xiàng)目中支持 Markdown 引入并渲染,需要怎么操作?這篇文章將介紹兩種方式。
自定義 Vite 插件
如果去網(wǎng)上相關(guān)問(wèn)題,大部分都是這種方式,就是自定義插件,使得 Vite 支持 Markdown 渲染,具體做法如下:
在項(xiàng)目根目錄創(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: [ // 用于開(kāi)發(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()] };
這樣,在使用時(shí),會(huì)將導(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'], }) ] }
配置中傳入一個(gè) options,選項(xiàng)對(duì)象,支持以下參數(shù):
mode?: ('html' | 'toc' | 'react' | 'vue')[]
markdown?: (body: string) => string
markdownIt?: MarkdownIt | MarkdownIt.Options
各個(gè)模式下的渲染示例:
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>
寫(xiě)在最后
到此這篇關(guān)于使用Vite2+Vue3渲染Markdown文檔的方法實(shí)踐的文章就介紹到這了,更多相關(guān)Vite2+Vue3渲染Markdown內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue代理請(qǐng)求數(shù)據(jù)出現(xiàn)404問(wèn)題及解決
這篇文章主要介紹了Vue代理請(qǐng)求數(shù)據(jù)出現(xiàn)404的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vant 時(shí)間選擇器--開(kāi)始時(shí)間和結(jié)束時(shí)間實(shí)例
這篇文章主要介紹了vant 時(shí)間選擇器--開(kāi)始時(shí)間和結(jié)束時(shí)間實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11解決vue過(guò)濾器filters獲取不到this對(duì)象的問(wèn)題
這篇文章主要介紹了解決vue過(guò)濾器filters獲取不到this對(duì)象的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Vue項(xiàng)目使用Websocket大文件FileReader()切片上傳實(shí)例
這篇文章主要介紹了Vue項(xiàng)目使用Websocket大文件FileReader()切片上傳實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue項(xiàng)目前端埋點(diǎn)的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目前端埋點(diǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Element?UI?Dialog對(duì)話框改成固定高度超出部分滾動(dòng)條滾動(dòng)
這篇文章主要給大家介紹了關(guān)于Element?UI?Dialog對(duì)話框改成固定高度超出部分滾動(dòng)條滾動(dòng)的相關(guān)資料,el-dialog默認(rèn)高度是自由拉伸的,當(dāng)內(nèi)容超過(guò)屏幕時(shí)會(huì)出現(xiàn)滾動(dòng)條,按鈕和標(biāo)題都會(huì)隨著滾動(dòng),用戶體驗(yàn)不好,需要的朋友可以參考下2024-05-05淺談Vue路由快照實(shí)現(xiàn)思路及其問(wèn)題
這篇文章主要介紹了淺談Vue路由快照實(shí)現(xiàn)思路及其問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06