来完成。 netlify 支持在网站 html 中注入代码:进入 netlify.app ,选择 site-settings ,找到 build & deploy 中的 Snippet injection ,选择在 </head>
或 </body>
标签前加入代码。
接下来我们来看后台的使用。
upd:netlify-cms 已经被弃用,现在使用 decap-cms。
进入 netlify.app ,选择 site-settings ,找到 build & deploy 中的 Snippet injection ,在 </body>
标签前加入:1
2
3
4
5
6
7
8
9
10
11
12
13<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
<script>
if (window.netlifyIdentity) {
window.netlifyIdentity.on("init", user => {
if (!user) {
window.netlifyIdentity.on("login", () => {
document.location.href = "/admin/";
});
}
});
}
</script>
<!--netlify cms 有一个小 bug ,登录时需要手动刷新。这是自动刷新的代码-->
然后在 source
文件夹下新建一个文件夹:source/admin
,再建两个文件:source/admin/index.html
,source/admin/config.yml
分别编辑如下: config.yml index.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36backend:
name: github
repo: # repo name
branch: main # Branch to update (optional; defaults to master)
site_url: https://shwst.one
display_url: https://shwst.one
# logo_url: https://images.shwst.one/
locale: 'cn'
# This line should *not* be indented
publish_mode: editorial_workflow
# This line should *not* be indented
media_folder: "source/images" # Media files will be stored in the repo under images/uploads
public_folder: "source"
# A list of collections the CMS should be able to edit
collections:
# Used in routes, ie. : /admin/collections/:slug/edit
- name: "posts"
# Used in the UI, ie.: "New Post"
label: "Post"
folder: "source/_posts" # The path to the folder where the documents are stored
sort: "date:desc"
create: true # Allow users to create new documents in this collection
editor:
preview: true
fields: # The fields each document in this collection have
- {label: "Title", name: "title", widget: "string"}
- {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD HH:mm:ss", date_format: "YYYY-MM-DD", time_format: "HH:mm:ss", required: false}
- {label: "Tags", name: "tags", widget: "list", required: false}
- {label: "Categories", name: "categories", widget: "list", required: false}
- {label: "Body", name: "body", widget: "markdown", required: false}
- {label: "Comments", name: "comments", widget: "boolean", default: true, required: false}
- {label: "Top", name: "top", widget: "number", value_type: "int", default: 2, required: false}1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="noindex" />
<title>Content Manager</title>
</head>
<body>
<!-- Include the script that builds the page and powers Decap CMS -->
<script src="https://unpkg.com/decap-cms@^3.0.0/dist/decap-cms.js"></script>
</body>
</html>
注意 backend
中的 repo & branch
要与你的实际分支相匹配。
然后在 netlify 中找到 site-settings-identity ,找到 git-gateway ,点击启用,选择 Blog 仓库。
这样你就可以在 你的网址/admin
中看到登陆界面了。
另外,如果出现权限问题,请按照 Netlify-docs 配置 auth。
自己注册一个 admin 账户,然后在 identity 中关闭注册,只允许邀请,保证安全。
ok 。尽情享受 CMS 吧!
decap CMS 不支持 latex ,并且 bug 较多,我以后有时间可能会把 Luogu 的 Markdown*Palettes 嵌入到 netlify 的 markdown 编辑器里,自己做一个小部件,或者哪位大佬帮我写一下吧 qwq 。