on master help sign in
lib: make buildPageTree more generic
commit004f287236dc1133906db91a05852fb7bafdafe1 authorMatilde Park <matilde@tlon.io> dateTue 22 Jun 2021, 16:51 (5 years ago) parent7c80508
3 files changed+10−6
lib/lib.js+7−3 pages/docs.js+1−1 pages/docs/[...slug].js+2−2
lib/lib.js+7−3
@@ -142,7 +142,7 @@ export function formatDate(d) {
142142// children: { [string]: PageTree },
143143// }
144144
145−export function buildPageTree(path) {
145+export function buildPageTree(path, type = "", content = false) {
146146 const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
147147
148148 const children = fs.readdirSync(path, { withFileTypes: true });
@@ -152,7 +152,7 @@ export function buildPageTree(path) {
152152 return {
153153 title: metadata.data.title,
154154 pages: pages.map((page) => {
155− const { data: pageData } = matter(
155+ const { data: pageData, content: pageContent } = matter(
156156 fs.readFileSync(join(path, page.name)),
157157 options
158158 );
@@ -160,7 +160,11 @@ export function buildPageTree(path) {
160160 title: pageData.title,
161161 base: page.name,
162162 slug: page.name.replace(/.md$/, ""),
163− weight: pageData?.weight ?? 0,
163+ ...(type === "weight" && { weight: pageData?.weight ?? 0 }),
164+ ...(type === "date" && {
165+ date: pageData?.date ?? "2000-01-01T00:00:00.000Z",
166+ }),
167+ ...(content && { content: pageContent }),
164168 };
165169 }),
166170 children: Object.fromEntries(
pages/docs.js+1−1
@@ -114,7 +114,7 @@ export default function DocsLayout({ posts, data, content }) {
114114}
115115
116116export async function getStaticProps() {
117− const posts = buildPageTree(join(process.cwd(), "content/docs"));
117+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
118118 const { data, content } = getPage(join(process.cwd(), "content/docs"));
119119 return { props: { posts, data, content } };
120120}
pages/docs/[...slug].js+2−2
@@ -131,7 +131,7 @@ export default function DocsLayout({ posts, data, content, params }) {
131131}
132132
133133export async function getStaticProps({ params }) {
134− const posts = buildPageTree(join(process.cwd(), "content/docs"));
134+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
135135 const { data, content } = getPage(
136136 join(process.cwd(), "content/docs", params.slug.join("/"))
137137 );
@@ -139,7 +139,7 @@ export async function getStaticProps({ params }) {
139139}
140140
141141export async function getStaticPaths() {
142− const posts = buildPageTree(join(process.cwd(), "content/docs"));
142+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
143143 const slugs = [];
144144 const allHrefs = (thisLink, tree) => {
145145 slugs.push(thisLink, ...tree.pages.map((e) => join(thisLink, e.slug)));