-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #322 and added simple markdown publishing system.
- Loading branch information
Showing
15 changed files
with
387 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { parseISO, format } from 'date-fns' | ||
|
||
type Props = { | ||
dateString: string | ||
} | ||
|
||
const DateFormatter = ({ dateString }: Props) => { | ||
const date = parseISO(dateString) | ||
return <time dateTime={dateString}>{format(date, 'LLLL d, yyyy')}</time> | ||
} | ||
|
||
export default DateFormatter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.markdown { | ||
@apply text-lg leading-relaxed; | ||
} | ||
|
||
.markdown p, | ||
.markdown ul, | ||
.markdown ol, | ||
.markdown blockquote { | ||
@apply my-6; | ||
} | ||
|
||
.markdown h2 { | ||
@apply text-3xl mt-12 mb-4 leading-snug; | ||
} | ||
|
||
.markdown h3 { | ||
@apply text-2xl mt-8 mb-4 leading-snug; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import markdownStyles from './markdown-styles.module.css' | ||
|
||
type Props = { | ||
content: string | ||
} | ||
|
||
const PostBody = ({ content }: Props) => { | ||
return ( | ||
<div className=""> | ||
<div | ||
className={markdownStyles['markdown']} | ||
dangerouslySetInnerHTML={{ __html: content }} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default PostBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { remark } from 'remark' | ||
import html from 'remark-html' | ||
|
||
export default async function markdownToHtml(markdown: string) { | ||
const result = await remark().use(html).process(markdown) | ||
return result.toString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from 'fs' | ||
import { join } from 'path' | ||
import matter from 'gray-matter' | ||
|
||
const postsDirectory = join(process.cwd(), 'ref') | ||
|
||
export function getPostSlugs() { | ||
return fs.readdirSync(postsDirectory) | ||
} | ||
|
||
export function getPostBySlug(slug: string, fields: string[] = []) { | ||
const realSlug = slug.replace(/\.md$/, '') | ||
const fullPath = join(postsDirectory, `${realSlug}.md`) | ||
const fileContents = fs.readFileSync(fullPath, 'utf8') | ||
const { data, content } = matter(fileContents) | ||
|
||
type Items = { | ||
[key: string]: string | ||
} | ||
|
||
const items: Items = {} | ||
|
||
// Ensure only the minimal needed data is exposed | ||
fields.forEach((field) => { | ||
if (field === 'slug') { | ||
items[field] = realSlug | ||
} | ||
if (field === 'content') { | ||
items[field] = content | ||
} | ||
|
||
if (typeof data[field] !== 'undefined') { | ||
items[field] = data[field] | ||
} | ||
}) | ||
|
||
return items | ||
} | ||
|
||
export function getAllPosts(fields: string[] = []) { | ||
const slugs = getPostSlugs() | ||
const posts = slugs | ||
.map((slug) => getPostBySlug(slug, fields)) | ||
// sort posts by date in descending order | ||
.sort((post1, post2) => (post1.date > post2.date ? -1 : 1)) | ||
return posts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { GetStaticPaths } from 'next'; | ||
import ErrorPage from 'next/error'; | ||
import Head from 'next/head'; | ||
import { useRouter } from 'next/router'; | ||
import DateFormatter from '../../components/ref/dateformatter'; | ||
import PostBody from '../../components/ref/postBody'; | ||
import markdownToHtml from '../../libs/pages/mdtoHtml'; | ||
import { getAllPosts, getPostBySlug } from '../../libs/pages/refposts'; | ||
import PostType from '../../types/post'; | ||
|
||
type Props = { | ||
post: PostType; | ||
morePosts: PostType[]; | ||
preview?: boolean; | ||
}; | ||
|
||
const Post = ({ post, morePosts, preview }: Props) => { | ||
const router = useRouter(); | ||
if (!router.isFallback && !post?.slug) { | ||
return <ErrorPage statusCode={404} />; | ||
} | ||
return ( | ||
<> | ||
{router.isFallback ? ( | ||
<h4>Loading…</h4> | ||
) : ( | ||
<> | ||
<article className="m-4"> | ||
<Head> | ||
<title>{post.title}</title> | ||
</Head> | ||
<h1 className="my-2">{post.title}</h1> | ||
<strong> | ||
<DateFormatter dateString={post.date} /> - {post.author.name} | ||
</strong> | ||
<hr /> | ||
<PostBody content={post.content} /> | ||
</article> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default Post; | ||
|
||
type Params = { | ||
params: { | ||
slug: string; | ||
}; | ||
}; | ||
|
||
export async function getStaticProps({ params }: Params) { | ||
const post = getPostBySlug(params.slug, ['title', 'date', 'slug', 'author', 'content']); | ||
const content = await markdownToHtml(post.content || ''); | ||
|
||
return { | ||
props: { | ||
post: { | ||
...post, | ||
content, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export const getStaticPaths: GetStaticPaths = async () => { | ||
const posts = getAllPosts(['slug']); | ||
|
||
return { | ||
paths: posts.map((post) => { | ||
return { | ||
params: { | ||
slug: post.slug, | ||
}, | ||
}; | ||
}), | ||
fallback: false, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Head from 'next/head'; | ||
import Link from 'next/link'; | ||
import { Container } from 'react-bootstrap'; | ||
import { getAllPosts } from '../libs/pages/refposts'; | ||
import Post from '../types/post'; | ||
|
||
type Props = { | ||
allPosts: Post[]; | ||
}; | ||
|
||
const Index = ({ allPosts }: Props) => { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Gallformers Reference Library</title> | ||
</Head> | ||
<Container className="mx-0 mt-4"> | ||
<h1 className="my-4">The Gallformers Reference Library</h1> | ||
{allPosts | ||
.sort((a, b) => a.title.localeCompare(b.title)) | ||
.map((p) => ( | ||
<div key={p.slug} className="my-2"> | ||
<Link href={`/ref/${p.slug}`}> | ||
<a>{p.title}</a> | ||
</Link> | ||
</div> | ||
))} | ||
</Container> | ||
</> | ||
); | ||
}; | ||
|
||
export default Index; | ||
|
||
export const getStaticProps = async () => { | ||
const allPosts = getAllPosts(['title', 'date', 'slug', 'author']); | ||
|
||
return { | ||
props: { allPosts }, | ||
}; | ||
}; |
Oops, something went wrong.