import { normalizePublicationSnapshot, resolveIndexPolicy, } from './mindspace-index-policy.mjs'; import { injectGeoTags } from './mindspace-geo-tags.mjs'; import { injectRobotsNoindex, injectSeoTags } from './mindspace-seo-tags.mjs'; export const ROBOTS_NOINDEX_HEADER = 'noindex, nofollow'; export function buildPublicationForSeoGeo({ publication = null, pageDataContext = null, publicationSnapshot = null, } = {}) { if (publicationSnapshot) { return normalizePublicationSnapshot(publicationSnapshot); } if (publication) { return normalizePublicationSnapshot(publication); } if (pageDataContext?.accessMode) { return normalizePublicationSnapshot({ accessMode: pageDataContext.accessMode, status: 'online', userConfirmedAt: null, publicationId: pageDataContext.publicationId, pageId: pageDataContext.pageId, }); } return null; } export function decorateMindSpaceSeoGeoHtml( html, { seoGeoConfig = null, publication = null, pageDataContext = null, publicationSnapshot = null, embed = false, context = {}, innerHtml = null, htmlFilePath = '', fileExists = null, meta = {}, } = {}, ) { const resolvedPublication = buildPublicationForSeoGeo({ publication, pageDataContext, publicationSnapshot, }); const policy = resolveIndexPolicy({ seoGeoConfig, publication: resolvedPublication, embed, }); if (policy.mode === 'off') { return { html: String(html ?? ''), robotsHeader: null, policy }; } if (policy.injectNoindex) { return { html: injectRobotsNoindex(html), robotsHeader: ROBOTS_NOINDEX_HEADER, policy, }; } let nextHtml = String(html ?? ''); const sourceHtml = String(innerHtml ?? html ?? ''); if (policy.seoEnabled) { nextHtml = injectSeoTags(nextHtml, { ...context, publication: resolvedPublication, canonicalEnabled: policy.canonicalEnabled, htmlFilePath, fileExists, meta, }); } if (policy.geoEnabled && policy.jsonLdEnabled) { nextHtml = injectGeoTags(nextHtml, { ...context, publication: resolvedPublication, sourceHtml, htmlFilePath, fileExists, meta, }); } return { html: nextHtml, robotsHeader: null, policy }; } export function applySeoGeoResponseHeaders(res, robotsHeader = null) { if (!robotsHeader || !res || typeof res.set !== 'function') return; res.set('X-Robots-Tag', robotsHeader); }