Skip to content

Commit

Permalink
Merge pull request #19 from shunkakinoki/shunkakinoki-patch-1
Browse files Browse the repository at this point in the history
feat: ini slug pages
  • Loading branch information
shunkakinoki authored Feb 16, 2021
2 parents df5a007 + 66114c1 commit 2b1d715
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 68 deletions.
25 changes: 18 additions & 7 deletions src/components/Blog/Blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,33 @@ export interface Props {
[key: string]: any;
};
source: MdxRemote.Source;
slug: string;
}

interface BlogLinkProps {
children: string;
href: string;
}

function BlogLink({children, href}: BlogLinkProps) {
return <Link href={href.replace(".md", "")}>{children}</Link>;
}
export default function Blog({source, slug}: Props): JSX.Element {
function BlogLink({children, href}: BlogLinkProps) {
return (
<Link
href={
slug === "blog"
? href.replace(".md", "")
: `${slug}/${href.replace(".md", "")}`
}
>
{children}
</Link>
);
}

const components = {
a: BlogLink,
};
const components = {
a: BlogLink,
};

export default function Blog({source}: Props): JSX.Element {
const content = hydrate(source, {components});

return (
Expand Down
4 changes: 4 additions & 0 deletions src/components/Content/Content.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@apply leading-relaxed;
}

.markdown a {
@apply underline;
}

.markdown p {
@apply my-3;
}
Expand Down
101 changes: 93 additions & 8 deletions src/pages/[pageId].tsx → src/pages/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import {ExtendedRecordMap} from "notion-types";
import validator from "validator";

import {NotionLinks} from "@/const";
import {getGithubContent} from "@/lib/github";
import {getGithubContent, getGithubSummary} from "@/lib/github";
import {resolveNotionPage} from "@/lib/notion";
import BlogScreen from "@/screens/BlogScreen";
import ContentScreen from "@/screens/ContentScreen";
import NotionScreen from "@/screens/NotionScreen";

export interface Props {
content: string;
frontMatter?: string;
type: "blog" | "collection" | "page";
slug?: string;
type: "blog" | "content" | "collection" | "page";
}

const blogCollections = ["blog", "pioneer"];

const coreCollections = ["cause", "mission", "values"];

const notionCollections = [
Expand Down Expand Up @@ -50,7 +54,36 @@ export const getStaticProps: GetStaticProps<Props> = async ({
locale,
}: // eslint-disable-next-line @typescript-eslint/require-await
GetStaticPropsContext) => {
const pageId = params?.pageId as string;
const slugs = params?.slug as string[];

if (slugs?.length !== 1) {
try {
const result = await getGithubContent(
slugs[0],
slugs?.splice(0, 1).join("/"),
locale,
);
if (result) {
const {frontMatter, source} = result;
return {
props: {
content: JSON.stringify(source),
frontMatter: JSON.stringify(frontMatter),
type: "content",
},
revalidate: 30,
};
}
} catch (err) {
return {
notFound: true,
revalidate: 30,
};
}
}

const pageId = slugs[0];

let notionCollection;

if (notionCollections.includes(pageId)) {
Expand Down Expand Up @@ -101,10 +134,31 @@ GetStaticPropsContext) => {
}

if (!validator.isUUID(pageId) && !notionCollection) {
try {
if (blogCollections.includes(pageId)) {
const result = await getGithubSummary(pageId, locale);
if (result) {
const {frontMatter, source} = result;
return {
props: {
content: JSON.stringify(source),
frontMatter: JSON.stringify(frontMatter),
slug: JSON.stringify(pageId),
type: "blog",
},
revalidate: 30,
};
} else {
return {
notFound: true,
revalidate: 30,
};
}
}

if (coreCollections.includes(pageId)) {
const result = await getGithubContent(
coreCollections.includes(pageId) ? pageId : "blog",
coreCollections.includes(pageId) ? pageId.toUpperCase() : pageId,
pageId,
pageId.toUpperCase(),
locale,
);
if (result) {
Expand All @@ -113,7 +167,27 @@ GetStaticPropsContext) => {
props: {
content: JSON.stringify(source),
frontMatter: JSON.stringify(frontMatter),
type: "blog",
type: "content",
},
revalidate: 30,
};
} else {
return {
notFound: true,
revalidate: 30,
};
}
}

try {
const result = await getGithubContent("blog", pageId, locale);
if (result) {
const {frontMatter, source} = result;
return {
props: {
content: JSON.stringify(source),
frontMatter: JSON.stringify(frontMatter),
type: "content",
},
revalidate: 30,
};
Expand Down Expand Up @@ -148,9 +222,20 @@ GetStaticPropsContext) => {
const PageId = ({
content,
frontMatter,
slug,
type,
}: InferGetStaticPropsType<typeof getStaticProps>): JSX.Element => {
if (content && frontMatter && type === "blog") {
if (content && frontMatter && slug && type === "blog") {
return (
<BlogScreen
frontMatter={JSON.parse(frontMatter) as {[key: string]: any}}
source={JSON.parse(content) as MdxRemote.Source}
slug={JSON.parse(slug) as string}
/>
);
}

if (content && frontMatter && type === "content") {
return (
<ContentScreen
frontMatter={JSON.parse(frontMatter) as {[key: string]: any}}
Expand Down
51 changes: 0 additions & 51 deletions src/pages/blog.tsx

This file was deleted.

8 changes: 6 additions & 2 deletions src/screens/BlogScreen/BlogScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import Header from "@/components/Header";

export type Props = BlogProps;

export default function BlogScreen({frontMatter, source}: Props): JSX.Element {
export default function BlogScreen({
frontMatter,
source,
slug,
}: Props): JSX.Element {
return (
<>
<Header />
<div className="flex flex-col items-start justify-center max-w-2xl mx-auto">
<Blog frontMatter={frontMatter} source={source} />
<Blog frontMatter={frontMatter} source={source} slug={slug} />
</div>
<Footer />
</>
Expand Down

1 comment on commit 2b1d715

@vercel
Copy link

@vercel vercel bot commented on 2b1d715 Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.