© Joe Attardi 2020
J. AttardiUsing Gatsby and Netlify CMShttps://doi.org/10.1007/978-1-4842-6297-9_5

5. The Netlify CMS Application

Joe Attardi1 
(1)
Billerica, MA, USA
 

We’ve installed and configured Netlify CMS, but how do we add posts to our blog? That’s where the Netlify CMS application comes in. When we installed the Netlify CMS Gatsby plugin, it configured a route in the Gatsby site, /admin, which will load the CMS user interface so that we can manage content.

Registering and logging in

To get started, open a web browser and go to the live version of the site at https://<your-site-name>.netlify.app/admin. You will see the Netlify CMS logo and a log-in button, as shown in Figure 5-1.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig1_HTML.jpg
Figure 5-1

The log-in screen

Click the “Login with Netlify Identity” button. A dialog will appear with two tabs: “Sign up” and “Log in.” Since we configured Netlify Identity to use open registration, we can sign up here for a new user account. Click the “Sign up” tab. Enter your name and email, and select a password, then click “Sign up.”
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig2_HTML.jpg
Figure 5-2

Signing up for a new account

After clicking the “Sign up” button, you will receive an email from Netlify to confirm your email address. Click the link in this email to complete the sign-up process. Once this is complete, you will be redirected to the main screen of the Netlify CMS Content Manager application. There isn’t much to see here yet, since we haven’t added any blog posts.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig3_HTML.jpg
Figure 5-3

The Netlify CMS application

On the left side of the screen is the list of collections. So far, we only have one collection, the Blog collection. The name that is displayed here is determined by the value of the label property in the collection configuration. As more collections are added, they will appear in this list.

Once we have created some blog posts, they will be listed in the center region. Currently it says “No Entries” because no posts have been created yet.

In the top right part of the screen is the “Quick add” button. This allows you to start the content creation process from any screen in the application.

Lastly, in the top left are two buttons: “Contents” and “Media.” This is used to switch between the main view and the media asset manager, which we will look at soon.

Creating a new blog post

Now that we’re logged in, let’s create a new blog post. In the center region of the screen, click the “New Blog” button. This will open a new editor where we can add the new blog post, as shown in Figure 5-4.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig4_HTML.jpg
Figure 5-4

The CMS editing interface

There are three fields shown in this editor: Title, Publish Date, and Body. These correspond to the three fields we configured in the CMS configuration file for the blog collection.

Let’s add a new post. First, enter a title in the Title field. The widget used for the Title field is string, which renders a single-line text field, as shown in Figure 5-5.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig5_HTML.jpg
Figure 5-5

The editor for the Title field editing interface

Next, let’s look at the Publish Date field. It has been prefilled with the current date and time. Click the field, and a date and time picker will appear.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig6_HTML.jpg
Figure 5-6

The date picker for the Publish Date field

For now, we can leave the date and time as is. Lastly, let’s add some content to the blog post. The Body field has a rich text editor. Figure 5-7 shows the different toolbar buttons that are available for formatting the text.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig7_HTML.jpg
Figure 5-7

The editor toolbar

The controls on the toolbar are as follows:
  1. 1.

    Toggles bold.

     
  2. 2.

    Toggles italic.

     
  3. 3.

    Toggles code/monospaced font.

     
  4. 4.

    Inserts a hyperlink.

     
  5. 5.

    Sets the heading style. There are six available heading levels.

     
  6. 6.

    Inserts a block quote.

     
  7. 7.

    Inserts an unordered list.

     
  8. 8.

    Inserts an ordered list.

     
  9. 9.

    Adds content to the text. Currently, the available options are images and code blocks.

     
  10. 10.

    Toggles between the formatted text and the Markdown source.

     
Go ahead and type a blog post in the Body field, using the various toolbar controls to format the text.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig8_HTML.jpg
Figure 5-8

Some content added to the Body field

If you prefer, you can also edit the raw Markdown source itself. To do this, simply click the toggle switch from “Rich Text” to “Markdown.” The content will transform into Markdown-formatted text.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig9_HTML.jpg
Figure 5-9

The Markdown source

Publishing the post

Finally, let’s publish this new post. Click the “Publish” button in the top right corner of the screen. This will open a dropdown menu with a few options. From this menu, click “Publish now.”
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig10_HTML.jpg
Figure 5-10

The publish menu

After a brief delay, the top of the screen will change to indicate that the new post was saved and published. The “Publish” button in the top right has changed to say “Published.”

Let’s return to the main screen of the CMS. To do this, click the back button in the upper left corner of the screen.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig11_HTML.jpg
Figure 5-11

The back button

Back at the main screen, you will now see the blog post we just added. It should look like Figure 5-12.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig12_HTML.jpg
Figure 5-12

The main screen showing the newly published blog post

How publishing works

We are using the Git Gateway backend, connected to GitHub. When we clicked the “Publish” button, here’s what happened:
  • A Markdown document was created for the blog post, containing the metadata in the front matter and the content in the body.

  • Using the GitHub API via the Git Gateway, a new commit was created containing this document on the master branch.

  • Since a new commit was created on the master branch, Netlify built and published a new version of the site containing the new blog post.

To see this, from the terminal, perform a git pull operation:
git pull origin master

You will see one new commit downloaded from GitHub. If you check the src directory, you’ll see a new directory called blog. Inside that directory is a new file. The filename should look something like 2020-06-21-welcome-to-the-coffee-blog.md. This filename is generated from the configuration file. Recall that for the blog collection, we set the slug pattern to {{year}}-{{month}}-{{day}}-{{slug}}.

If you open the new Markdown file in your editor, it will look something like what is shown in Listing 5-1.
---
title: Welcome to the Coffee Blog
date: 2020-06-21T18:26:40.185Z
---
# Hello!
Welcome to the **Coffee Blog**. This is our first blog post. Stay tuned for more information soon!
Listing 5-1

The Markdown file that was generated by the CMS

Adding media

The Netlify CMS application also includes an interface for managing media files. You can upload images and other media here.

To access the media manager, click the “Media” button at the top of the page.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig13_HTML.jpg
Figure 5-13

The toolbar containing the “Media” button

We haven’t added any media yet, so the media manager will be empty, as shown in Figure 5-14.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig14_HTML.jpg
Figure 5-14

The empty media manager

Let’s upload an image to add to our blog post. Open up your favorite image search engine and find an image of a cup of coffee, then save it to your computer.

Then, in the media manager, click the “Upload” button. Select the image that you downloaded in the file chooser that appears, and it will be added to your media library. This is done by using the Git Gateway to create a new commit in the repository containing the image – media files are also stored in the repository.

After uploading, the media manager will refresh, and you will see the new coffee image, as shown in Figure 5-15.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig15_HTML.jpg
Figure 5-15

The new image in the media manager

Alternative media storage options

As with the site content, all media is stored in your Git repository. After you upload an image, a new commit will be created in the repository which adds that image.

If your site has lots of media files, or large ones like audio and video, the Git repository can become quite large. Netlify CMS supports several solutions to this problem:
  • Cloudinary (https://cloudinary.com) and Uploadcare (https://uploadcare.com) are third-party CDNs for managing and distributing media content. Netlify CMS has native support for working with these services.

  • Netlify Large Media is another service offered by Netlify. It is a Git LFS (Large File Storage) service. Note that this service requires hosting your site on the Netlify platform.

Adding media to a blog post

Let’s add the image we just uploaded to the blog post we created previously. Click the X to close the media manager, then click the blog post in the list of content.

Inserting the image

Go to the beginning of the body content, then click the plus button on the toolbar. You will see the Add menu pop up, as shown in Figure 5-16.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig16_HTML.jpg
Figure 5-16

The Add toolbar menu

From the pop-up menu, click “Image.” This will add an image component to the text editor.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig17_HTML.jpg
Figure 5-17

The new image component

From here, click the “Choose an image” button. This will show the media manager. Click on the coffee cup image, and click the green “Choose selected” button in the top right of the dialog. You can also add alternative text and a title (tooltip) for the image.
../images/502348_1_En_5_Chapter/502348_1_En_5_Fig18_HTML.jpg
Figure 5-18

The editor with an image added

Publishing the updated blog post

The last step to update the blog entry is to republish it. As we did before, click “Publish,” then click “Publish now.” Once the updated blog post is published, Netlify will begin deploying the new version of the site containing the updated blog post.

While this is happening, go back to your terminal and perform another git pull operation:
git pull origin master

You will see that the blog post file was updated and a new file was added in the static/img directory. This is what we configured for the media_folder option in the CMS configuration file.

Summary

In this chapter, we
  • Created an account in the CMS application

  • Created and published a new blog post

  • Added an image to the blog post

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.47.25