All files / apps/developer-portal/layers/wiki/app/utils wiki.ts

97.56% Statements 80/82
85.71% Branches 48/56
100% Functions 23/23
100% Lines 69/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217                                                                            4x                 4x   4x       4x                   4x     4x   4x 98x   296x   4x 169x 145x 145x   45x                     4x 127x 127x 127x 127x   127x 127x   58x     4x 169x 169x 169x 169x   169x 24x   24x               145x 102x 102x   102x                     43x 18x               25x 25x 169x   169x               4x 166x 142x   116x   116x     24x     4x 29x 957x 169x     86x   29x 1014x 174x   174x 126x   273x   439x 169x 43x     83x     174x             174x       4x       10x 14x 27x   19x       3x    
import type { DocKind } from "#layers/snapshot/shared/types/DocKind.ts";
import { stripLeadingEmoji, toTitleCase } from "#layers/docs/shared/docPages.ts";
import type { Translate } from "~/types/Translate";
 
/** Nothing below may compare against a capitalised path — `@nuxt/content` lower-cases every path it finds. */
 
interface WikiPage {
    path: string;
    title?: string;
}
 
export interface WikiEntry {
    /** The collection path, e.g. `/packages/ui/readme`. The route prefixes it with `/wiki`. */
    path: string;
    label: string;
    kind: DocKind;
}
 
export interface WikiGroup {
    key: string;
    label: string;
    entries: WikiEntry[];
}
 
export interface WikiSection {
    id: string;
    label: string;
    icon: string;
    /** Why this section exists, shown on the wiki home and nowhere else. */
    blurb: string;
    groups: WikiGroup[];
}
 
/**
 * The decisions and audits *rubrics* aren't excluded — they're docs about the process, not the
 * records. A `TEMPLATE.md`'s placeholder heading (`<Decision title>`) is markup `@nuxt/content`
 * treats as raw HTML and drops from the extracted title, so its nav entry would end up blank.
 */
const EXCLUDED = [
    /\/changelog$/,
    /^\/docs\/decisions\/\d{4}-/,
    /^\/docs\/audits\/\d{4}-/,
    /^\/docs\/benchmarks\/(?!readme$)/,
    /\/template$/,
];
 
/** Each project area is its own wiki section. */
const PROJECT_AREAS = ["apps", "packages", "infrastructure"] as const;
 
const SECTION_ORDER = ["workspace", "docs", ...PROJECT_AREAS, "agents"] as const;
 
type SectionId = (typeof SECTION_ORDER)[number];
 
const SECTION_ICONS = {
    workspace: "i-lucide-home",
    docs: "i-lucide-library",
    apps: "i-lucide-app-window",
    packages: "i-lucide-package",
    infrastructure: "i-lucide-server",
    agents: "i-lucide-bot",
} as const satisfies Record<SectionId, string>;
 
/** Ordered where the order carries meaning; anything new falls through to alphabetical. */
const DOCS_GROUPS = ["concepts", "guides", "decisions", "audits", "workflows", "benchmarks"];
 
/** The folders with a `wiki.groups.<folder>` label; any other folder is title-cased. */
const NAMED_GROUPS = new Set([...DOCS_GROUPS, "skills", "agents"]);
 
const groupLabel = (group: string, t: Translate, fallback: string): string =>
    NAMED_GROUPS.has(group) ? t(`wiki.groups.${group}`) : fallback;
 
const segmentsOf = (path: string): string[] => path.split("/").filter(Boolean);
 
const kindOf = (path: string): DocKind => {
    if (path.endsWith("/skill")) return "skill";
    Iif (path.endsWith("/agents") || path === "/agents") return "agent";
    if (path.startsWith("/docs/")) return "doc";
 
    return "readme";
};
 
interface Placed {
    section: SectionId;
    group: string;
    groupLabel: string;
    entry: WikiEntry;
}
 
/** A generic label for a README, since its group already names the project. */
const labelWithin = (root: string, path: string, title: string, t: Translate): string => {
    const raw = segmentsOf(path.slice(root.length));
    const rest = raw[0] === "src" ? raw.slice(1) : raw;
    const file = rest.at(-1) ?? "";
    const directory = rest.slice(0, -1).join("/");
 
    Iif (file === "skill") return stripLeadingEmoji(title);
    if (file === "readme") return directory === "" ? t("wiki.overview") : directory;
 
    return stripLeadingEmoji(title);
};
 
const place = (page: WikiPage, t: Translate): Placed => {
    const { path } = page;
    const title = page.title ?? path;
    const segments = segmentsOf(path);
    const kind = kindOf(path);
 
    if (segments[0] === ".claude") {
        const group = segments[1] ?? "skills";
 
        return {
            section: "agents",
            group,
            groupLabel: groupLabel(group, t, group),
            entry: { path, label: stripLeadingEmoji(title), kind },
        };
    }
 
    if (segments[0] === "docs") {
        const group = segments.length > 2 ? (segments[1] ?? "docs") : "docs";
        const root = group === "docs" ? "/docs" : `/docs/${group}`;
 
        return {
            section: "docs",
            group,
            groupLabel:
                group === "docs"
                    ? t("wiki.groups.start")
                    : groupLabel(group, t, toTitleCase(group)),
            entry: { path, label: labelWithin(root, path, title, t), kind },
        };
    }
 
    if (segments.length === 1) {
        return {
            section: "workspace",
            group: "root",
            groupLabel: t("wiki.groups.root"),
            entry: { path, label: stripLeadingEmoji(title), kind },
        };
    }
 
    const area = segments[0] as SectionId;
    const group = segments[1] ?? area;
    const root = `/${segments.slice(0, 2).join("/")}`;
 
    return {
        section: area,
        group,
        groupLabel: toTitleCase(group),
        entry: { path, label: labelWithin(root, path, title, t), kind },
    };
};
 
const rankGroup = (section: SectionId, key: string): number => {
    if (section === "docs") {
        if (key === "docs") return -1;
 
        const index = DOCS_GROUPS.indexOf(key);
 
        return index === -1 ? DOCS_GROUPS.length : index;
    }
 
    return 0;
};
 
export const buildWiki = (pages: WikiPage[], t: Translate): WikiSection[] => {
    const placed = pages
        .filter((page) => !EXCLUDED.some((pattern) => pattern.test(page.path)))
        .map((page) => place(page, t));
 
    /** Overview first, then everything else by label. */
    const rankEntry = (entry: WikiEntry): number => (entry.label === t("wiki.overview") ? 0 : 1);
 
    return SECTION_ORDER.map((id) => {
        const mine = placed.filter((item) => item.section === id);
        const keys = [...new Set(mine.map((item) => item.group))];
 
        const groups = keys
            .map((key) => ({
                key,
                label: mine.find((item) => item.group === key)?.groupLabel ?? key,
                entries: mine
                    .filter((item) => item.group === key)
                    .map((item) => item.entry)
                    .sort((a, b) => rankEntry(a) - rankEntry(b) || a.label.localeCompare(b.label)),
            }))
            .sort(
                (a, b) => rankGroup(id, a.key) - rankGroup(id, b.key) || a.key.localeCompare(b.key),
            );
 
        return {
            id,
            label: t(`wiki.sections.${id}.label`),
            icon: SECTION_ICONS[id],
            blurb: t(`wiki.sections.${id}.blurb`),
            groups,
        };
    }).filter((section) => section.groups.length > 0);
};
 
/** The section and group a path sits in, for the breadcrumb above a page. */
export const locate = (
    sections: WikiSection[],
    path: string,
): { section: WikiSection; group: WikiGroup; entry: WikiEntry } | null => {
    for (const section of sections) {
        for (const group of section.groups) {
            const entry = group.entries.find((candidate) => candidate.path === path);
 
            if (entry) return { section, group, entry };
        }
    }
 
    return null;
};