one
Version:
One is a new React Framework that makes Vite serve both native and web.
40 lines (39 loc) • 1.5 kB
JavaScript
import { useMemo } from "react";
import { removeSupportedExtensions } from "./matchers.mjs";
import { routeNode } from "./router.mjs";
import { sortRoutes } from "./sortRoutes.mjs";
function useSitemap() {
return useMemo(() => getSitemap(routeNode), [routeNode]);
}
function getSitemap(root) {
return root ? mapRouteToSitemap(root, []) : null;
}
function mapRouteToSitemap(route, parents) {
return {
contextKey: route.contextKey,
filename: getRouteFilename(route),
href: getRouteHref(route, parents),
isInitial: route.initialRouteName === route.route,
isInternal: route.internal ?? false,
isGenerated: route.generated ?? false,
children: [...route.children].sort(sortRoutes).map(child => mapRouteToSitemap(child, getRouteSegments(route, parents)))
};
}
function getRouteSegments(route, parents) {
return [...parents, ...route.route.split("/")];
}
function getRouteHref(route, parents) {
const path = getRouteSegments(route, parents).map(segment => segment === "index" ? "" : segment).filter(Boolean).join("/");
return `/${path}`;
}
function getRouteFilename(route) {
const contextKey = removeSupportedExtensions(route.contextKey);
const segments = contextKey.split("/");
if (route.contextKey.match(/_layout\.[jt]sx?$/)) {
return segments.slice(-2).join("/");
}
const routeSegmentsCount = route.route.split("/").length;
return segments.slice(-routeSegmentsCount).join("/");
}
export { getSitemap, useSitemap };
//# sourceMappingURL=sitemap.mjs.map