mirror of
https://github.com/Xevion/utsa-handbook.git
synced 2025-12-07 20:08:53 -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 = {
|
export type Link = {
|
||||||
text: string;
|
text: string;
|
||||||
@@ -7,21 +6,24 @@ export type Link = {
|
|||||||
header?: boolean;
|
header?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The order of the headers (lower is closer to the top)
|
||||||
const headerOrder: Record<string, number> = {
|
const headerOrder: Record<string, number> = {
|
||||||
root: 0,
|
root: 0,
|
||||||
learning: 1,
|
learning: 1,
|
||||||
living: 2,
|
living: 2,
|
||||||
improving: 3,
|
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 {
|
function toTitleCase(s: string): string {
|
||||||
return s.replace(
|
return s.replace(/\w\S*/g, function (txt) {
|
||||||
/\w\S*/g,
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
||||||
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
|
* @returns {Link[]} - An array of links to be used in the sidebar
|
||||||
*/
|
*/
|
||||||
const get = async (): Promise<Link[]> => {
|
const get = async (): Promise<Link[]> => {
|
||||||
const entries = await getCollection('handbook');
|
const entries = await getCollection("handbook");
|
||||||
const entriesByHeader: Record<string, CollectionEntry<'handbook'>[]> = {};
|
const entriesByHeader: Record<string, CollectionEntry<"handbook">[]> = {};
|
||||||
|
|
||||||
|
// Group the entries by their header
|
||||||
entries.forEach((entry) => {
|
entries.forEach((entry) => {
|
||||||
// Acquire the header using the first part of the slug (otherwise, use 'root' as the header)
|
// 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';
|
const header =
|
||||||
|
entry.slug.indexOf("/") !== -1 ? entry.slug.split("/")[0] : "root";
|
||||||
|
|
||||||
if (entriesByHeader[header])
|
if (entriesByHeader[header]) entriesByHeader[header].push(entry);
|
||||||
entriesByHeader[header].push(entry);
|
else entriesByHeader[header] = [entry];
|
||||||
else
|
|
||||||
entriesByHeader[header] = [entry];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Begin building the links array (root level first, then the rest of the entries)
|
// 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}`,
|
link: `/handbook/${entry.slug}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Remove the root level
|
||||||
delete entriesByHeader.root;
|
delete entriesByHeader.root;
|
||||||
|
|
||||||
|
// Sort the headers by the order defined in headerOrder
|
||||||
const sortedHeaders = Object.keys(entriesByHeader).sort((a, b) => {
|
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) => {
|
sortedHeaders.forEach((header) => {
|
||||||
links.push({ text: toTitleCase(header), header: true });
|
links.push({ text: toTitleCase(header), header: true });
|
||||||
entriesByHeader[header].forEach((entry) => {
|
entriesByHeader[header].forEach((entry) => {
|
||||||
@@ -64,7 +72,6 @@ const get = async (): Promise<Link[]> => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
return links;
|
return links;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user