From 45e38a3900ff1a00215491442db3000d4b56220f Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 10 Mar 2024 09:04:20 -0500 Subject: [PATCH] Improve sidebar generation docs, formatting --- src/content/sidebar.ts | 47 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/content/sidebar.ts b/src/content/sidebar.ts index 8379aca..a46b13b 100644 --- a/src/content/sidebar.ts +++ b/src/content/sidebar.ts @@ -1,5 +1,4 @@ -import { getCollection, getEntries, type CollectionEntry } from 'astro:content'; - +import { getCollection, getEntries, type CollectionEntry } from "astro:content"; export type Link = { text: string; @@ -7,21 +6,24 @@ export type Link = { header?: boolean; }; +// The order of the headers (lower is closer to the top) const headerOrder: Record = { root: 0, learning: 1, living: 2, improving: 3, - default: 999 -} + default: 9999, // If the header is not defined in here, it will be placed at the bottom +}; +/** + * Applies title case (first letter uppercase, the rest lowercase) to all words (a letter followed by any number of non-whitespace characters) in the input string + * @param s The input string + * @returns Every word title-cased + */ function toTitleCase(s: string): string { - return s.replace( - /\w\S*/g, - function(txt) { - return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); - } - ); + return s.replace(/\w\S*/g, function (txt) { + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); + }); } /** @@ -29,17 +31,17 @@ function toTitleCase(s: string): string { * @returns {Link[]} - An array of links to be used in the sidebar */ const get = async (): Promise => { - const entries = await getCollection('handbook'); - const entriesByHeader: Record[]> = {}; + const entries = await getCollection("handbook"); + const entriesByHeader: Record[]> = {}; + // Group the entries by their header entries.forEach((entry) => { // Acquire the header using the first part of the slug (otherwise, use 'root' as the header) - const header = entry.slug.indexOf('/') !== -1 ? entry.slug.split('/')[0] : 'root'; - - if (entriesByHeader[header]) - entriesByHeader[header].push(entry); - else - entriesByHeader[header] = [entry]; + const header = + entry.slug.indexOf("/") !== -1 ? entry.slug.split("/")[0] : "root"; + + if (entriesByHeader[header]) entriesByHeader[header].push(entry); + else entriesByHeader[header] = [entry]; }); // Begin building the links array (root level first, then the rest of the entries) @@ -48,12 +50,18 @@ const get = async (): Promise => { link: `/handbook/${entry.slug}`, })); + // Remove the root level delete entriesByHeader.root; + // Sort the headers by the order defined in headerOrder const sortedHeaders = Object.keys(entriesByHeader).sort((a, b) => { - return (headerOrder[a] || headerOrder.default) - (headerOrder[b] || headerOrder.default); + return ( + (headerOrder[a] || headerOrder.default) - + (headerOrder[b] || headerOrder.default) + ); }); + // Add the rest of the entries to the links array sortedHeaders.forEach((header) => { links.push({ text: toTitleCase(header), header: true }); entriesByHeader[header].forEach((entry) => { @@ -64,7 +72,6 @@ const get = async (): Promise => { }); }); - return links; };