Skip to content
This repository was archived by the owner on Mar 31, 2021. It is now read-only.
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ A jQuery plugin for rendering a readme.md in Github file as html.
<script>
$(function () {
var options = {
//required

owner: 'zmckinnon',
repo: 'jquery-gh-readme'
repo: 'jquery-gh-readme',

//optional
branch: 'master',
file: '/README.md'
};
$('#readme').readme(options);
});
Expand Down
51 changes: 5 additions & 46 deletions src/jquery.gh-readme.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
(function ($) {

var b64array = "ABCDEFGHIJKLMNOP" +
"QRSTUVWXYZabcdef" +
"ghijklmnopqrstuv" +
"wxyz0123456789+/" +
"=";

function decodeBase64(data) {
// from http://decodebase64.com/
var output = "";
var hex = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;

data = data.replace(/[^A-Za-z0-9\+\/\=]/g, "");

do {
enc1 = b64array.indexOf(data.charAt(i++));
enc2 = b64array.indexOf(data.charAt(i++));
enc3 = b64array.indexOf(data.charAt(i++));
enc4 = b64array.indexOf(data.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";

} while (i < data.length);

return output;
}

function convertMarkdown(markdownToConvert) {
return marked(markdownToConvert);
}
Expand All @@ -52,13 +9,15 @@
var self = this;
var owner = options.owner;
var repo = options.repo;
$.get('https://api.github.com/repos/' + owner + '/' + repo + '/readme', function (response) {
var markdownToConvert = decodeBase64(response.content);
var branch = options.branch||'master';
var file = options.file||'/README.md';
$.get('https://raw.githubusercontent.com/' + owner + '/' + repo + '/' + branch + '' + file + '', function (response) {
var markdownToConvert = response.content;
var html = convertMarkdown(markdownToConvert);
$(self).html(html);
deferred.resolve();
});
return deferred.promise();
};

})(jQuery);
})(jQuery);
2 changes: 1 addition & 1 deletion tests/specs/jquery.gh-readme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('jQuery.gh-readme', function () {
owner: 'zmckinnon',
repo: 'jquery-gh-readme'
};
jQuery.get = function (url, callback) { callback({ content: 'IyMgVGVzdCAxMjMgIyM=' }); };
jQuery.get = function (url, callback) { callback({ content: '## Test 123' }); };
$('#readme').readme(options).then(done);
});

Expand Down