Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short message for GitHub Pages related to a single page or to the site #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,59 @@ Reference 'github.commits.widget.js' and containter div and place such script.
</script>
```

where, user is your github account, repo is name of repository and branch is the name of branch you want to track.
This sample shows a short *last modification* message related to the site (e.g., `Last modification just now`):

```yaml
...
baseurl: "/my-repo"
branch: "gh-pages"
...
author:
name: itsme
...
```

```html
Last modification<span id="last-site-mod"> unknown</span>
```

```html
$(function() {
$('#last-site-mod').githubInfoWidget(
{ user: '{{ site.author.name }}', repo: '{{ site.baseurl }}', branch: '{{ site.branch }}', nouser: true, nomsg: true, abstime: false, simple: 2, last: 1, limitMessageTo: 500 });
});
</script>
```

This sample shows a short *last modification* message related to a specific page (e.g., `Last modification: 2016-10-03, about 2 hours ago`):

```html
Last modification: <span id="last-page-mod">unknown</span>
```

```html
<script>
$(function() {
$('#last-page-mod').githubInfoWidget(
{ user: '{{ site.author.name }}', repo: '{{ site.baseurl }}', branch: '{{ site.branch }}', path: '{{ page.path }}', nouser: true, nomsg: true, abstime: true, simple: 1, last: 1, limitMessageTo: 500 });
});
</script>
```

Configuration
-------------

* user is your github account
* repo is name of repository; can be with or without initial `/`
* branch is the name of branch you want to track
* nouser (true/false/unset): if set to true, does not show user
* noavatar (true/false/unset): if set to true, does not show avatar
* nomsg (true/false/unset): if set to true, does not show message
* abstime (true/false/unset): if set to true, shows absolute time
* noreltime (true/false/unset): if set to true, does not show relative time
* simple (unset, 1, 2): if set to 1, shows a simple message without list; if set to 2, also show the number of commits in the tooltip


You might limit number commits shown in widget by providing with 'last' parameter:

```html
Expand Down
107 changes: 88 additions & 19 deletions js/github.commits.widget.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/*

Modified by ircama, 2016; added the following options:
- nouser (true/false/unset): if set to true, does not show user
- noavatar (true/false/unset): if set to true, does not show avatar
- nomsg (true/false/unset): if set to true, does not show message
- abstime (true/false/unset): if set to true, shows absolute time
- noreltime (true/false/unset): if set to true, does not show relative time
- simple (unset, 1, 2): if set to 1, shows a simple message without list; if set to 2, also show the number of commits in the tooltip

https://github.com/alexanderbeletsky/github-commits-widget

# Legal Info (MIT License)
Expand Down Expand Up @@ -35,11 +43,18 @@ THE SOFTWARE.

widget.prototype = (function() {

function getCommits(user, repo, branch, callback) {
function getCommits(user, repo, branch, path, simple, element, callback) {
$.ajax({
url: "https://api.github.com/repos/" + user + "/" + repo + "/commits?sha=" + branch,
url: "https://api.github.com/repos/" + user + repo + "/commits?sha=" + branch + (path === undefined ? '' : "&path=" + path),
dataType: 'jsonp',
success: callback
success: callback,
error: function(){
if (simple)
{
element.empty();
element.append((simple < 2 ? ' ' : ': ') + '<span class="noprint">check ' + '<a class="github-commit" href="' + 'https://github.com/' + user + repo + '/commits/' + branch + (path === undefined ? '' : '/' + path) + '" target="_blank">here</a></span>');
}
}
});
}

Expand All @@ -51,43 +66,84 @@ THE SOFTWARE.
var callback = widget.callback;
var element = widget.element;
var user = widget.options.user;
var nouser = widget.options.nouser === undefined ? 0 : nouser = widget.options.nouser;
var noavatar = widget.options.noavatar === undefined ? 0 : noavatar = widget.options.noavatar;
var nomsg = widget.options.nomsg === undefined ? 0 : widget.options.nomsg;
var abstime = widget.options.abstime === undefined ? 0 : widget.options.abstime;
var noreltime = widget.options.noreltime === undefined ? 0 : widget.options.noreltime;
var repo = widget.options.repo;
if ((repo.length<1) || (repo.substring(0, 1) != '/'))
repo = '/' + repo;
var branch = widget.options.branch;
var path = widget.options.path;
var avatarSize = widget.options.avatarSize || 20;
var simple = widget.options.simple === undefined ? 0 : widget.options.simple;
var last = widget.options.last === undefined ? 0 : widget.options.last;
var limitMessage = widget.options.limitMessageTo === undefined ? 0 : widget.options.limitMessageTo;

getCommits(user, repo, branch, function (data) {
getCommits(user, repo, branch, path, simple, element, function (data) {
var commits = data.data;
var totalCommits = (last < commits.length ? last : commits.length);

element.empty();

var list = $('<ul class="github-commits-list">').appendTo(element);
if (totalCommits === undefined)
{
element.append((simple < 2 ? ' ' : ': ') + '<span class="noprint">check ' + '<a class="github-commit" href="' + 'https://github.com/' + user + repo + '/commits/' + branch + (path === undefined ? '' : '/' + path) + '" target="_blank">here</a></span>');
return;
}

if (!simple)
var list = $('<ul class="github-commits-list">').appendTo(element);

for (var c = 0; c < totalCommits; c++) {
var cur = commits[c];
var li = $("<li>");

var e_user = $('<span class="github-user">');
//add avatar & github link if possible
if (cur.author !== null) {
e_user.append(avatar(cur.author.gravatar_id, avatarSize));
e_user.append(author(cur.author.login));
if (simple)
{
element.append(' ');

var li = $('<a class="github-commit">')
.attr("title", ((simple < 2) ? 'Last commits: ' + commits.length + '. Message in last commit:\n' : '') + cur.commit.message)
.attr("href", 'https://github.com/' + user + repo + '/commits/' + branch + (path === undefined ? '' : '/' + path))
.attr("target","_blank")
.appendTo(element);
}
else //otherwise just list the name
else
{
e_user.append(cur.commit.committer.name);
var li = $("<li>");
}

li.append(e_user);
if (!nouser)
{
var e_user = $('<span class="github-user">');
//add avatar & github link if possible
if (cur.author !== null) {
if (!noavatar)
e_user.append(avatar(cur.author.gravatar_id, avatarSize));
e_user.append(author(cur.author.login));
}
else //otherwise just list the name
{
e_user.append(cur.commit.committer.name);
}
li.append(e_user);
}

//add commit message
li.append(message(cur.commit.message, cur.sha));
li.append(when(cur.commit.committer.date));
if (!nomsg)
li.append(message(cur.commit.message, cur.sha));

list.append(li);
}
if (abstime)
{
li.append('<span class=github-commit-date>' + timewhen(cur.commit.committer.date) + '</span>');
}
if (!noreltime)
li.append('<span class="github-commit-reltime noprint">' + (abstime ? ', ' : '') + when(cur.commit.committer.date) + '</span>');

if (!simple)
list.append(li);
}

callback(element);

Expand All @@ -112,7 +168,7 @@ THE SOFTWARE.

var link = $('<a class="github-commit"></a>')
.attr("title", originalCommitMessage)
.attr("href", 'https://github.com/' + user + '/' + repo + '/commit/' + sha)
.attr("href", 'https://github.com/' + user + repo + '/commit/' + sha)
.text(commitMessage);

return link;
Expand All @@ -139,8 +195,21 @@ THE SOFTWARE.
} else if (differenceInDays == 1) {
return 'yesterday';
}
if (differenceInDays > 365)
{
return 'about ' + ( Math.round( differenceInDays / 365 * 10) / 10 ) + ' year(s) ago';
}
if (differenceInDays > 60)
{
return 'about ' + ( Math.round( differenceInDays / 30 * 10) / 10 ) + ' month(s) ago';
}
return differenceInDays + ' days ago';
}

function timewhen(commitDate) {
var localedate = new Date(commitDate).toISOString().slice(0, 10);
return localedate;
}
});
}

Expand Down