跳到內容

連線至 CMS

一般工作流程

將 VitePress 連線至 CMS 主要會圍繞在 動態路由。請務必在繼續之前了解其運作方式。

由於每個 CMS 的運作方式不同,因此我們在此只能提供一個一般工作流程,您需要根據您的特定情境進行調整。

  1. 如果您的 CMS 需要驗證,請建立一個 .env 檔案來儲存您的 API 令牌,並載入它

    js
    // posts/[id].paths.js
    import { loadEnv } from 'vitepress'
    
    const env = loadEnv('', process.cwd())
  2. 從 CMS 取得必要的資料,並將其格式化為適當的路徑資料

    js
    export default {
      async paths() {
        // use respective CMS client library if needed
        const data = await (await fetch('https://my-cms-api', {
          headers: {
            // token if necessary
          }
        })).json()
    
        return data.map(entry => {
          return {
            params: { id: entry.id, /* title, authors, date etc. */ },
            content: entry.content
          }
        })
      }
    }
  3. 渲染頁面中的內容

    md
    # {{ $params.title }}
    
    - by {{ $params.author }} on {{ $params.date }}
    
    <!-- @content -->

整合指南

如果您撰寫了有關將 VitePress 與特定 CMS 整合的指南,請使用下方「編輯此頁面」連結在此提交!

在 MIT 授權下發佈。