adding completed project
This commit is contained in:
82
pages/blog/[slug].js
Normal file
82
pages/blog/[slug].js
Normal file
@@ -0,0 +1,82 @@
|
||||
import Image from "next/image"
|
||||
import matter from 'gray-matter'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import styles from "../../styles/Blog.module.css"
|
||||
|
||||
const glob = require('glob')
|
||||
|
||||
import Layout from '../../components/Layout'
|
||||
|
||||
export default function BlogTemplate({ frontmatter, markdownBody, siteTitle }) {
|
||||
function reformatDate(fullDate) {
|
||||
const date = new Date(fullDate)
|
||||
return date.toDateString().slice(4)
|
||||
}
|
||||
|
||||
/*
|
||||
** Odd fix to get build to run
|
||||
** It seems like on first go the props
|
||||
** are undefined — could be a Next bug?
|
||||
*/
|
||||
|
||||
if (!frontmatter) return <></>
|
||||
|
||||
return (
|
||||
<Layout siteTitle={siteTitle}>
|
||||
<article className={styles.blog}>
|
||||
<figure className={styles.blog__hero}>
|
||||
<Image
|
||||
width="1920"
|
||||
height="1080"
|
||||
src={frontmatter.hero_image}
|
||||
alt={`blog_hero_${frontmatter.title}`}
|
||||
/>
|
||||
</figure>
|
||||
<div className={styles.blog__info}>
|
||||
<h1>{frontmatter.title}</h1>
|
||||
<h3>{reformatDate(frontmatter.date)}</h3>
|
||||
</div>
|
||||
<div className={styles.blog__body}>
|
||||
<ReactMarkdown children={markdownBody} />
|
||||
</div>
|
||||
<h2 className={styles.blog__footer}>Written By: {frontmatter.author}</h2>
|
||||
</article>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
export async function getStaticProps({ ...ctx }) {
|
||||
const { slug } = ctx.params
|
||||
const content = await import(`../../posts/${slug}.md`)
|
||||
const config = await import(`../../data/config.json`)
|
||||
const data = matter(content.default)
|
||||
|
||||
return {
|
||||
props: {
|
||||
siteTitle: config.title,
|
||||
frontmatter: data.data,
|
||||
markdownBody: data.content,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function getStaticPaths() {
|
||||
//get all .md files in the posts dir
|
||||
const blogs = glob.sync('posts/**/*.md')
|
||||
|
||||
//remove path and extension to leave filename only
|
||||
const blogSlugs = blogs.map(file =>
|
||||
file
|
||||
.split('/')[1]
|
||||
.replace(/ /g, '-')
|
||||
.slice(0, -3)
|
||||
.trim()
|
||||
)
|
||||
|
||||
// create paths with `slug` param
|
||||
const paths = blogSlugs.map(slug => `/blog/${slug}`)
|
||||
return {
|
||||
paths,
|
||||
fallback: false,
|
||||
}
|
||||
}
|
||||
115
pages/index.js
115
pages/index.js
@@ -1,69 +1,54 @@
|
||||
import Head from 'next/head'
|
||||
import Image from 'next/image'
|
||||
import styles from '../styles/Home.module.css'
|
||||
import matter from 'gray-matter'
|
||||
import Layout from '../components/Layout'
|
||||
import BlogList from '../components/BlogList'
|
||||
|
||||
export default function Home() {
|
||||
const Index = props => {
|
||||
return (
|
||||
<div className={styles.container}>
|
||||
<Head>
|
||||
<title>Create Next App</title>
|
||||
<meta name="description" content="Generated by create next app" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
|
||||
<main className={styles.main}>
|
||||
<h1 className={styles.title}>
|
||||
Welcome to <a href="https://nextjs.org">Next.js!</a>
|
||||
</h1>
|
||||
|
||||
<p className={styles.description}>
|
||||
Get started by editing{' '}
|
||||
<code className={styles.code}>pages/index.js</code>
|
||||
</p>
|
||||
|
||||
<div className={styles.grid}>
|
||||
<a href="https://nextjs.org/docs" className={styles.card}>
|
||||
<h2>Documentation →</h2>
|
||||
<p>Find in-depth information about Next.js features and API.</p>
|
||||
</a>
|
||||
|
||||
<a href="https://nextjs.org/learn" className={styles.card}>
|
||||
<h2>Learn →</h2>
|
||||
<p>Learn about Next.js in an interactive course with quizzes!</p>
|
||||
</a>
|
||||
|
||||
<a
|
||||
href="https://github.com/vercel/next.js/tree/canary/examples"
|
||||
className={styles.card}
|
||||
>
|
||||
<h2>Examples →</h2>
|
||||
<p>Discover and deploy boilerplate example Next.js projects.</p>
|
||||
</a>
|
||||
|
||||
<a
|
||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
|
||||
className={styles.card}
|
||||
>
|
||||
<h2>Deploy →</h2>
|
||||
<p>
|
||||
Instantly deploy your Next.js site to a public URL with Vercel.
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer className={styles.footer}>
|
||||
<a
|
||||
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Powered by{' '}
|
||||
<span className={styles.logo}>
|
||||
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
|
||||
</span>
|
||||
</a>
|
||||
</footer>
|
||||
</div>
|
||||
<Layout
|
||||
pathname="/"
|
||||
siteTitle={props.title}
|
||||
siteDescription={props.description}
|
||||
>
|
||||
<section>
|
||||
<BlogList allBlogs={props.allBlogs} />
|
||||
</section>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
export default Index
|
||||
|
||||
export async function getStaticProps() {
|
||||
const siteConfig = await import(`../data/config.json`)
|
||||
//get posts & context from folder
|
||||
const posts = (context => {
|
||||
const keys = context.keys()
|
||||
const values = keys.map(context)
|
||||
|
||||
const data = keys.map((key, index) => {
|
||||
// Create slug from filename
|
||||
const slug = key
|
||||
.replace(/^.*[\\\/]/, '')
|
||||
.split('.')
|
||||
.slice(0, -1)
|
||||
.join('.')
|
||||
const value = values[index]
|
||||
// Parse yaml metadata & markdownbody in document
|
||||
const document = matter(value.default)
|
||||
return {
|
||||
frontmatter: document.data,
|
||||
markdownBody: document.content,
|
||||
slug,
|
||||
}
|
||||
})
|
||||
return data
|
||||
})(require.context('../posts', true, /\.md$/))
|
||||
|
||||
return {
|
||||
props: {
|
||||
allBlogs: posts,
|
||||
title: siteConfig.default.title,
|
||||
description: siteConfig.default.description,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
33
pages/info.js
Normal file
33
pages/info.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import Layout from '../components/Layout'
|
||||
import matter from 'gray-matter'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import styles from "../styles/Info.module.css"
|
||||
|
||||
export default function Info({ frontmatter, markdownBody, title }) {
|
||||
console.log(markdownBody)
|
||||
return (
|
||||
<Layout
|
||||
pathname="info"
|
||||
bgColor={frontmatter.background_color}
|
||||
siteTitle={title}
|
||||
>
|
||||
<section className={styles.info_blurb}>
|
||||
<ReactMarkdown children={markdownBody} />
|
||||
</section>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
const content = await import(`../data/info.md`)
|
||||
const config = await import(`../data/config.json`)
|
||||
const data = matter(content.default)
|
||||
|
||||
return {
|
||||
props: {
|
||||
title: config.title,
|
||||
frontmatter: data.data,
|
||||
markdownBody: data.content,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user