#Using Github as a database

5 messages · Page 1 of 1 (latest)

onyx bronze
#

So, I have a github repo that stores my markdown files. It was working until I wanted to access the content. Right now I am using the basic gray matter and stuff to get and render local markdown files, but how can I get it from the github? I am having trouble with that part. Unless anyone knows a better way of storing markdown files. I just don';t want to store all my markdown files on the site.

#
export async function getStaticProps() {
  const postsDirectory = path.join(process.cwd(), 'posts/blogs');
  const filenames = fs.readdirSync(postsDirectory);

  const posts = filenames.map((filename) => {
    const filePath = path.join(postsDirectory, filename);
    const fileContent = fs.readFileSync(filePath, 'utf8');
    const { data, content } = matter(fileContent);
    return {
      slug: filename.replace('.md', ''),
      data,
      content,
    };
  });

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

This is how I am getting the local files

#

here is what I use to get the stuff for the "post" part but I have no clue how to get access to the content

import axios from 'axios';

export async function getStaticProps() {
  const { data: files } = await axios.get(
    'https://api.github.com/repos/Kxffie/markdown_storage/contents'
  );

  const postPromises = files.map(async ({ name, sha, download_url }) => {
    const { data: fileContent } = await axios.get(download_url);
    const { data, content } = matter(fileContent);
    return {
      slug: name.replace('.md', ''),
      data,
      content,
    };
  });

  const posts = await Promise.all(postPromises);

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

there is the download_url but in my slug part im not sure what to do

#

wait i got it