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

100% Statements 30/30
85.71% Branches 12/14
100% Functions 15/15
100% Lines 25/25

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        1x 8x     1x 7x 7x 8x   7x 7x 7x 8x 8x       8x   8x                     7x 6x 6x   7x   6x 12x 6x     7x   8x     7x               8x   6x          
import type { BreadcrumbItem } from "@nuxt/ui";
import { toCollectionPath } from "#layers/docs/shared/docPages.ts";
import { locate } from "#layers/wiki/app/utils/wiki.ts";
 
const slugOf = (param: string | string[] | undefined): string[] =>
    Array.isArray(param) ? param : [];
 
/** One rendered doc, where it sits in the tree and what the snapshot knows of it. */
export const useWikiPage = () => {
    const route = useRoute();
    const { t } = useI18n();
    const path = computed(() => `/${slugOf(route.params.slug).join("/")}`.toLowerCase());
 
    const wiki = useWiki();
    const sections = wiki.data;
    const content = useAsyncData(
        () => `doc-${path.value}`,
        () => queryCollection("docs").path(path.value).first(),
        { watch: [path] },
    );
 
    const here = computed(() => locate(sections.value, path.value));
 
    const crumbs = computed<BreadcrumbItem[]>(() => [
        { label: t("wiki.title"), to: "/wiki", icon: "i-lucide-library" },
        ...(here.value
            ? [
                  { label: here.value.section.label, icon: here.value.section.icon },
                  { label: here.value.group.label, class: "font-mono" },
                  { label: here.value.entry.label },
              ]
            : []),
    ]);
 
    const around = computed(() => {
        const entries = here.value?.group.entries ?? [];
        const { previous, next } = neighbours(
            entries,
            entries.findIndex((entry) => entry.path === path.value),
        );
        const link = (entry: typeof previous) =>
            entry ? { to: `/wiki${entry.path}`, title: entry.label } : null;
        return { previous: link(previous), next: link(next) };
    });
 
    const meta = computed(
        () =>
            docsArtifact()?.pages.find((doc) => toCollectionPath(doc.path) === path.value) ?? null,
    );
 
    return {
        path,
        sections,
        page: content.data,
        here,
        crumbs,
        around,
        meta,
        source: computed(() => meta.value?.path ?? `${path.value.slice(1)}.md`),
        brokenLinks: computed(
            () => meta.value?.brokenLinks.map((link) => link.href).join(", ") ?? "",
        ),
        ready: Promise.all([wiki, content]),
    };
};