Sha256: 203ba1da4e05c593f5c87d98fa795e401600e2d28128dbdd449aaec3671684cd

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

class GHBlog {
  constructor(user, repo) {
    this.API_URL = 'https://api.github.com';
    this.POSTS_URL = `${this.API_URL}/repos/${user}/${repo}/contents/posts`;
    this.COMMITS_URL = `${this.API_URL}/repos/${user}/${repo}/commits?path=`;
  }

  getPosts() {
    return fetch(this.POSTS_URL)
      .then(result => result.json())
      .then(infos => Promise.all(infos.map(this.loadPost.bind(this))));
    }

  loadPost(info) {
    const loadedSha = localStorage[`${info.path}-sha`];
    if(loadedSha && loadedSha == info.sha) {
      return Promise.resolve({path: info.path, sha: info.sha});
    } else {
      return Promise.all([
          this.fetchPost(info.url),
          this.fetchCommits(info.path)
        ]).then(results => {
          const [html, commits] = results;
          localStorage[`${info.path}-sha`] = info.sha;
          return {html, commits, path: info.path, sha: info.sha, updated: true};
        });
    }
  }

  fetchPost(url) {
    return fetch(url, {
        headers: {
          accept: 'application/vnd.github.v3.html+json'
        }
      }).then(result => result.text());
  }

  fetchCommits(path) {
    return fetch(`${this.COMMITS_URL}${path}`)
      .then(result => result.json())
      .then(commits => {
        const created = commits[commits.length - 1].commit.author.date;
        const updated = commits[0].commit.author.date;
        return {
          created: new Date(created).toLocaleString(),
          updated: new Date(updated).toLocaleString()
        };
      });
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
casjaysdev-jekyll-theme-0.1.7 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.1.6 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.1.4 assets/js/gh-blog.js