/share/spice/search_code/search_code.js

http://github.com/duckduckgo/zeroclickinfo-spice · JavaScript · 82 lines · 72 code · 10 blank · 0 comment · 13 complexity · c211c0786138e68dd78dba0a9de40897 MD5 · raw file

  1. (function(env) {
  2. "use strict";
  3. function formatTitle(result) {
  4. var formatted_title = result.name;
  5. if (result.displayname && result.displayname !== '') {
  6. formatted_title += ' (' + result.displayname + ')';
  7. }
  8. return formatted_title;
  9. }
  10. function formatSubtitle(result) {
  11. var formatted_subtitle = null;
  12. if (result.namespace && result.namespace !== '') {
  13. formatted_subtitle = result.namespace;
  14. }
  15. return formatted_subtitle;
  16. }
  17. env.ddg_spice_search_code = function(api_result) {
  18. if(!api_result) {
  19. return Spice.failed('search_code');
  20. }
  21. var query = api_result.query;
  22. var data = api_result.results;
  23. if(!data.length || data.length === 0) {
  24. return Spice.failed('search_code');
  25. }
  26. var result;
  27. for (var i = 0; i < data.length; i++) {
  28. var checkRelevancy = [data[i].name, data[i].displayname, data[i].namespace].join(" ");
  29. if (DDG.isRelevant(checkRelevancy, [], 2)) {
  30. result = data[i];
  31. break;
  32. }
  33. }
  34. if (!result) {
  35. return Spice.failed('search_code');
  36. }
  37. result.title = formatTitle(result);
  38. result.subtitle = formatSubtitle(result);
  39. Spice.add({
  40. id: 'search_code',
  41. name: 'Answer',
  42. data: result,
  43. meta: {
  44. sourceUrl: 'https://searchcode.com/?q=' + query,
  45. sourceName: 'searchcode',
  46. },
  47. normalize: function(item) {
  48. var subtitleArray = [];
  49. if (item.subtitle) {
  50. subtitleArray.push(item.subtitle);
  51. }
  52. if (item.url && item.url !== '') {
  53. subtitleArray.push({
  54. href: item.url,
  55. text: "Reference"
  56. });
  57. }
  58. return {
  59. title: item.title,
  60. subtitle: subtitleArray
  61. };
  62. },
  63. templates: {
  64. group: 'text',
  65. options: {
  66. content: Spice.search_code.content,
  67. moreAt: true
  68. }
  69. }
  70. });
  71. }
  72. }(this));