on master help sign in
lib: sort sidebars by weight in pagetree
commit33b159e84e99326760afe761affa80a7a6cb354b authorMatilde Park <matilde@tlon.io> dateFri 9 Jul 2021, 15:03 (5 years ago) parente9207b3
1 file changed+16−4
lib/lib.js+16−4
lib/lib.js+16−4
@@ -190,10 +190,22 @@ export function buildPageTree(path, ordering = "", content = false) {
190190 })
191191 .sort((a, b) => a.weight - b.weight),
192192 children: Object.fromEntries(
193− subdirs.map((subdir) => [
194− subdir.name,
195− buildPageTree(join(path, subdir.name), ordering, content),
196− ])
193+ subdirs
194+ .sort((a, b) => {
195+ const aMetadata = matter(
196+ fs.readFileSync(join(path, a.name, "_index.md")),
197+ options
198+ );
199+ const bMetadata = matter(
200+ fs.readFileSync(join(path, b.name, "_index.md")),
201+ options
202+ );
203+ return aMetadata?.data?.weight - bMetadata?.data?.weight || 0;
204+ })
205+ .map((subdir) => [
206+ subdir.name,
207+ buildPageTree(join(path, subdir.name), ordering, content),
208+ ])
197209 ),
198210 };
199211}