Make a digital diary with Netlify CMS
Written or Updated on August 05, 2022 ποΈ
Netlify CMS
Netlify CMS is open source Git-based Headless CMS which is provided by Netlify.
All contents are pushed to Github repository and multiple users can post and edit contents via admin UI.
What is Git-based CMS? There are roughly two types of Headless CMS,
- γ»API-based
- γ»Git-based
In Api-based CMS, we end up depending on the platform of CMS.
On the other hand, Git-based uses git repository as a DB, and CMS does βpushβ to git repository instead of us.
Itβs just like a mediator between Github and us.
Git-based is simpler, free and everything is under our control.
Personally, I prefer to use Git-based CMS so Iβm gonna use it in this article.
What weβre gonna make
Make a simple digital diary using Netlify CMS.
This is a very minimum structure, so I hope itβs easy to understand.
Step 1
First things first. Letβs make a new repository on Github and publish it on Netlify.
Just a simple HTML file is enough this time because the CMS part is the main topic.
*
βββ public
βββ index.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Diary</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Make a new repository then push our commit.
git init
git add .
git commit -m 'init'
git remote add origin master <your repository address>
git push -u origin master
Publish on Netlify.
No build command and publish directory is βpublicβ
Step 2
OK, letβs tackle CMS settings.
Make a new directory βadminβ inside βpublicβ then create index.html.
*
βββ public/
βββ index.html
β
βββ admin/
βββ index.html
There are only 3 things to do in index.html
- γ»Load βnetlify-identity-widgetβ for login feature.
- γ»Load Netlify CMS file.
- γ»Set up redirect path for after login.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Content Manager</title>
<!-- 1. Load "netlify-identity-widget" for login feature. -->
<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</head>
<body>
<!-- 2. Load Netlify CMS file. -->
<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js"></script>
<!-- 3. Set up redirect path for after login. -->
<script>
if (window.netlifyIdentity) {
window.netlifyIdentity.on("init", user => {
if (!user) {
window.netlifyIdentity.on("login", () => {
document.location.href = "/admin/";
});
}
});
}
</script>
</body>
</html>
We need to load βnetlify-identity-widgetβ in public/index.html as well.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- Load "netlify-identity-widget" for login feature. -->
<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Step 3
Inside βadminβ directory, create the setting file called config.yml. This setting file is the key but I struggled at first because itβs a bit tricky.
*
βββ public/
βββ index.html
β
βββ admin/
βββ index.html
βββ config.yml
backend:
name: git-gateway
branch: master
media_folder: "./"
public_folder: "./"
collections:
- name: "diary"
label: "Diary"
folder: "diary"
create: true
slug: "{{title}}"
path: "{{year}}/{{month}}/{{slug}}"
fields:
- { label: "Title", name: "title", widget: "string" }
- { label: "Date", name: "date", widget: "datetime" }
- { label: "Body", name: "body", widget: "markdown" }
Iβm gonna explain one by one.
The first setting is for a declaration that we use Github repository and specifying the branch name.
backend:
name: git-gateway
branch: master
Next setting, media_folder and public-folder, is for specifying where we store upload files.
For example, you want to store images like the following,
*
βββ public/
βββ index.html
β
βββ admin/
β βββ index.html
β βββ config.yml
β
βββ static/
βββ image/
set like this.
media_folder: "public/static/image"
public_folder: "/image"
Iβm not gonna upload images this time, so I leave them blank.
Before moving on to the next setting, letβs think about where and how to store diaries.
For this time, letβs say there are year directories and there gonna be month directories underneath, then we store a diary in a month directory.
*
βββ public/
β βββ index.html
β β
β βββ admin/
β β βββ index.html
β β βββ config.yml
β β
β βββ static/
β βββ image/
β
βββ diary/
βββ 2020/
β βββ 11/
β βββ 12/
β
βββ 2021/
βββ 01/
*These directories are automatically created, so we donβt have to prepare them beforehand.
Next is the last setting.
collections:
- name: "diary"γ
label: "Diary"
folder: "diary"
create: true
slug: "{{title}}"
path: "{{year}}/{{month}}/{{slug}}"
fields:
- { label: "Title", name: "title", widget: "string" }
- { label: "Body", name: "body", widget: "markdown" }
Each property represents the followings,
- name: Post type identifier. Must be unique.
- label: What the admin UI calls the post type.
- folder: Where files of this type are stored, relative to the repo root.
- create: Set to true to allow users to create new files in this collection.
- slug: Template for filenames.
- path: Path to store files.
- fields: Fields in the content editor
{{title}} refers βtitleβ of fields but {{year}} and {{month}} are default templates which Netlify CMS provides. It will be automatically populated with our posting datetime.
Letβs push to GitHub again.
Netlify will republish a new version of our website, triggered by git push.
git add .
git commit -m 'set netlify cms'
git push
Step 4
Itβs almost done! But there is one more thing to do to enable the login feature.
From the site page on Netlify > Site settings > Identity
Click [Enable Identity].
Scroll down and also enable Git gateway.
Now we access admin UI, finally.
https://<YOUR_APP_NAME>.netlify.app/admin/
We need to set up an account at first. You will get the verification email after sign-up so verify your account by clicking the link inside the email.
Welcome to admin UI! We can see βDiaryβ specified in config.yml. Letβs click βNew diaryβ and post a new one.
We can also see βTitleβ and βBodyβ specified in config.yml.
Write title and body then click βpublishβ.
Go check Github repository. Ta-da! Itβs created in the right directory.
We can see it in the console as well.
You can use Headless CMS partially
That was very easy, wasnβt it? All you care about is just config.yml.
As you might have noticed, you can use Headless CMS partially in your project.
That flexibility is one of the Pros of Headless CMS.
Schema is also flexible, You can use array and object or nested one.
Please refer official document and try other cool features by yourself :)