Skip to content

Commit 5ac9c20

Browse files
sake92jvican
authored andcommitted
Added contributors widget (#1098)
Fixes #44 Following [this answer](https://stackoverflow.com/a/19200303/4496364) I managed to get this via Github API: ![](https://upload.cc/i1/2018/07/04/n4zkhJ.png) I roughly followed Mozilla's [docs page](https://developer.mozilla.org/en-US/docs/Web/HTML) design and their contributors listing. Some users don't have the "author" field. Probably deleted their profile? So, the fallback is `commit.commit.committer.name` which is, I suppose, always present because of Git... Also, if the author isn't present, image icon is generated with https://github.com/identicons. Example here is Ghildiyal user. Currently, I added this to tour only. IDK did you guys intend to put these on each page? If so, there could be some problems with getting page URL from Github. IDK if all of them follow same pattern as "tours"..
1 parent db7efad commit 5ac9c20

11 files changed

+124
-2
lines changed

_includes/contributors-list.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="content-contributors">
2+
<h3>Contributors to this page:</h3>
3+
<div id="contributors" class="contributors-container"></div>
4+
</div>

_includes/inner-page-main-content.html

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<div class="content-primary">
44
<div class="inner-box">
55
{{content}}
6+
7+
{% include contributors-list.html %}
68
</div>
79
</div>
810

_layouts/cheatsheet.html

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
{% endfor %}
3131
</ul>
3232
{% endif %}
33+
34+
{% include contributors-list.html %}
3335
</div>
3436
</div>
3537
</div>

_layouts/glossary.html

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<div class="content-primary documentation glossary">
99
<div class="inner-box">
1010
{{content}}
11+
12+
{% include contributors-list.html %}
1113
</div>
1214
</div>
1315

_layouts/multipage-overview.html

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<div class="content-primary documentation">
1010
<div class="inner-box">
1111
{{content}}
12+
13+
{% include contributors-list.html %}
1214
</div>
1315
</div>
1416

_layouts/singlepage-overview.html

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<div class="content-primary documentation">
99
<div class="inner-box">
1010
{{content}}
11+
12+
{% include contributors-list.html %}
1113
</div>
1214
</div>
1315

_layouts/style-guide.html

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<div class="content-primary documentation style-guide">
1010
<div class="inner-box">
1111
{{content}}
12+
13+
{% include contributors-list.html %}
1214
</div>
1315
</div>
1416

_layouts/tour.html

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<a href="{{page.next-page}}.html"><strong>next</strong> &rarr;</a>
2121
{% endif %}
2222
</div>
23+
24+
{% include contributors-list.html %}
2325
</div>
2426
</div>
2527

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.content-contributors {
2+
.contributors-container {
3+
display: flex;
4+
flex-wrap: wrap;
5+
align-items: center;
6+
div {
7+
margin: 5px;
8+
a {
9+
vertical-align: middle;
10+
padding: 3px;
11+
text-decoration: none;
12+
}
13+
img {
14+
vertical-align: middle;
15+
width: 35px;
16+
height: 35px;
17+
margin-bottom: 0;
18+
border-radius: 7px;
19+
}
20+
}
21+
}
22+
}

resources/css/style.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
@import 'layout/books';
5555
@import 'layout/training-events';
5656
@import 'layout/blog';
57-
@import 'layout/download'; // COMPONENTS
57+
@import 'layout/download';
58+
@import 'layout/content-contributors'; // COMPONENTS
5859
//------------------------------------------------
5960
//------------------------------------------------
6061
@import 'components/buttons';

resources/js/functions.js

+82-1
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,85 @@ $(document).ready(function(){
521521
$("html, body").animate({ scrollTop: 0 }, 600);
522522
return false;
523523
});
524-
});
524+
});
525+
526+
//Contributors widget
527+
// see https://stackoverflow.com/a/19200303/4496364
528+
$(document).ready(function () {
529+
let githubApiUrl = 'https://api.github.com/repos/scala/docs.scala-lang/commits';
530+
let identiconsUrl = 'https://github.com/identicons';
531+
/* - we need to transform "/tour/basics.html" to "_ba/tour/basics.md"
532+
* - some files aren't prefixed with underscore, see rootFiles
533+
* - some files are placed in _overviews but rendered to its folder, see overviewsFolders
534+
*/
535+
536+
let rootFiles = ['getting-started', 'learn', 'glossary'];
537+
let overviewsFolders = ['FAQ', 'cheatsheets', 'collections', 'compiler-options',
538+
'core', 'jdk-compatibility', 'macros', 'parallel-collections',
539+
'plugins', 'quasiquotes', 'reflection',
540+
'repl', 'scaladoc', 'tutorials'
541+
];
542+
543+
let thisPageUrl = window.location.pathname;
544+
// chop off beginning slash and ending .html
545+
thisPageUrl = thisPageUrl.substring(1, thisPageUrl.lastIndexOf('.'));
546+
let isRootFile = rootFiles.some(rf => thisPageUrl.startsWith(rf));
547+
let isInOverviewsFolder = overviewsFolders.some(of => thisPageUrl.startsWith(of));
548+
if(isRootFile) {
549+
thisPageUrl = thisPageUrl + '.md';
550+
} else if(isInOverviewsFolder) {
551+
thisPageUrl = '_overviews/'+ thisPageUrl + '.md';
552+
} else {
553+
thisPageUrl = '_' + thisPageUrl + '.md';
554+
}
555+
556+
let url = githubApiUrl + '?path=' + thisPageUrl;
557+
$.get(url, function (data, status) {
558+
if(!data || data.length < 1) {
559+
$('.content-contributors').html(''); // clear content
560+
return false; // break
561+
}
562+
let contributorsUnique = [];
563+
data.forEach(commit => {
564+
// add if not already in array
565+
let addedToList = contributorsUnique.find(c => {
566+
let matches = c.authorName == commit.commit.author.name;
567+
if (!matches && commit.author) {
568+
matches = c.authorName == commit.author.login;
569+
}
570+
return matches;
571+
});
572+
573+
if (!addedToList) {
574+
// first set fallback properties
575+
let authorName = commit.commit.author.name;
576+
let authorLink = '';
577+
let authorImageLink = identiconsUrl + '/' + commit.commit.author.name + '.png';
578+
// if author present, fill these preferably
579+
if (commit.author) {
580+
authorName = commit.author.login;
581+
authorLink = commit.author.html_url;
582+
authorImageLink = commit.author.avatar_url;
583+
}
584+
contributorsUnique.push({
585+
'authorName': authorName,
586+
'authorLink': authorLink,
587+
'authorImageLink': authorImageLink
588+
});
589+
}
590+
});
591+
592+
let contributorsHtml = '';
593+
contributorsUnique.forEach(contributor => {
594+
let contributorHtml = '<div>';
595+
contributorHtml += '<img src="' + contributor.authorImageLink + '">';
596+
if (contributor.authorLink)
597+
contributorHtml += '<a href="' + contributor.authorLink + '">' + contributor.authorName + '</a>';
598+
else
599+
contributorHtml += '<a>' + contributor.authorName + '</a>';
600+
contributorHtml += '</div>';
601+
contributorsHtml += contributorHtml;
602+
});
603+
$('#contributors').html(contributorsHtml);
604+
});
605+
});

0 commit comments

Comments
 (0)