ff-webpage/composables/calculateSitemap.ts

72 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2025-04-22 13:57:17 +02:00
import type Article from "../types/collection/article";
import type Page from "../types/collection/page";
import type { ComponentTypes } from "../types/component/baseComponent";
import type SharedList from "../types/component/shared/list";
import type Global from "../types/single/global";
export default async function () {
const { find } = useStrapi();
const nuxtApp = useNuxtApp();
const { navbar, footer } = await nuxtApp.runWithContext(() => provideGlobal());
const { data: page_res } = await nuxtApp.runWithContext(() =>
useAsyncData("sitemap_pages", () =>
find<Page>("pages", {
filters: {
ref_only_access: false,
},
})
)
);
const pages = page_res.value?.data ?? [];
const accessableURLs: Array<{ path: string; origin: string; document?: string; hasCollection?: boolean }> = [];
for (const element of navbar?.navbar_items ?? []) {
if (!element.default_active_child) {
accessableURLs.push({
path: element.URL.startsWith("/") ? element.URL : `/${element.URL}`,
origin: "navbar",
document: element?.page?.documentId,
hasCollection: element.page.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
});
}
for (const subelement of element.navbar_sub_items) {
let url = `${element.URL}/${subelement.URL}`;
accessableURLs.push({
path: url.startsWith("/") ? url : `/${url}`,
origin: "navbar",
document: subelement?.page?.documentId,
hasCollection:
subelement?.page?.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
});
}
}
for (const element of pages) {
let url = element.slug.replaceAll("~", "/");
if (!accessableURLs.find((a) => a.path == url)) {
accessableURLs.push({
path: url.startsWith("/") ? url : `/${url}`,
origin: "page",
document: element.documentId,
hasCollection: element.content.filter((c: ComponentTypes) => c.__component == "shared.list").length != 0,
});
}
}
for (const element of footer?.links ?? []) {
let url = element.URL.startsWith("/") ? element.URL : `/${element.URL}`;
if (!accessableURLs.find((a) => a.path == url) && !element.URL.startsWith("http")) {
accessableURLs.push({
path: url,
origin: "footer",
document: undefined,
hasCollection: undefined,
});
}
}
return accessableURLs;
}