import fs from 'fs'; import matter from 'gray-matter'; import yaml from 'js-yaml'; import { Box, Grid, Stack, Heading, Text } from '@chakra-ui/react'; import ChakraUIRenderer from 'chakra-ui-markdown-renderer'; import ReactMarkdown from 'react-markdown'; import { useRouter } from 'next/router'; import { useEffect } from 'react'; import gfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; import { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps, NextPage } from 'next'; import MDComponents from '../components/UI/docs'; import { Breadcrumbs, DocsNav, DocumentNav } from '../components/UI/docs'; import { PageMetadata } from '../components/UI'; import { NavLink } from '../types'; import { getFileList } from '../utils/getFileList'; import { textStyles } from '../theme/foundations'; import { getParsedDate } from '../utils'; const MATTER_OPTIONS = { engines: { yaml: (s: any) => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object } }; // This method crawls for all valid docs paths export const getStaticPaths: GetStaticPaths = () => { const paths: string[] = getFileList('docs'); // This is folder that get crawled for valid docs paths. Change if this path changes. return { paths, fallback: false }; }; // Reads file data for markdown pages export const getStaticProps: GetStaticProps = async context => { const { slug } = context.params as ParsedUrlQuery; const filePath = (slug as string[])!.join('/'); let file; let lastModified; const navLinks = yaml.load(fs.readFileSync('src/data/documentation-links.yaml', 'utf8')); 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); return { props: { frontmatter, content, navLinks, lastModified: getParsedDate(lastModified.mtime, { month: 'long', day: 'numeric', year: 'numeric' }) } }; }; interface Props { frontmatter: { [key: string]: string; }; content: string; navLinks: NavLink[]; lastModified: string; } const DocPage: NextPage = ({ frontmatter, content, navLinks, lastModified }) => { const router = useRouter(); useEffect(() => { const id = router.asPath.split('#')[1]; const element = document.getElementById(id); if (!element) { return; } element.scrollIntoView({ behavior: 'smooth', block: 'start' }); }, [router.asPath]); return ( <>
{frontmatter.title} Last edited on {lastModified} {content}
); }; export default DocPage;