next-tinacms-markdown

The next-tinacms-markdown package provides helpers to make local Markdown files editable. This package is intended to be used with a TinaCMS Git-based workflow.

Installation

yarn add next-tinacms-markdown

API

ExportDescription
MarkdownFileA interface representing a Markdown file stored on the local filesystem in Git.
useMarkdownFormReact Hook that creates a TinaCMS Form Plugin for editing a MarkdownFile.

MarkdownFile

The MarkdownFile interface represents a Markdown file stored on the local filesystem in Git.

export interface MarkdownFile {
  fileRelativePath: string
  frontmatter: any
  markdownBody: string
}
NameDescription
fileRelativePathThe path to the file relative to the root of the Git repository.
markdownBodyThe content body of from the Markdown file.
frontmatterThe parsed frontmatter data from the Markdown file.

useMarkdownForm

The useMarkdownForm function is a React Hook creates a TinaCMS Form Plugin for editing a MarkdownFile.

import { Form, FormOptions } from "tinacms"

export function useMarkdownForm(
  markdownFile: MarkdownFile,
  options?: FormOptions
):[any, Form]

The useMarkdownForm hook accepts a MarkdownFile and an optional FormOptions object. It returns an array with containing the current values aand the Form.

Usage

This package does not handle rendering the markdown content on the page. You can bring your own renderer or use react-markdown:

yarn add react-markdown

Example: pages/index.js

import { usePlugin } from 'tinacms'
import { useMarkdownForm } from 'next-tinacms-markdown'
import ReactMarkdown from 'react-markdown'

export default function Index({ markdownFile }) {
  // Create the Form
  const [homePage, homePageForm] = useMarkdownForm(markdownFile)

  // Register it with the CMS
  usePlugin(homePageForm)

  return (
    <>
      <h1>{homePage.title}</h1>
      <ReactMarkdown>{homePage.markdownBody}</ReactMarkdown>
    </>
  )
}

export async function getStaticProps() {
  const infoData = await import(`../content/info.md`)
  const data = matter(infoData.default)

  return {
    props: {
      markdownFile: {
        fileRelativePath: `content/info.md`,
        frontmatter: data.data,
        markdownBody: data.content,
      }
    },
  }
}

View on GitHub ->