跳转到内容

Decap CMS 和 Astro

Decap CMS(前身为 Netlify CMS)是一个开源的、基于 Git 的内容管理系统。

Decap 允许你充分利用 Astro 的所有功能,包括图像优化和内容集合。

Decap 会在你的项目中添加一个路由(通常是 /admin),该路由会加载一个 React 应用,允许授权用户直接从已部署的网站管理内容。Decap 会将更改直接提交到你的 Astro 项目的源仓库。

有两种方法可以将 Decap 添加到 Astro

  1. 使用以下命令通过包管理器安装 Decap

    终端窗口
    npm install decap-cms-app
  2. 将包导入到页面 <body> 中的 <script> 标签中

    /admin
    <body>
    <!-- Include the script that builds the page and powers Decap CMS -->
    <script src="https://unpkg.com/decap-cms@^3.1.2/dist/decap-cms.js"></script>
    </body>
  1. public/admin/ 创建一个静态的 admin 文件夹

  2. config.yml 添加到 public/admin/

    • 目录public
      • 目录admin
        • config.yml
  3. 要添加对内容集合的支持,请在 config.yml 中配置每个 schema。以下示例配置了一个 blog 集合,为每个条目的 frontmatter 属性定义了一个 label

    /public/admin/config.yml
    collections:
    - name: "blog" # Used in routes, e.g., /admin/collections/blog
    label: "Blog" # Used in the UI
    folder: "src/content/blog" # The path to the folder where the documents are stored
    create: true # Allow users to create new documents in this collection
    fields: # The fields for each document, usually in frontmatter
    - { label: "Layout", name: "layout", widget: "hidden", default: "blog" }
    - { label: "Title", name: "title", widget: "string" }
    - { label: "Publish Date", name: "date", widget: "datetime" }
    - { label: "Featured Image", name: "thumbnail", widget: "image" }
    - { label: "Rating (scale of 1-5)", name: "rating", widget: "number" }
    - { label: "Body", name: "body", widget: "markdown" }
  4. src/pages/admin.html 中为你的 React 应用添加 admin 路由。

    • 目录public
      • 目录admin
        • config.yml
    • 目录src
      • 目录pages
        • admin.html
    /src/pages/admin.html
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="robots" content="noindex" />
    <link href="/admin/config.yml" type="text/yaml" rel="cms-config-url" />
    <title>Content Manager</title>
    </head>
    <body>
    <script src="https://unpkg.com/decap-cms@^3.1.2/dist/decap-cms.js"></script>
    </body>
    </html>
  5. 要通过 Decap 编辑器启用媒体上传到特定文件夹,请添加适当的路径

    /public/admin/config.yml
    media_folder: "src/assets/images" # Location where files will be stored in the repo
    public_folder: "src/assets/images" # The src attribute for uploaded media

有关完整的说明和选项,请参阅 Decap CMS 配置文档

导航到 yoursite.com/admin/ 来使用 Decap CMS 编辑器。

Decap CMS 最初由 Netlify 开发,并对 Netlify Identity 提供一流的支持。

部署到 Netlify 时,通过 Netlify 仪表板为你的项目配置 Identity,并在项目的 admin 路由上包含 Netlify Identity Widget。如果你计划通过电子邮件邀请新用户,可以选择在你的网站主页上包含 Identity Widget。

部署到 Netlify 以外的托管提供商时,你必须创建自己的 OAuth 路由。

在 Astro 中,这可以通过在项目中配置并启用适配器的按需渲染路由来完成。

有关兼容的社区维护的 OAuth 客户端列表,请参阅 Decap 的 OAuth 文档

以下站点在生产环境中使用 Astro + Decap CMS

更多 CMS 指南

贡献 社区 赞助