add lastModified date (#68)

* add lastModified date

* prettier

* fix type errors on build

Co-authored-by: Paul Wackerow <54227730+wackerow@users.noreply.github.com>
This commit is contained in:
Corwin Smith 2022-11-30 15:02:08 +01:00 committed by GitHub
parent b04fd9206f
commit d4c57f9174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

@ -1,7 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import matter from 'gray-matter'; import matter from 'gray-matter';
import yaml from 'js-yaml'; 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 ChakraUIRenderer from 'chakra-ui-markdown-renderer';
import ReactMarkdown from 'react-markdown'; import ReactMarkdown from 'react-markdown';
import gfm from 'remark-gfm'; import gfm from 'remark-gfm';
@ -12,6 +12,7 @@ import MDXComponents from '../components/';
import { Breadcrumbs } from '../components/docs'; import { Breadcrumbs } from '../components/docs';
import { PageMetadata } from '../components/UI'; import { PageMetadata } from '../components/UI';
import { textStyles } from '../theme/foundations'; import { textStyles } from '../theme/foundations';
import { getParsedDate } from '../utils';
const MATTER_OPTIONS = { const MATTER_OPTIONS = {
engines: { engines: {
@ -49,11 +50,14 @@ export const getStaticProps: GetStaticProps = async context => {
const { slug } = context.params as ParsedUrlQuery; const { slug } = context.params as ParsedUrlQuery;
const filePath = (slug as string[])!.join('/'); const filePath = (slug as string[])!.join('/');
let file; let file;
let lastModified;
try { try {
file = fs.readFileSync(`${filePath}.md`, 'utf-8'); file = fs.readFileSync(`${filePath}.md`, 'utf-8');
lastModified = fs.statSync(`${filePath}.md`);
} catch { } catch {
file = fs.readFileSync(`${filePath}/index.md`, 'utf-8'); file = fs.readFileSync(`${filePath}/index.md`, 'utf-8');
lastModified = fs.statSync(`${filePath}/index.md`);
} }
const { data: frontmatter, content } = matter(file, MATTER_OPTIONS); const { data: frontmatter, content } = matter(file, MATTER_OPTIONS);
@ -61,7 +65,12 @@ export const getStaticProps: GetStaticProps = async context => {
return { return {
props: { props: {
frontmatter, frontmatter,
content content,
lastModified: getParsedDate(lastModified.mtime, {
month: 'numeric',
day: 'numeric',
year: 'numeric'
})
} }
}; };
}; };
@ -71,9 +80,10 @@ interface Props {
[key: string]: string; [key: string]: string;
}; };
content: string; content: string;
lastModified: string;
} }
const DocPage: NextPage<Props> = ({ frontmatter, content }) => { const DocPage: NextPage<Props> = ({ frontmatter, content, lastModified }) => {
return ( return (
<> <>
<PageMetadata title={frontmatter.title} description={frontmatter.description} /> <PageMetadata title={frontmatter.title} description={frontmatter.description} />
@ -84,7 +94,9 @@ const DocPage: NextPage<Props> = ({ frontmatter, content }) => {
<Heading as='h1' mt='4 !important' mb={0} {...textStyles.header1}> <Heading as='h1' mt='4 !important' mb={0} {...textStyles.header1}>
{frontmatter.title} {frontmatter.title}
</Heading> </Heading>
{/* <Text as='span' mt='0 !important'>last edited {TODO: get last edited date}</Text> */} <Text as='span' mt='0 !important'>
last edited {lastModified}
</Text>
</Stack> </Stack>
<ReactMarkdown remarkPlugins={[gfm]} components={ChakraUIRenderer(MDXComponents)}> <ReactMarkdown remarkPlugins={[gfm]} components={ChakraUIRenderer(MDXComponents)}>
{content} {content}

@ -1,12 +1,13 @@
export const getParsedDate = (date: string) => { export const getParsedDate = (
const dateOptions = { date: string | Date,
dateOptions: Intl.DateTimeFormatOptions = {
year: 'numeric', year: 'numeric',
month: 'long', month: 'long',
day: 'numeric', day: 'numeric',
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
timeZone: 'UTC' timeZone: 'UTC'
} as const; } as const
) => {
return new Date(date).toLocaleDateString('en', dateOptions); return new Date(date).toLocaleDateString('en', dateOptions);
}; };