Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Huw Fulcher
Huw Fulcher

Posted on

     

How to integrate TinaCMS with Jekyll

Originally posted athuwfulcher.com

Introduction

TinaCMS, the successor toForestry has just gone to v1.0, bringing a stable, open-source, git based CMS to static sites. I've just started using TinaCMS withmy site and I'm actually writing this post using it!

While there issome documentation on adding TinaCMS to a non-NextJS site there are few extra steps that are needed to get it working nicely with Jekyll.

1. Install TinaCMS

  1. npx @tinacms/cli@latest initNote: Docs as of (10/11/2022) specify a --static flag, this is removed and won't work
  2. Choose whatever package manager is preferable
  3. ChooseOther when asked for a framework
  4. (Optional) Use Typescript, it is recommended
  5. When asked where public assets are stored specify the top level with/. Jekyll considers all folders that are not prefixed with_ to be public. Storing tina in theassets folder will cause issues with pathing later on.

2. Configure TinaCMS

a) Update package.json

Replace the contentscripts with the following (or add based on what you need):

"dev": "tinacms dev -c \"bundle exec jekyll serve\"","build": "tinacms build && jekyll build",
Enter fullscreen modeExit fullscreen mode

Runnpm run dev to check no errors are present. You should be able to navigate tolocalhost:4000/admin/ and see the TinaCMS interface.

Theadmin folder should be at the top level of the directory.

b) Edit schema

At this point TinaCMS is looking atcontent/posts for the.md files to display and edit. For Jekyll this path should be_posts/. To fix this we can edit theconfig.ts file in.tina.

i. Update media folder

TinaCMS supports uploading and embedding media in your markdown documents from the web editor. Themedia property inconfig.ts controls where this folder is placed. For Jekyll, this should be underassets. Changemedia to:

media: {  tina: {    mediaRoot: "uploads",    publicFolder: "assets",  },},
Enter fullscreen modeExit fullscreen mode

You can change themediaRoot name to whatever you like, this specifies the folder name underassets.

ii. Update schema path

Underschema the different types of content are defined as a list ofcollections. The only collection present when first creating TinaCMS ispost. Each collection will have apath variable that defines where TinaCMS should look for the associated files. Update this to_posts.

Once done yourcollections should look like this:

collections: [  {    name: "post",    label: "Posts",    path: "_posts",    fields: [      {        type: "string",        name: "title",        label: "Title",        isTitle: true,        required: true,      },      {        type: "rich-text",        name: "body",        label: "Body",        isBody: true,      },    ],  },],
Enter fullscreen modeExit fullscreen mode

iii. Tidy up existing posts

Depending on if you're doing a fresh Jekyll install or already have an existing blog, TinaCMS prefers filenames to be.md rather than.markdown. Update those accordingly.

Now when you accesslocalhost:4000/admin/ and gotPosts you should see your existing markdown files.

iv. Updating the schema

At this point you can happily use TinaCMS to update or create new markdown files but it will be restricted to only being able to interact with the title and body. If you have any additional frontmatter it will ignore it. To resolve this you can update thefields section of yourconfig.ts. Using the starter jekyll site I've created one below:

fields: [  {    type: "string",    name: "layout",    label: "Layout",    required: true,  },  {     type: "string",    name: "title",    label: "Title",    isTitle: true,    required: true,  },  {     type: "datetime",    name: "date",    label: "Date",    required: true,  },  {     type: "string",    name: "categories",    label: "Categories",  },  {    type: "rich-text",    name: "body",    label: "Body",    isBody: true,  },],
Enter fullscreen modeExit fullscreen mode

Go ahead and try creating a new markdown file using the editor. You should see it appear in_posts but there's a snag... Jekyll requires a specific naming convention to know which markdown files it should show:

YEAR-MONTH-DAY-title.md

Thankfully, TinaCMS allows us to customise our own filename conventions. Add the following to yourpost collection just afterpath:

ui: {  filename: {    readonly: false,    slugify: values => {      const date = new Date();      const day = date.getDate();      const month = date.getMonth() + 1;      const year = date.getFullYear();      let currentDate = `${year}-${month}-${day}`;      return `${currentDate}-${values?.title?.toLowerCase().replace(/ /g, '-')}`    }  }},
Enter fullscreen modeExit fullscreen mode

This will take the title you specify, slugify it and prefix it with the current date. When you save files they will be in the correct format for Jekyll to find!

Wrapping up

This just scratches the surface of the customisation you can do with TinaCMS. It has the ability to add custom components, fields, templates and a whole lot more. If something in this guide doesn't work get in touch with any of the social links below and I'll see if I can help.

References

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
polynar profile image
David Polynar
  • Joined

Thanks for the article! What can you say about drafts, how to solve them? If I put the md file without a date in the posts folder, can it work?

CollapseExpand
 
polynar profile image
David Polynar
  • Joined

Maybe can I create a boolean field: published and add it to the md's front matter?

CollapseExpand
 
huwfulcher profile image
Huw Fulcher
Ex-Data Scientist. Now Software Engineer and MLOps specialist.
  • Email
  • Location
    Cardiff
  • Education
    MSc Data Science & Analytics, BSc Computer Science
  • Work
    Senior Software Engineer
  • Joined

Hey David! Yes you can use a field called published in the front matter.

Jekyll recognises this automatically so there's no need for custom logic to filter out drafts!

jekyllrb.com/docs/front-matter/

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Ex-Data Scientist. Now Software Engineer and MLOps specialist.
  • Location
    Cardiff
  • Education
    MSc Data Science & Analytics, BSc Computer Science
  • Work
    Senior Software Engineer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp