@marvalt/digivalt-core 0.2.8 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2453,6 +2453,216 @@ async function generateSuiteCRMData(options) {
2453
2453
  }
2454
2454
  }
2455
2455
 
2456
+ const SITEMAP_SKIP_TOP_LEVEL_KEYS = new Set([
2457
+ 'config',
2458
+ 'site_settings',
2459
+ 'header',
2460
+ 'footer',
2461
+ 'theme_styles',
2462
+ 'navigation_menu',
2463
+ 'menus',
2464
+ 'categories',
2465
+ 'tags',
2466
+ 'generated_at',
2467
+ 'frontend_id',
2468
+ 'frontend_name',
2469
+ 'front_page',
2470
+ ]);
2471
+ function normalizePathname(pathname) {
2472
+ const decoded = decodeURIComponent(pathname);
2473
+ if (!decoded || decoded === '/')
2474
+ return '/';
2475
+ const stripped = decoded.replace(/\/+$/, '');
2476
+ return stripped === '' ? '/' : stripped;
2477
+ }
2478
+ function getAllowedHosts() {
2479
+ const hosts = new Set();
2480
+ const api = process.env.VITE_WORDPRESS_API_URL || process.env.WORDPRESS_API_URL;
2481
+ if (api) {
2482
+ try {
2483
+ hosts.add(new URL(api).hostname);
2484
+ }
2485
+ catch {
2486
+ /* ignore */
2487
+ }
2488
+ }
2489
+ return hosts;
2490
+ }
2491
+ /**
2492
+ * When CMS host is unknown, accept any http(s) URL so local/offline merges still produce paths.
2493
+ */
2494
+ function urlToPath(urlStr, allowedHosts) {
2495
+ const t = urlStr.trim();
2496
+ if (!t || t === '#' || t.toLowerCase().startsWith('mailto:') || t.toLowerCase().startsWith('tel:')) {
2497
+ return null;
2498
+ }
2499
+ if (!/^https?:\/\//i.test(t)) {
2500
+ return null;
2501
+ }
2502
+ let u;
2503
+ try {
2504
+ u = new URL(t);
2505
+ }
2506
+ catch {
2507
+ return null;
2508
+ }
2509
+ if (allowedHosts.size > 0 && !allowedHosts.has(u.hostname)) {
2510
+ return null;
2511
+ }
2512
+ return normalizePathname(u.pathname);
2513
+ }
2514
+ function parsePathsJson(raw) {
2515
+ try {
2516
+ const data = JSON.parse(raw);
2517
+ if (Array.isArray(data)) {
2518
+ return data.filter((p) => typeof p === 'string' && p.startsWith('/'));
2519
+ }
2520
+ if (data && typeof data === 'object' && Array.isArray(data.paths)) {
2521
+ return data.paths.filter((p) => typeof p === 'string' && p.startsWith('/'));
2522
+ }
2523
+ }
2524
+ catch {
2525
+ /* ignore */
2526
+ }
2527
+ return [];
2528
+ }
2529
+ function loadStaticPaths(cwd, staticInputPath, allowedHosts) {
2530
+ const full = path.join(cwd, staticInputPath);
2531
+ if (!fs.existsSync(full)) {
2532
+ return [];
2533
+ }
2534
+ const raw = fs.readFileSync(full, 'utf8');
2535
+ const paths = parsePathsJson(raw);
2536
+ const out = [];
2537
+ for (const p of paths) {
2538
+ if (p.startsWith('http://') || p.startsWith('https://')) {
2539
+ const normalized = urlToPath(p, allowedHosts);
2540
+ if (normalized)
2541
+ out.push(normalized);
2542
+ }
2543
+ else {
2544
+ out.push(normalizePathname(p));
2545
+ }
2546
+ }
2547
+ return out;
2548
+ }
2549
+ function walkMenuItems(items, allowedHosts, out) {
2550
+ if (!Array.isArray(items))
2551
+ return;
2552
+ for (const item of items) {
2553
+ if (!item || typeof item !== 'object')
2554
+ continue;
2555
+ const rec = item;
2556
+ if (typeof rec.url === 'string') {
2557
+ const p = urlToPath(rec.url, allowedHosts);
2558
+ if (p)
2559
+ out.add(p);
2560
+ }
2561
+ if (Array.isArray(rec.children)) {
2562
+ walkMenuItems(rec.children, allowedHosts, out);
2563
+ }
2564
+ }
2565
+ }
2566
+ function collectLinksFromArray(arr, allowedHosts, out) {
2567
+ for (const item of arr) {
2568
+ if (!item || typeof item !== 'object')
2569
+ continue;
2570
+ const link = item.link;
2571
+ if (typeof link === 'string') {
2572
+ const p = urlToPath(link, allowedHosts);
2573
+ if (p)
2574
+ out.add(p);
2575
+ }
2576
+ }
2577
+ }
2578
+ function collectPathsFromWpData(data, allowedHosts) {
2579
+ const out = new Set();
2580
+ const menu = data.navigation_menu;
2581
+ if (menu?.items) {
2582
+ walkMenuItems(menu.items, allowedHosts, out);
2583
+ }
2584
+ const config = data.config;
2585
+ const postTypeKeys = config?.post_types && typeof config.post_types === 'object'
2586
+ ? Object.keys(config.post_types)
2587
+ : null;
2588
+ if (postTypeKeys && postTypeKeys.length > 0) {
2589
+ for (const key of postTypeKeys) {
2590
+ const arr = data[key];
2591
+ if (Array.isArray(arr)) {
2592
+ collectLinksFromArray(arr, allowedHosts, out);
2593
+ }
2594
+ }
2595
+ }
2596
+ else {
2597
+ for (const [key, value] of Object.entries(data)) {
2598
+ if (SITEMAP_SKIP_TOP_LEVEL_KEYS.has(key))
2599
+ continue;
2600
+ if (Array.isArray(value)) {
2601
+ collectLinksFromArray(value, allowedHosts, out);
2602
+ }
2603
+ }
2604
+ }
2605
+ return out;
2606
+ }
2607
+ function writeFallback(cwd, outputPath) {
2608
+ const full = path.join(cwd, outputPath);
2609
+ fs.mkdirSync(path.dirname(full), { recursive: true });
2610
+ fs.writeFileSync(full, `${JSON.stringify({ paths: ['/'] }, null, 2)}\n`, 'utf8');
2611
+ }
2612
+ /**
2613
+ * Merge sitemap-static-paths.json, navigation_menu URLs, and CPT/post links from wordpress-data.json.
2614
+ */
2615
+ async function generateSitemapPaths(options = {}) {
2616
+ dotenv.config();
2617
+ dotenv.config({ path: '.env.local' });
2618
+ const cwd = options.cwd ?? process.cwd();
2619
+ const wpDataPath = options.wpDataPath ?? 'public/wordpress-data.json';
2620
+ const staticInputPath = options.staticInputPath ?? 'public/sitemap-static-paths.json';
2621
+ const outputPath = options.outputPath ?? 'public/sitemap-paths.json';
2622
+ const allowedHosts = getAllowedHosts();
2623
+ const merged = new Set();
2624
+ for (const p of loadStaticPaths(cwd, staticInputPath, allowedHosts)) {
2625
+ merged.add(normalizePathname(p));
2626
+ }
2627
+ const wpFull = path.join(cwd, wpDataPath);
2628
+ if (fs.existsSync(wpFull)) {
2629
+ try {
2630
+ const raw = fs.readFileSync(wpFull, 'utf8');
2631
+ const data = JSON.parse(raw);
2632
+ const fromWp = collectPathsFromWpData(data, allowedHosts);
2633
+ for (const p of fromWp) {
2634
+ merged.add(p);
2635
+ }
2636
+ }
2637
+ catch (e) {
2638
+ console.warn('⚠️ Could not parse wordpress-data.json for sitemap paths:', e.message);
2639
+ }
2640
+ }
2641
+ merged.add('/');
2642
+ const sorted = [...merged].sort((a, b) => {
2643
+ if (a === '/')
2644
+ return -1;
2645
+ if (b === '/')
2646
+ return 1;
2647
+ return a.localeCompare(b);
2648
+ });
2649
+ const outFull = path.join(cwd, outputPath);
2650
+ fs.mkdirSync(path.dirname(outFull), { recursive: true });
2651
+ fs.writeFileSync(outFull, `${JSON.stringify({ paths: sorted }, null, 2)}\n`, 'utf8');
2652
+ console.log(`📍 Sitemap paths written (${sorted.length} URLs): ${outFull}`);
2653
+ }
2654
+ async function generateSitemapPathsSafe(options = {}) {
2655
+ try {
2656
+ await generateSitemapPaths(options);
2657
+ }
2658
+ catch (e) {
2659
+ console.error('❌ Sitemap path generation failed; writing minimal / only:', e.message);
2660
+ const cwd = options.cwd ?? process.cwd();
2661
+ const outputPath = options.outputPath ?? 'public/sitemap-paths.json';
2662
+ writeFallback(cwd, outputPath);
2663
+ }
2664
+ }
2665
+
2456
2666
  async function generateAllData() {
2457
2667
  // Load .env files if they exist (for local development)
2458
2668
  dotenv.config();
@@ -2470,6 +2680,7 @@ async function generateAllData() {
2470
2680
  console.error('❌ Some generation tasks failed. See above for details.');
2471
2681
  process.exit(1);
2472
2682
  }
2683
+ await generateSitemapPathsSafe();
2473
2684
  console.log('✅ Generation complete!');
2474
2685
  }
2475
2686
 
@@ -2478,6 +2689,8 @@ var index = /*#__PURE__*/Object.freeze({
2478
2689
  generateAllData: generateAllData,
2479
2690
  generateGravityFormsData: generateGravityFormsData,
2480
2691
  generateMauticData: generateMauticData,
2692
+ generateSitemapPaths: generateSitemapPaths,
2693
+ generateSitemapPathsSafe: generateSitemapPathsSafe,
2481
2694
  generateSuiteCRMData: generateSuiteCRMData,
2482
2695
  generateWordPressData: generateWordPressData
2483
2696
  });
@@ -2493,6 +2706,8 @@ const getDigiValtVersion = () => {
2493
2706
  exports.generateAllData = generateAllData;
2494
2707
  exports.generateGravityFormsData = generateGravityFormsData;
2495
2708
  exports.generateMauticData = generateMauticData;
2709
+ exports.generateSitemapPaths = generateSitemapPaths;
2710
+ exports.generateSitemapPathsSafe = generateSitemapPathsSafe;
2496
2711
  exports.generateSuiteCRMData = generateSuiteCRMData;
2497
2712
  exports.generateWordPressData = generateWordPressData;
2498
2713
  exports.generators = index;