@paroicms/server
Version:
The ParoiCMS server
102 lines • 4.48 kB
JavaScript
import { getDocumentTypeByName } from "@paroicms/internal-anywhere-lib";
import { getNodeAncestorsWithSlug, } from "../../admin-backend/node/node-cte.queries.js";
import { makeCacheDependencyKey, makeCacheKey } from "../../common/text-cache.js";
import { shouldPrefixWithLanguage } from "../../helpers/url-helpers.js";
export function getHomeUrl(siteContext, language) {
return shouldPrefixWithLanguage(siteContext.siteSchema, language) ? `/${language}` : "/";
}
export async function getUrlOfDocument(siteContext, tracker, documentId, options = {}) {
const cacheKey = makeCacheKey(siteContext.logger, { kind: "url", documentId });
const cacheValue = await siteContext.textCache.getCacheValue(cacheKey, tracker);
if (cacheValue) {
if (!options.returnUrlDependencyKeys)
return cacheValue.value;
return {
url: cacheValue.value,
dependencyKeys: await siteContext.textCache.getDependencyKeys(cacheKey, tracker),
};
}
const ancestors = await getNodeAncestorsWithSlug(siteContext, tracker, documentId);
const publishedAncestors = ensureAncestorsArePublished(ancestors);
if (!publishedAncestors) {
if (options.returnUndef)
return;
throw new Error(`Document '${documentId.nodeId}:${documentId.language}' is not published yet`);
}
publishedAncestors.reverse();
publishedAncestors.shift();
const { language } = documentId;
let dependencyKeys;
let url;
if (publishedAncestors.length === 0) {
url = getHomeUrl(siteContext, language);
dependencyKeys = [];
}
else {
url = makeDocumentUrl(siteContext, publishedAncestors, language);
dependencyKeys = makeUrlDependencyKeys(publishedAncestors, language);
}
await siteContext.textCache.setCacheValue({
cacheKey,
value: url,
dependencyKeys,
}, tracker);
return options.returnUrlDependencyKeys ? { url, dependencyKeys } : url;
}
function ensureAncestorsArePublished(ancestors) {
const now = Date.now();
for (const ancestor of ancestors) {
if (!ancestor.ready ||
ancestor.publishDate === undefined ||
ancestor.publishDate.getTime() > now) {
return;
}
}
return ancestors;
}
function makeDocumentUrl(siteContext, ancestors, language) {
const { siteSchema } = siteContext;
const urlSegments = shouldPrefixWithLanguage(siteContext.siteSchema, language) ? [language] : [];
let parent = getDocumentTypeByName(siteSchema, "home");
for (const ancestor of ancestors) {
if (!parent.regularChildren?.includes(ancestor.typeName) &&
!parent.routingChildren?.includes(ancestor.typeName)) {
throw new Error(`schema and database are not compatible, '${ancestor.typeName}' should not be a child of '${parent.typeName}'`);
}
const docType = getDocumentTypeByName(siteSchema, ancestor.typeName);
if (docType.documentKind === "routing") {
const routePart = docType.route?.[language];
if (!routePart) {
throw new Error(`missing urlPath['${language}'] in document type '${docType.typeName}'`);
}
urlSegments.push(routePart);
}
else {
if (docType.route === ":relativeId-:slug") {
urlSegments.push(makeRelativeIdSlugUrlPart(ancestor));
}
else if (docType.route === ":yyyy/:mm/:dd/:relativeId-:slug") {
const { publishDate } = ancestor;
urlSegments.push(String(publishDate.getFullYear()).padStart(4, "0"));
urlSegments.push(String(publishDate.getMonth() + 1).padStart(2, "0"));
urlSegments.push(String(publishDate.getDate()).padStart(2, "0"));
urlSegments.push(makeRelativeIdSlugUrlPart(ancestor));
}
else {
throw new Error(`in '${docType.typeName}', unknown value for route: '${docType.route}'`);
}
}
parent = docType;
}
return `/${urlSegments.join("/")}`;
}
function makeUrlDependencyKeys(ancestors, language) {
return ancestors.map((part) => makeCacheDependencyKey({
documentId: { language, nodeId: part.nodeId },
perimeter: "slug",
}));
}
function makeRelativeIdSlugUrlPart(part) {
return part.slug ? `${part.relativeId}-${part.slug}` : part.relativeId;
}
//# sourceMappingURL=make-url.js.map