All files / seo/src sitemap.js

100% Statements 32/32
86.36% Branches 19/22
100% Functions 12/12
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                            13x           13x             17x               17x             2x 5x 4x   1x       2x             17x 1x     16x 16x   17x             8x 10x   10x 10x     10x 10x     10x 10x     10x     8x                   10x                       1x 1x             3x               1x             1x 1x 1x                
/**
 * Coherent.js Sitemap Generator
 * 
 * Generate XML sitemaps for SEO
 * 
 * @module seo/sitemap
 */
 
/**
 * Sitemap Generator
 * Creates XML sitemaps
 */
export class SitemapGenerator {
  constructor(options = {}) {
    this.options = {
      hostname: '',
      xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9',
      ...options
    };
    
    this.urls = [];
  }
 
  /**
   * Add URL to sitemap
   */
  add(url, options = {}) {
    this.urls.push({
      loc: this.normalizeUrl(url),
      lastmod: options.lastmod || new Date().toISOString().split('T')[0],
      changefreq: options.changefreq || 'weekly',
      priority: options.priority !== undefined ? options.priority : 0.5,
      ...options
    });
 
    return this;
  }
 
  /**
   * Add multiple URLs
   */
  addMultiple(urls) {
    urls.forEach(url => {
      if (typeof url === 'string') {
        this.add(url);
      } else {
        this.add(url.url, url);
      }
    });
 
    return this;
  }
 
  /**
   * Normalize URL
   */
  normalizeUrl(url) {
    if (url.startsWith('http')) {
      return url;
    }
    
    const hostname = this.options.hostname.replace(/\/$/, '');
    const path = url.startsWith('/') ? url : `/${url}`;
    
    return `${hostname}${path}`;
  }
 
  /**
   * Generate XML sitemap
   */
  generate() {
    const urlEntries = this.urls.map(url => {
      const entries = [`    <loc>${this.escapeXml(url.loc)}</loc>`];
      
      Eif (url.lastmod) {
        entries.push(`    <lastmod>${url.lastmod}</lastmod>`);
      }
      
      Eif (url.changefreq) {
        entries.push(`    <changefreq>${url.changefreq}</changefreq>`);
      }
      
      Eif (url.priority !== undefined) {
        entries.push(`    <priority>${url.priority}</priority>`);
      }
      
      return `  <url>\n${entries.join('\n')}\n  </url>`;
    }).join('\n');
 
    return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="${this.options.xmlns}">
${urlEntries}
</urlset>`;
  }
 
  /**
   * Escape XML special characters
   */
  escapeXml(str) {
    return String(str)
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&apos;');
  }
 
  /**
   * Clear all URLs
   */
  clear() {
    this.urls = [];
    return this;
  }
 
  /**
   * Get URL count
   */
  count() {
    return this.urls.length;
  }
}
 
/**
 * Create a sitemap generator
 */
export function createSitemapGenerator(options = {}) {
  return new SitemapGenerator(options);
}
 
/**
 * Quick sitemap generation
 */
export function generateSitemap(urls, options = {}) {
  const generator = new SitemapGenerator(options);
  generator.addMultiple(urls);
  return generator.generate();
}
 
export default {
  SitemapGenerator,
  createSitemapGenerator,
  generateSitemap
};