Decap CMS 和 Astro
Decap CMS(前身为 Netlify CMS)是一个开源的、基于 Git 的内容管理系统。
Decap 允许你充分利用 Astro 的所有功能,包括图像优化和内容集合。
Decap 会在你的项目中添加一个路由(通常是 /admin
),该路由会加载一个 React 应用,允许授权用户直接从已部署的网站管理内容。Decap 会将更改直接提交到你的 Astro 项目的源仓库。
安装 DecapCMS
章节标题:“安装 DecapCMS”有两种方法可以将 Decap 添加到 Astro
-
使用以下命令通过包管理器安装 Decap
终端窗口 npm install decap-cms-app终端窗口 pnpm add decap-cms-app终端窗口 yarn add decap-cms-app -
将包导入到页面
<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>
-
在
public/admin/
创建一个静态的 admin 文件夹 -
将
config.yml
添加到public/admin/
目录public
目录admin
- config.yml
-
要添加对内容集合的支持,请在
config.yml
中配置每个 schema。以下示例配置了一个blog
集合,为每个条目的 frontmatter 属性定义了一个label
/public/admin/config.yml collections:- name: "blog" # Used in routes, e.g., /admin/collections/bloglabel: "Blog" # Used in the UIfolder: "src/content/blog" # The path to the folder where the documents are storedcreate: true # Allow users to create new documents in this collectionfields: # 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" } -
在
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> -
要通过 Decap 编辑器启用媒体上传到特定文件夹,请添加适当的路径
/public/admin/config.yml media_folder: "src/assets/images" # Location where files will be stored in the repopublic_folder: "src/assets/images" # The src attribute for uploaded media
有关完整的说明和选项,请参阅 Decap CMS 配置文档。
导航到 yoursite.com/admin/
来使用 Decap CMS 编辑器。
身份验证
章节标题:“身份验证”Decap CMS 与 Netlify Identity
章节标题:“Decap CMS 与 Netlify Identity”Decap CMS 最初由 Netlify 开发,并对 Netlify Identity 提供一流的支持。
部署到 Netlify 时,通过 Netlify 仪表板为你的项目配置 Identity,并在项目的 admin
路由上包含 Netlify Identity Widget。如果你计划通过电子邮件邀请新用户,可以选择在你的网站主页上包含 Identity Widget。
Decap CMS 与外部 OAuth 客户端
章节标题:“Decap CMS 与外部 OAuth 客户端”部署到 Netlify 以外的托管提供商时,你必须创建自己的 OAuth 路由。
在 Astro 中,这可以通过在项目中配置并启用适配器的按需渲染路由来完成。
有关兼容的社区维护的 OAuth 客户端列表,请参阅 Decap 的 OAuth 文档。
社区资源
章节标题:“社区资源”-
Netlify Identity 模板:astro-decap-ssg-netlify
-
使用 Astro 模板的按需渲染 OAuth 路由:astro-decap-starter-ssr
-
博客文章:Aftab Alam 的 使用基于 Git 的 CMS 创作你的 Astro 网站内容
-
YouTube 教程:Kumail Pirzada 的 在几分钟内用 Astro 和 NetlifyCMS 创建一个自定义博客!
生产站点
章节标题:“生产站点”以下站点在生产环境中使用 Astro + Decap CMS
- yunielacosta.com by Yuniel Acosta — GitHub 上的源代码 (Netlify CMS)
- portfolioris.nl by Joris Hulsbosch – GitHub 上的源代码