All files / seo/src meta.js

89.88% Statements 80/89
82.14% Branches 46/56
86.36% Functions 19/22
89.28% Lines 75/84

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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318                            22x               22x                 8x 3x     8x     8x     8x   8x             4x               4x     4x   4x             4x               4x   4x             3x   3x             3x             1x   1x             1x             31x             31x             23x             23x               3x   3x 3x 3x     3x 3x   3x             1x   1x 1x     1x       1x 1x     1x       1x 2x     1x             6x   6x 1x     6x             1x   1x       1x             2x 2x                                             18x             1x 1x               1x             4x   4x 3x     4x 2x     4x 2x     4x 1x     4x 2x     4x       4x       4x 4x     4x 1x     4x 2x     4x                
/**
 * Coherent.js SEO Meta Tags
 * 
 * Utilities for generating SEO meta tags
 * 
 * @module seo/meta
 */
 
/**
 * Meta Tag Builder
 * Creates SEO-optimized meta tags
 */
export class MetaBuilder {
  constructor(defaults = {}) {
    this.defaults = {
      siteName: '',
      siteUrl: '',
      locale: 'en_US',
      twitterHandle: '',
      ...defaults
    };
    
    this.tags = [];
  }
 
  /**
   * Set page title
   */
  title(title, options = {}) {
    // A replacer function, so `$&`, `$'`, `` $` `` or `$$` in the title are
    // inserted literally instead of being read as replacement patterns.
    const fullTitle = options.template
      ? options.template.replace('%s', () => title)
      : title;
 
    this.tags.push({ title: { text: fullTitle } });
 
    // Open Graph
    this.og('title', fullTitle);
    
    // Twitter
    this.twitter('title', fullTitle);
 
    return this;
  }
 
  /**
   * Set page description
   */
  description(description) {
    this.tags.push({
      meta: {
        name: 'description',
        content: description
      }
    });
 
    // Open Graph
    this.og('description', description);
    
    // Twitter
    this.twitter('description', description);
 
    return this;
  }
 
  /**
   * Set canonical URL
   */
  canonical(url) {
    this.tags.push({
      link: {
        rel: 'canonical',
        href: url
      }
    });
 
    // Open Graph
    this.og('url', url);
 
    return this;
  }
 
  /**
   * Set keywords
   */
  keywords(keywords) {
    const keywordString = Array.isArray(keywords) ? keywords.join(', ') : keywords;
    
    this.tags.push({
      meta: {
        name: 'keywords',
        content: keywordString
      }
    });
 
    return this;
  }
 
  /**
   * Set robots directives
   */
  robots(directives) {
    const content = Array.isArray(directives) ? directives.join(', ') : directives;
    
    this.tags.push({
      meta: {
        name: 'robots',
        content
      }
    });
 
    return this;
  }
 
  /**
   * Set Open Graph tag
   */
  og(property, content) {
    this.tags.push({
      meta: {
        property: `og:${property}`,
        content
      }
    });
 
    return this;
  }
 
  /**
   * Set Twitter Card tag
   */
  twitter(name, content) {
    this.tags.push({
      meta: {
        name: `twitter:${name}`,
        content
      }
    });
 
    return this;
  }
 
  /**
   * Set image for social sharing
   */
  image(url, options = {}) {
    // Open Graph
    this.og('image', url);
    
    if (options.width) this.og('image:width', options.width);
    if (options.height) this.og('image:height', options.height);
    if (options.alt) this.og('image:alt', options.alt);
 
    // Twitter
    this.twitter('image', url);
    if (options.alt) this.twitter('image:alt', options.alt);
 
    return this;
  }
 
  /**
   * Set article metadata
   */
  article(options = {}) {
    this.og('type', 'article');
 
    Eif (options.publishedTime) {
      this.og('article:published_time', options.publishedTime);
    }
    
    Iif (options.modifiedTime) {
      this.og('article:modified_time', options.modifiedTime);
    }
    
    Eif (options.author) {
      this.og('article:author', options.author);
    }
    
    Iif (options.section) {
      this.og('article:section', options.section);
    }
    
    Eif (options.tags) {
      options.tags.forEach(tag => this.og('article:tag', tag));
    }
 
    return this;
  }
 
  /**
   * Set Twitter Card type
   */
  twitterCard(type = 'summary_large_image') {
    this.twitter('card', type);
    
    if (this.defaults.twitterHandle) {
      this.twitter('site', this.defaults.twitterHandle);
    }
 
    return this;
  }
 
  /**
   * Set locale
   */
  locale(locale, alternates = []) {
    this.og('locale', locale);
    
    alternates.forEach(alt => {
      this.og('locale:alternate', alt);
    });
 
    return this;
  }
 
  /**
   * Set site name
   */
  siteName(name) {
    this.og('site_name', name || this.defaults.siteName);
    return this;
  }
 
  /**
   * Add custom meta tag
   */
  meta(attributes) {
    this.tags.push({ meta: attributes });
    return this;
  }
 
  /**
   * Add link tag
   */
  link(attributes) {
    this.tags.push({ link: attributes });
    return this;
  }
 
  /**
   * Build all meta tags
   */
  build() {
    return this.tags;
  }
 
  /**
   * Reset builder
   */
  reset() {
    this.tags = [];
    return this;
  }
}
 
/**
 * Create a meta builder
 */
export function createMetaBuilder(defaults = {}) {
  return new MetaBuilder(defaults);
}
 
/**
 * Quick meta tags generator
 */
export function generateMeta(options = {}) {
  const builder = new MetaBuilder(options.defaults);
 
  if (options.title) {
    builder.title(options.title, { template: options.titleTemplate });
  }
 
  if (options.description) {
    builder.description(options.description);
  }
 
  if (options.canonical) {
    builder.canonical(options.canonical);
  }
 
  if (options.keywords) {
    builder.keywords(options.keywords);
  }
 
  if (options.image) {
    builder.image(options.image.url, options.image);
  }
 
  Iif (options.robots) {
    builder.robots(options.robots);
  }
 
  Iif (options.article) {
    builder.article(options.article);
  }
 
  Eif (options.twitterCard !== false) {
    builder.twitterCard(options.twitterCard);
  }
 
  if (options.locale) {
    builder.locale(options.locale, options.alternateLocales);
  }
 
  if (options.siteName) {
    builder.siteName(options.siteName);
  }
 
  return builder.build();
}
 
export default {
  MetaBuilder,
  createMetaBuilder,
  generateMeta
};