Last active
May 20, 2017 10:10
-
-
Save NexZhu/4f369e0c3fe3f4dd0710 to your computer and use it in GitHub Desktop.
A bookmarklet as a temporal solution for advance searching in Astral
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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); | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minified code for bookmarklet: