diff options
| -rw-r--r-- | pages/logs/index.tsx | 37 | ||||
| -rw-r--r-- | utils/Posts.tsx | 57 | 
2 files changed, 89 insertions, 5 deletions
| diff --git a/pages/logs/index.tsx b/pages/logs/index.tsx index 7f11e86..ed3c58b 100644 --- a/pages/logs/index.tsx +++ b/pages/logs/index.tsx @@ -1,16 +1,43 @@  import DefaultPage from '../../templates/Default';  import Markdown from '../../components/Markdown'; + +import { GetStaticProps, GetStaticPropsContext } from 'next'; +import React, { FC } from 'react'; +import { getPosts, getMetadata, Post, PostMetadata } from '../../utils/Posts';  //@ts-ignore  import md from './logs.md'; -export default function Logs() { + +interface Props { +  posts: Post[] +}; + +const Logs : FC<Props> = ({ posts }) => { +  console.log(posts);    return(      <DefaultPage        path={"/logs"}      > -      <Markdown -        md={md} -      /> +      <ul> +      { +        posts.map(({ directory, meta }) => ( +          <li><a href={'/logs/' + directory}>{meta.name}</a></li> +        )) +      } +      </ul>      </DefaultPage>    ); -}
\ No newline at end of file +}; + +export default Logs; + + +export const getStaticProps: GetStaticProps = async ( +  context: GetStaticPropsContext +) => { +  const posts = await getPosts(); + +  return { +    props: { posts: posts }, +  } +} diff --git a/utils/Posts.tsx b/utils/Posts.tsx new file mode 100644 index 0000000..e82357a --- /dev/null +++ b/utils/Posts.tsx @@ -0,0 +1,57 @@ +import { promises as fs } from 'fs'; +import path from 'path'; + +interface PostMetadata { +  name: string; +} +   +interface Post { +  directory: string; +  path: string; +  meta: PostMetadata; +} + +export type { +  PostMetadata, +  Post +}; + +async function getMetadata(postPath : string) : Promise<PostMetadata> { +  const metaPath = path.join(postPath, 'meta.json'); + +  const postMetadata = await fs.readFile(metaPath, 'utf8'); + +  return JSON.parse(postMetadata) as PostMetadata; +} + +async function getPosts() : Promise<Post[]> { +  const postsDirectory = path.join(process.cwd(), 'posts'); +  const directories = await fs.readdir(postsDirectory); + +  const posts = directories.map(async (directory) => { +    const postPath = path.join(postsDirectory, directory); +     +    const meta = await getMetadata(postPath); + +    return { +      directory, +      path: postPath, +      meta  +    }; +  }); + +  return await Promise.all(posts); +} + +async function getMarkdown(post : Post) : Promise<string> { +  const markdownPath = path.join(post.path, 'main.md'); + +  const markdown = await fs.readFile(markdownPath, 'utf8'); + +  return markdown; +} + +export { +  getPosts, +  getMetadata, +};
\ No newline at end of file | 
