Skip to content

Instantly share code, notes, and snippets.

@NexZhu
Last active May 20, 2017 10:10
Show Gist options
  • Select an option

  • Save NexZhu/4f369e0c3fe3f4dd0710 to your computer and use it in GitHub Desktop.

Select an option

Save NexZhu/4f369e0c3fe3f4dd0710 to your computer and use it in GitHub Desktop.
A bookmarklet as a temporal solution for advance searching in Astral
(function() {
var keywords = prompt().toLowerCase().split(/\s*,\s*/);
var kwWords = keywords.filter(function(keyword) {
return !keyword.startsWith('#')
});
var kwTags = _.difference(keywords, kwWords).map(function(tag) {
return tag.substr(1);
});
// build repos
var repos = $('.repo').map(function() {
var $el = $(this);
var name = $el.find('.repo-name').text();
var tags = $el.find('.repo-tags li').map(function() {
return $(this).text().toLowerCase();
}).get();
var description = $el.find('.repo-description').text();
var link = $el.find('.link a').attr('href');
return {
name: name,
tags: tags,
description: description,
link: link
};
}).get();
repos = kwWords.reduce(function(repos, word) {
return repos.filter(function(repo) {
return repo.name.toLowerCase().includes(word) || repo.description.toLowerCase().includes(word);
});
}, repos);
// build tags from repos
var tags = [];
_.each(repos, function(repo) {
_.each(repo.tags, function(name) {
tag = _.findWhere(tags, {
name: name
});
if (tag === undefined) {
tag = {
name: name,
repos: []
};
tags.push(tag);
}
tag.repos.push(repo);
});
});
var result = kwTags.reduce(function(repos, tag) {
var matches = _.findWhere(tags, {
name: tag
});
return _.intersection(repos, matches ? matches.repos : []);
}, repos).map(function(repo) {
return '<a href="' + repo.link + '">' + repo.name + '</a>';
}).join('<br>');
var w = window.open(null, 'Result', "width=400,height=400,location=no,menubar=no,status=no,toolbar=no");
$(w.document.body).html(result);
})();
@NexZhu
Copy link
Author

NexZhu commented Feb 11, 2016

Minified code for bookmarklet:

javascript:(function(){var a=prompt().toLowerCase().split(/\s_,\s_/),b=a.filter(function(a){return!a.startsWith("#")}),c=.difference(a,b).map(function(a){return a.substr(1)}),d=$(".repo").map(function(){var a=$(this),b=a.find(".repo-name").text(),c=a.find(".repo-tags li").map(function(){return $(this).text().toLowerCase()}).get(),d=a.find(".repo-description").text(),e=a.find(".link a").attr("href");return{name:b,tags:c,description:d,link:e}}).get();d=b.reduce(function(a,b){return a.filter(function(a){return a.name.toLowerCase().includes(b)||a.description.toLowerCase().includes(b)})},d);var e=[];.each(d,function(a){.each(a.tags,function(b){tag=.findWhere(e,{name:b}),void 0===tag&&(tag={name:b,repos:[]},e.push(tag)),tag.repos.push(a)})});var f=c.reduce(function(a,b){var c=_.findWhere(e,{name:b});return _.intersection(a,c?c.repos:[])},d).map(function(a){return''+a.name+""}).join("
"),g=window.open(null,"Result","width=400,height=400,location=no,menubar=no,status=no,toolbar=no");$(g.document.body).html(f)})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment