import fs from 'node:fs'; import path from 'node:path'; import { backfillMindspaceSeoGeoHtml } from './mindspace-geo-backfill.mjs'; function isPublicHtmlPath(value) { const normalized = String(value ?? '').replace(/\\/g, '/'); return /(?:^|\/)public\/[^/]+\.html$/i.test(normalized); } export function prepareMindspacePublicHtmlForDisk(html, { htmlFilePath = '' } = {}) { const source = String(html ?? ''); const filePath = String(htmlFilePath ?? ''); if (!source.trim()) return source; if (filePath && !isPublicHtmlPath(filePath)) return source; if (!filePath && !/]/i.test(source)) return source; return backfillMindspaceSeoGeoHtml(source, { htmlFilePath: filePath, fixWarnings: true, }); } export function materializeMindspaceSeoGeoOnDisk( absolutePath, { forceGeo = false, fixWarnings = true, } = {}, ) { const filePath = path.resolve(String(absolutePath ?? '')); if (!filePath || !/\.html$/i.test(filePath)) { return { changed: false, html: '' }; } const before = fs.readFileSync(filePath, 'utf8'); const after = backfillMindspaceSeoGeoHtml(before, { htmlFilePath: filePath, forceGeo, fixWarnings, }); if (after === before) { return { changed: false, html: before }; } fs.writeFileSync(filePath, after, 'utf8'); return { changed: true, html: after }; }