next-github-source
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

next-github-source

Use GitHub repo as CMS in Next.js (similar Hashnode's Github as source)

Warning This package is early in development. Do NOT use in production environment.

🚀 Quick Start

npm install next-github-source

lib/githubSourceClient.js

import { createClient } from "next-github-source";

export const client = createClient({
  repo: {
    remote: `https://github.com/Hashnode/Hashnode-source-from-github-template`,
  },
  pattern: [`**`, "!README.md"], // list all except README.md
});

pages/index.js

import Link from "next/link";
import { client } from "../../lib/githubSourceClient";

export const getStaticProps = async () => {
  const posts = await client.getAllEntries();

  return {
    props: { posts },
  };
};

export default function Posts({ posts }) {
  return (
    <div>
      {posts.map((post, idx) => (
        <Link key={idx} href={`/posts/${post.slug}`}>
          <a>
            [{post.source}]({post.slug})
          </a>
        </Link>
      ))}
    </div>
  );
}

pages/posts/[slug].js

import { client } from "../../lib/githubSourceClient";

export const getStaticPaths = async () => {
  return {
    paths: await client.getAllEntryPaths(),
    fallback: false,
  };
};

export const getStaticProps = async ({ params }) => {
  const post = await client.getEntry(params.slug);

  return {
    props: { post },
  };
};

export default function Post({ post }) {
  return <div>{JSON.stringify(post)}</div>;
}

TODO

  • [ ] Examples with next-mdx-remote
  • [ ] On-demand revalidation support with examples
  • [ ] Matter support with types & validation
  • [ ] High test coverage
  • [ ] Docs

License

MIT License © 2022 Sinan Bekar

Package Sidebar

Install

npm i next-github-source

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

12.3 kB

Total Files

5

Last publish

Collaborators

  • sinanbekar