Sha256: 5e6c0508e88f9dd6252ecada53e1b7c7f742b3afb22c4d382bd67675b27da283

Contents?: true

Size: 1.49 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
casjaysdev-jekyll-theme-0.2.6 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.2.5 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.2.4 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.2.3 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.2.2 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.2.1 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.1.9 assets/js/gh-blog.js
casjaysdev-jekyll-theme-0.1.8 assets/js/gh-blog.js