All files / seo/src structured-data.js

89.74% Statements 35/39
77.77% Branches 28/36
100% Functions 16/16
89.74% Lines 35/39

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252                            18x             18x 18x             5x                               3x                                   2x                                                       2x                                                         2x     4x                         2x     3x                             1x                                     11x 2x     9x   11x                       6x 6x             1x 1x               1x             6x   6x   1x 1x         1x 1x   1x 1x   1x 1x   1x 1x         1x     6x                
/**
 * Coherent.js Structured Data
 * 
 * Generate JSON-LD structured data for SEO
 * 
 * @module seo/structured-data
 */
 
/**
 * Structured Data Builder
 * Creates JSON-LD structured data
 */
export class StructuredDataBuilder {
  constructor() {
    this.schemas = [];
  }
 
  /**
   * Add schema
   */
  add(schema) {
    this.schemas.push(schema);
    return this;
  }
 
  /**
   * Create Organization schema
   */
  organization(data) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'Organization',
      name: data.name,
      url: data.url,
      logo: data.logo,
      description: data.description,
      contactPoint: data.contactPoint,
      sameAs: data.socialLinks
    });
  }
 
  /**
   * Create WebSite schema
   */
  website(data) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'WebSite',
      name: data.name,
      url: data.url,
      description: data.description,
      potentialAction: data.searchAction ? {
        '@type': 'SearchAction',
        target: data.searchAction.target,
        'query-input': data.searchAction.queryInput || 'required name=search_term_string'
      } : undefined
    });
  }
 
  /**
   * Create Article schema
   */
  article(data) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'Article',
      headline: data.headline,
      description: data.description,
      image: data.image,
      author: data.author ? {
        '@type': 'Person',
        name: data.author.name,
        url: data.author.url
      } : undefined,
      publisher: data.publisher ? {
        '@type': 'Organization',
        name: data.publisher.name,
        logo: data.publisher.logo ? {
          '@type': 'ImageObject',
          url: data.publisher.logo
        } : undefined
      } : undefined,
      datePublished: data.datePublished,
      dateModified: data.dateModified
    });
  }
 
  /**
   * Create Product schema
   */
  product(data) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'Product',
      name: data.name,
      description: data.description,
      image: data.image,
      brand: data.brand ? {
        '@type': 'Brand',
        name: data.brand
      } : undefined,
      offers: data.offers ? {
        '@type': 'Offer',
        price: data.offers.price,
        priceCurrency: data.offers.currency || 'USD',
        availability: data.offers.availability || 'https://schema.org/InStock',
        url: data.offers.url
      } : undefined,
      aggregateRating: data.rating ? {
        '@type': 'AggregateRating',
        ratingValue: data.rating.value,
        reviewCount: data.rating.count
      } : undefined
    });
  }
 
  /**
   * Create BreadcrumbList schema
   */
  breadcrumb(items) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'BreadcrumbList',
      itemListElement: items.map((item, index) => ({
        '@type': 'ListItem',
        position: index + 1,
        name: item.name,
        item: item.url
      }))
    });
  }
 
  /**
   * Create FAQ schema
   */
  faq(questions) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'FAQPage',
      mainEntity: questions.map(q => ({
        '@type': 'Question',
        name: q.question,
        acceptedAnswer: {
          '@type': 'Answer',
          text: q.answer
        }
      }))
    });
  }
 
  /**
   * Create Person schema
   */
  person(data) {
    return this.add({
      '@context': 'https://schema.org',
      '@type': 'Person',
      name: data.name,
      url: data.url,
      image: data.image,
      jobTitle: data.jobTitle,
      worksFor: data.organization ? {
        '@type': 'Organization',
        name: data.organization
      } : undefined,
      sameAs: data.socialLinks
    });
  }
 
  /**
   * Build JSON-LD script tag
   */
  build() {
    if (this.schemas.length === 0) {
      return null;
    }
 
    const data = this.schemas.length === 1 ? this.schemas[0] : this.schemas;
 
    return {
      script: {
        type: 'application/ld+json',
        text: JSON.stringify(data, null, 2)
      }
    };
  }
 
  /**
   * Build as JSON string
   */
  toJSON() {
    const data = this.schemas.length === 1 ? this.schemas[0] : this.schemas;
    return JSON.stringify(data, null, 2);
  }
 
  /**
   * Clear all schemas
   */
  clear() {
    this.schemas = [];
    return this;
  }
}
 
/**
 * Create a structured data builder
 */
export function createStructuredData() {
  return new StructuredDataBuilder();
}
 
/**
 * Quick structured data generation
 */
export function generateStructuredData(type, data) {
  const builder = new StructuredDataBuilder();
  
  switch (type) {
    case 'organization':
      builder.organization(data);
      break;
    case 'website':
      builder.website(data);
      break;
    case 'article':
      builder.article(data);
      break;
    case 'product':
      builder.product(data);
      break;
    case 'breadcrumb':
      builder.breadcrumb(data);
      break;
    case 'faq':
      builder.faq(data);
      break;
    case 'person':
      builder.person(data);
      break;
    default:
      builder.add(data);
  }
  
  return builder.build();
}
 
export default {
  StructuredDataBuilder,
  createStructuredData,
  generateStructuredData
};