mirror of
https://github.com/Xevion/utsa-handbook.git
synced 2025-12-06 01:16:46 -06:00
Improve sidebar generation docs, formatting
This commit is contained in:
@@ -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<string, number> = {
|
||||
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<Link[]> => {
|
||||
const entries = await getCollection('handbook');
|
||||
const entriesByHeader: Record<string, CollectionEntry<'handbook'>[]> = {};
|
||||
const entries = await getCollection("handbook");
|
||||
const entriesByHeader: Record<string, CollectionEntry<"handbook">[]> = {};
|
||||
|
||||
// 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[]> => {
|
||||
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<Link[]> => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
return links;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user