Source: types.js

/**
 * @module types
 * @description Types here are only data containers and prototypes,
 * helper functions should be added into utils to
 * prevent circular require dependencies when using other utils functions.
 */

/**
 * @description Site's data.
 */
class Site {
  /**
   * @param {String} siteDir
   * @property {Object} siteConfig
   * @property {Object} themeConfig
   * @property {Map} languages
   * @property {Map} layouts
   * @property {File[]} posts
   * @property {File[]} pages
   * @property {File[]} assets
   * @property {File[]} files Generated by generator.
   * @property {Category[]} categories
   * @property {Number} categoriesLength Flattened categories length.
   * @property {Tag[]} tags
   * @property {Number} tagsLength Flattened tags length.
   * @return {Site}
   */
  constructor(siteDir) {
    this.siteDir = siteDir;
    this.siteConfig = {};
    this.themeConfig = {};
    this.languages = new Map();
    this.layouts = new Map();
    this.posts = [];
    this.pages = [];
    this.assets = [];
    this.files = [];
    this.categories = [];
    this.categoriesLength = 0;
    this.tags = [];
    this.tagsLength = 0;
  }
}

/**
 * @description Available keys for site file arrays.
 * @static
 */
Site.arrayKeys = ["posts", "pages", "assets", "files"];

/**
 * @description File's data. Properties of parameters will be assigned to File.
 * You must provide `docDir` and `docPath` to File so it could be saved, or
 * provide `docDir` and `srcPath`, and ensure `docPath` will be generated from
 * `srcPath` by renderer.
 */
class File {
  /**
   * @param {...Object} objects
   * @property {String} srcDir
   * @property {String} srcPath
   * @property {String} docDir
   * @property {String} docPath Maybe generated by renderer from `srcPath`.
   * @property {Boolean} binary Whether file is a binary.
   * @property {String} created
   * @property {String} updated
   * @property {String} title
   * @property {String} layout
   * @property {Boolean} draft Whether file is a draft.
   * @property {Buffer|String} raw Raw file content that is read from disk.
   * @property {String} text File content after front-matter.
   * @property {String} content Rendered content that is wrote to disk.
   * @property {Object} frontMatter
   * @property {Category[]} categories Only available for posts.
   * @property {Tag[]} tags Only available for posts.
   * @property {Category} category Current category if this is a category page.
   * @property {Tag} tag Current tag if this is a tag page.
   * @property {String} excerpt
   * @property {String} more Content after excerpt.
   * @property {TOC[]} toc Content TOC.
   * @property {File[]} files Files belonging to this file.
   * @property {File[]} posts Posts belonging to this file. Typically available
   * for pages like home or archives.
   * @property {File[]} pages If this page is paginated, this contains all
   * paginated pages. Generated by `paginate()`.
   * @property {Number} index If this page is paginated, this is the index of
   * current page in all paginated pages. Generated by `paginate()`.
   * @property {File} next Next post.
   * @property {File} prev Prev post.
   */
  constructor(...objects) {
    // Most members are optional, so set things like tags, posts and pages to
    // `null` for File. This is different from what I do for Site and Tags.
    this.srcDir = null;
    this.srcPath = null;
    this.docDir = null;
    this.docPath = null;
    this.binary = false;
    this.created = null;
    this.updated = null;
    this.title = null;
    this.layout = null;
    this.draft = false;
    this.raw = null;
    this.text = null;
    this.content = null;
    this.frontMatter = null;
    this.categories = null;
    this.tags = null;
    this.categroy = null;
    this.tag = null;
    this.excerpt = null;
    this.more = null;
    this.toc = null;
    this.files = null;
    this.posts = null;
    this.pages = null;
    this.index = 0;
    this.next = null;
    this.prev = null;
    Object.assign(this, ...objects);
  }
}

/**
 * @description Category data.
 */
class Category {
  /**
   * @param {String} name Category name.
   * @param {File[]} posts Posts belonging to this category.
   * @param {Category[]} subs Sub categories.
   * @property {String} name Category name.
   * @property {String} docPath
   * @property {File[]} posts Posts belonging to this category.
   * @property {Category[]} subs Sub categories.
   */
  constructor(name, posts = [], subs = []) {
    this.name = name;
    this.docPath = null;
    this.posts = posts;
    this.subs = subs;
  }
}

/**
 * @description Tag data.
 */
class Tag {
  /**
   * @param {String} name Tag name.
   * @param {File[]} posts Posts belonging to this tag.
   * @property {String} name Tag name.
   * @property {String} docPath
   * @property {File[]} posts Posts belonging to this tag.
   */
  constructor(name, posts = []) {
    this.name = name;
    this.docPath = null;
    this.posts = posts;
  }
}

/**
 * @description TOC data.
 */
class TOC {
  /**
   * @param {String} name TOC name.
   * @param {String} anchor HTML ID as the anchor.
   * @param {String} text Title text.
   * @param {TOC[]} subs Sub TOCs.
   * @property {String} name TOC name.
   * @property {String} anchor HTML ID as the anchor.
   * @property {String} text Title text.
   * @property {TOC[]} subs Sub TOCs.
   */
  constructor(name, anchor, text, subs = []) {
    this.name = name;
    this.anchor = anchor;
    // Fix a typo without an API break...
    this.archor = anchor;
    this.text = text;
    this.subs = subs;
  }
}

export {
  Site,
  File,
  Category,
  Tag,
  TOC
};