on master help sign in
docs: working docs slug pages
commit26796bf12f50dfa1d2faa6693f5c8d8dbcf096f5 authorMatilde Park <matilde@tlon.io> dateFri 18 Jun 2021, 15:29 (5 years ago) parent4f10b05
3 files changed+30−21
lib/lib.js+18−4 pages/docs.js+2−4 pages/docs/[...slug].js+10−13
lib/lib.js+18−4
@@ -210,8 +210,22 @@ export function buildPageTree(path) {
210210 };
211211}
212212
213−export const getIndexPage = (path) => {
214− const fileContents = fs.readFileSync(path, "utf8");
215− const { data, content } = matter(fileContents, options);
216− return { data, content };
213+export const getPage = (path) => {
214+ try {
215+ let fileContents = fs.readFileSync(`${path}.md`, "utf8");
216+ if (fileContents) {
217+ const { data, content } = matter(fileContents, options);
218+ return { data, content };
219+ }
220+ } catch {
221+ try {
222+ let fileContents = fs.readFileSync(`${path}/_index.md`, "utf8");
223+ if (fileContents) {
224+ const { data, content } = matter(fileContents, options);
225+ return { data, content };
226+ }
227+ } catch {
228+ console.error("no md file for slug");
229+ }
230+ }
217231};
pages/docs.js+2−4
@@ -1,7 +1,7 @@
11import Link from "next/link";
22import { useRouter } from "next/router";
33import { join } from "path";
4−import { getDocs, formatDate, buildPageTree, getIndexPage } from "../lib/lib";
4+import { getDocs, formatDate, buildPageTree, getPage } from "../lib/lib";
55import Markdown from "../components/Markdown";
66
77const breadcrumbs = (posts, paths) => {
@@ -93,8 +93,6 @@ export default function DocsLayout({ posts, data, content }) {
9393
9494export async function getStaticProps() {
9595 const posts = buildPageTree(join(process.cwd(), "content/docs"));
96− const { data, content } = getIndexPage(
97− join(process.cwd(), "content/docs", "_index.md")
98− );
96+ const { data, content } = getPage(join(process.cwd(), "content/docs"));
9997 return { props: { posts, data, content } };
10098}
pages/docs/[...slug].js+10−13
@@ -1,12 +1,7 @@
11import Link from "next/link";
22import { useRouter } from "next/router";
33import { join } from "path";
4−import {
5− getDocs,
6− formatDate,
7− buildPageTree,
8− getIndexPage,
9−} from "../../lib/lib";
4+import { getDocs, formatDate, buildPageTree, getPage } from "../../lib/lib";
105import Markdown from "../../components/Markdown";
116
127const breadcrumbs = (posts, paths) => {
@@ -85,23 +80,26 @@ function ContentArea(props) {
8580 );
8681}
8782
88−export default function DocsLayout({ posts, data, content }) {
83+export default function DocsLayout({ posts, data, content, params }) {
8984 return (
9085 <div className="flex w-screen h-screen min-h-screen w-screen overflow-hidden">
9186 <Sidebar>{childPages("/docs", posts.children)}</Sidebar>
92− <ContentArea breadcrumbs={breadcrumbs(posts, [])} title={data.title}>
87+ <ContentArea
88+ breadcrumbs={breadcrumbs(posts, params.slug.slice(0, -1))}
89+ title={data.title}
90+ >
9391 <Markdown post={{ content: content }} />
9492 </ContentArea>
9593 </div>
9694 );
9795}
9896
99−export async function getStaticProps() {
97+export async function getStaticProps({ params }) {
10098 const posts = buildPageTree(join(process.cwd(), "content/docs"));
101− const { data, content } = getIndexPage(
102− join(process.cwd(), "content/docs", "_index.md")
99+ const { data, content } = getPage(
100+ join(process.cwd(), "content/docs", params.slug.join("/"))
103101 );
104− return { props: { posts, data, content } };
102+ return { props: { posts, data, content, params } };
105103}
106104
107105export async function getStaticPaths() {
@@ -118,7 +116,6 @@ export async function getStaticPaths() {
118116 });
119117 };
120118 allHrefs("/docs", posts);
121− console.log(slugs);
122119 return {
123120 paths: slugs.filter((e) => e !== "/docs"),
124121 fallback: false,