on master help sign in
lib: make buildPageTree more generic
004f287Matilde Park
lib/lib.js+7−3
@@ -142,7 +142,7 @@ export function formatDate(d) {
142// children: { [string]: PageTree },
142// children: { [string]: PageTree },
143// }
143// }
144
144
145−export function buildPageTree(path) {
145+export function buildPageTree(path, type = "", content = false) {
146 const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
146 const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
147
147
148 const children = fs.readdirSync(path, { withFileTypes: true });
148 const children = fs.readdirSync(path, { withFileTypes: true });
@@ -152,7 +152,7 @@ export function buildPageTree(path) {
152 return {
152 return {
153 title: metadata.data.title,
153 title: metadata.data.title,
154 pages: pages.map((page) => {
154 pages: pages.map((page) => {
155− const { data: pageData } = matter(
155+ const { data: pageData, content: pageContent } = matter(
156 fs.readFileSync(join(path, page.name)),
156 fs.readFileSync(join(path, page.name)),
157 options
157 options
158 );
158 );
@@ -160,7 +160,11 @@ export function buildPageTree(path) {
160 title: pageData.title,
160 title: pageData.title,
161 base: page.name,
161 base: page.name,
162 slug: page.name.replace(/.md$/, ""),
162 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 }),
164 };
168 };
165 }),
169 }),
166 children: Object.fromEntries(
170 children: Object.fromEntries(
pages/docs.js+1−1
@@ -114,7 +114,7 @@ export default function DocsLayout({ posts, data, content }) {
114}
114}
115
115
116export async function getStaticProps() {
116export async function getStaticProps() {
117− const posts = buildPageTree(join(process.cwd(), "content/docs"));
117+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
118 const { data, content } = getPage(join(process.cwd(), "content/docs"));
118 const { data, content } = getPage(join(process.cwd(), "content/docs"));
119 return { props: { posts, data, content } };
119 return { props: { posts, data, content } };
120}
120}
pages/docs/[...slug].js+2−2
@@ -131,7 +131,7 @@ export default function DocsLayout({ posts, data, content, params }) {
131}
131}
132
132
133export async function getStaticProps({ params }) {
133export async function getStaticProps({ params }) {
134− const posts = buildPageTree(join(process.cwd(), "content/docs"));
134+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
135 const { data, content } = getPage(
135 const { data, content } = getPage(
136 join(process.cwd(), "content/docs", params.slug.join("/"))
136 join(process.cwd(), "content/docs", params.slug.join("/"))
137 );
137 );
@@ -139,7 +139,7 @@ export async function getStaticProps({ params }) {
139}
139}
140
140
141export async function getStaticPaths() {
141export async function getStaticPaths() {
142− const posts = buildPageTree(join(process.cwd(), "content/docs"));
142+ const posts = buildPageTree(join(process.cwd(), "content/docs"), "weight");
143 const slugs = [];
143 const slugs = [];
144 const allHrefs = (thisLink, tree) => {
144 const allHrefs = (thisLink, tree) => {
145 slugs.push(thisLink, ...tree.pages.map((e) => join(thisLink, e.slug)));
145 slugs.push(thisLink, ...tree.pages.map((e) => join(thisLink, e.slug)));