|
| 1 | +import type { z } from "zod" |
| 2 | +import { EntryGroup, isDirectory, isFile } from "renoun/file-system" |
| 3 | + |
| 4 | +import type { frontmatterSchema } from "./validations" |
| 5 | +import { removeFromArray } from "./lib/utils" |
| 6 | +import { generateDirectories } from "./sources" |
| 7 | + |
| 8 | +export const DocumentationGroup = new EntryGroup({ |
| 9 | + entries: [...generateDirectories()], |
| 10 | +}) |
| 11 | + |
| 12 | +export type EntryType = Awaited<ReturnType<typeof DocumentationGroup.getEntry>> |
| 13 | +export type DirectoryType = Awaited<ReturnType<typeof DocumentationGroup.getDirectory>> |
| 14 | + |
| 15 | +/** |
| 16 | + * Helper function to get the title for an element in the sidebar/navigation |
| 17 | + * @param collection {EntryType} the collection to get the title for |
| 18 | + * @param frontmatter {z.infer<typeof frontmatterSchema>} the frontmatter to get the title from |
| 19 | + * @param includeTitle? {boolean} whether to include the title in the returned string |
| 20 | + * @returns {string} the title to be displayed in the sidebar/navigation |
| 21 | + */ |
| 22 | +export function getTitle( |
| 23 | + collection: EntryType, |
| 24 | + frontmatter: z.infer<typeof frontmatterSchema>, |
| 25 | + includeTitle = false, |
| 26 | +): string { |
| 27 | + return includeTitle |
| 28 | + ? (frontmatter.navTitle ?? frontmatter.title ?? collection.getTitle()) |
| 29 | + : (frontmatter.navTitle ?? collection.getTitle()) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Helper function to get the file content for a given source entry |
| 34 | + * This function will try to get the file based on the given path and the "mdx" extension |
| 35 | + * If the file is not found, it will try to get the index file based on the given path and the "mdx" extension |
| 36 | + * If there is also no index file, it will return null |
| 37 | + * |
| 38 | + * @param source {EntryType} the source entry to get the file content for |
| 39 | + */ |
| 40 | +export const getFileContent = async (source: EntryType) => { |
| 41 | + // first, try to get the file based on the given path |
| 42 | + |
| 43 | + return await DocumentationGroup.getFile(source.getPathSegments(), "mdx").catch(async () => { |
| 44 | + return await DocumentationGroup.getFile([...source.getPathSegments(), "index"], "mdx").catch( |
| 45 | + () => null, |
| 46 | + ) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Helper function to get the sections for a given source entry |
| 52 | + * This function will try to get the sections based on the given path |
| 53 | + * |
| 54 | + * If there there are no entries/children for the current path, it will return an empty array |
| 55 | + * |
| 56 | + * @param source {EntryType} the source entry to get the sections for |
| 57 | + * @returns |
| 58 | + */ |
| 59 | +export async function getSections(source: EntryType) { |
| 60 | + if (source.getDepth() > -1) { |
| 61 | + if (isDirectory(source)) { |
| 62 | + return ( |
| 63 | + await (await DocumentationGroup.getDirectory(source.getPathSegments())).getEntries() |
| 64 | + ).filter((ele) => ele.getPath() !== source.getPath()) |
| 65 | + } |
| 66 | + |
| 67 | + if (isFile(source) && source.getBaseName() === "index") { |
| 68 | + return await source.getParent().getEntries() |
| 69 | + } |
| 70 | + return [] |
| 71 | + } else { |
| 72 | + return ( |
| 73 | + await (await DocumentationGroup.getDirectory(source.getPathSegments())).getEntries() |
| 74 | + ).filter((ele) => ele.getPath() !== source.getPath()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Helper function to get the breadcrumb items for a given slug |
| 80 | + * |
| 81 | + * @param slug {string[]} the slug to get the breadcrumb items for |
| 82 | + */ |
| 83 | +export const getBreadcrumbItems = async (slug: string[]) => { |
| 84 | + // we do not want to have "index" as breadcrumb element |
| 85 | + const cleanedSlug = removeFromArray(slug, ["index"]) |
| 86 | + |
| 87 | + const combinations = cleanedSlug.map((_, index) => cleanedSlug.slice(0, index + 1)) |
| 88 | + |
| 89 | + const items = [] |
| 90 | + |
| 91 | + for (const currentPageSegement of combinations) { |
| 92 | + let collection: EntryType |
| 93 | + let file: Awaited<ReturnType<typeof getFileContent>> |
| 94 | + let frontmatter: z.infer<typeof frontmatterSchema> | undefined |
| 95 | + try { |
| 96 | + collection = await DocumentationGroup.getEntry(currentPageSegement) |
| 97 | + if (collection.getPathSegments().includes("index")) { |
| 98 | + file = await getFileContent(collection.getParent()) |
| 99 | + } else { |
| 100 | + file = await getFileContent(collection) |
| 101 | + } |
| 102 | + |
| 103 | + frontmatter = await file?.getExportValue("frontmatter") |
| 104 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 105 | + } catch (e: unknown) { |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + if (!frontmatter) { |
| 110 | + items.push({ |
| 111 | + title: collection.getTitle(), |
| 112 | + path: ["docs", ...collection.getPathSegments()], |
| 113 | + }) |
| 114 | + } else { |
| 115 | + const title = getTitle(collection, frontmatter, true) |
| 116 | + items.push({ |
| 117 | + title, |
| 118 | + path: ["docs", ...removeFromArray(collection.getPathSegments(), ["index"])], |
| 119 | + }) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return items |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Checks if an entry is hidden (starts with an underscore) |
| 128 | + * |
| 129 | + * @param entry {EntryType} the entry to check for visibility |
| 130 | + */ |
| 131 | +export function isHidden(entry: EntryType) { |
| 132 | + return entry.getBaseName().startsWith("_") |
| 133 | +} |
0 commit comments