From d4c57f9174adebc384eca99bceb2f2935cf31de6 Mon Sep 17 00:00:00 2001 From: Corwin Smith Date: Wed, 30 Nov 2022 15:02:08 +0100 Subject: [PATCH] add lastModified date (#68) * add lastModified date * prettier * fix type errors on build Co-authored-by: Paul Wackerow <54227730+wackerow@users.noreply.github.com> --- src/pages/[...slug].tsx | 20 ++++++++++++++++---- src/utils/getParsedDate.ts | 9 +++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pages/[...slug].tsx b/src/pages/[...slug].tsx index 74711922ea..cb0d69cefc 100644 --- a/src/pages/[...slug].tsx +++ b/src/pages/[...slug].tsx @@ -1,7 +1,7 @@ import fs from 'fs'; import matter from 'gray-matter'; import yaml from 'js-yaml'; -import { Stack, Heading } from '@chakra-ui/react'; +import { Stack, Heading, Text } from '@chakra-ui/react'; import ChakraUIRenderer from 'chakra-ui-markdown-renderer'; import ReactMarkdown from 'react-markdown'; import gfm from 'remark-gfm'; @@ -12,6 +12,7 @@ import MDXComponents from '../components/'; import { Breadcrumbs } from '../components/docs'; import { PageMetadata } from '../components/UI'; import { textStyles } from '../theme/foundations'; +import { getParsedDate } from '../utils'; const MATTER_OPTIONS = { engines: { @@ -49,11 +50,14 @@ export const getStaticProps: GetStaticProps = async context => { const { slug } = context.params as ParsedUrlQuery; const filePath = (slug as string[])!.join('/'); let file; + let lastModified; try { file = fs.readFileSync(`${filePath}.md`, 'utf-8'); + lastModified = fs.statSync(`${filePath}.md`); } catch { file = fs.readFileSync(`${filePath}/index.md`, 'utf-8'); + lastModified = fs.statSync(`${filePath}/index.md`); } const { data: frontmatter, content } = matter(file, MATTER_OPTIONS); @@ -61,7 +65,12 @@ export const getStaticProps: GetStaticProps = async context => { return { props: { frontmatter, - content + content, + lastModified: getParsedDate(lastModified.mtime, { + month: 'numeric', + day: 'numeric', + year: 'numeric' + }) } }; }; @@ -71,9 +80,10 @@ interface Props { [key: string]: string; }; content: string; + lastModified: string; } -const DocPage: NextPage = ({ frontmatter, content }) => { +const DocPage: NextPage = ({ frontmatter, content, lastModified }) => { return ( <> @@ -84,7 +94,9 @@ const DocPage: NextPage = ({ frontmatter, content }) => { {frontmatter.title} - {/* last edited {TODO: get last edited date} */} + + last edited {lastModified} + {content} diff --git a/src/utils/getParsedDate.ts b/src/utils/getParsedDate.ts index 5c87738c12..87bca62789 100644 --- a/src/utils/getParsedDate.ts +++ b/src/utils/getParsedDate.ts @@ -1,12 +1,13 @@ -export const getParsedDate = (date: string) => { - const dateOptions = { +export const getParsedDate = ( + date: string | Date, + dateOptions: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZone: 'UTC' - } as const; - + } as const +) => { return new Date(date).toLocaleDateString('en', dateOptions); };