Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

search-service.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. angular.module('codexApp')
  2. .service('SearchService', [ '$rootScope', '$http', 'ThumbnailService', '$state', 'FileService', function($rootScope, $http, ThumbnailService, $state, FileService) {
  3. var fs = require('fs');
  4. var fulltextsearchlight = require('full-text-search-light');
  5. var search = new fulltextsearchlight();
  6. var appDataPath = FileService.getAppDataPath();
  7. this.init = function() {
  8. // Load db
  9. fulltextsearchlight.load(appDataPath + '/search.json', function(error, search_loaded){
  10. if(error){
  11. console.log("> SEARCH DB NOT FOUND!")
  12. console.log(error)
  13. search.save(appDataPath + '/search.json', function(error){
  14. console.log("> CREATING SEARCH DB")
  15. buildSearchDB();
  16. console.log("> NEW SEARCH DB CREATED")
  17. });
  18. }
  19. else {
  20. search = search_loaded
  21. console.log("> SEARCH DB LOADED");
  22. }
  23. });
  24. }
  25. this.search = function(search_text) {
  26. var results = search.search(search_text)
  27. console.log("> FOUND " + results.length + " RESULTS");
  28. return results
  29. }
  30. var buildSearchDB = function() {
  31. var parsed_files = []
  32. var all_files = FileService.getAllFiles();
  33. for (var i = 0; i < all_files.length; i++) {
  34. file = all_files[i]
  35. var file_data = {
  36. path: file.path,
  37. title: file.title,
  38. type: file.type
  39. }
  40. if(file.type == "Markdown"){
  41. var raw_data = fs.readFileSync(file.path, 'utf8');
  42. var data = new Buffer(raw_data).toString('utf8')
  43. console.log(file);
  44. file_data.content = data;
  45. file_data.thumbnail = file.thumbnail;
  46. }
  47. parsed_files.push(file_data);
  48. }
  49. for (var i = 0; i < parsed_files.length; i++) {
  50. console.log(parsed_files[i].title);
  51. search.add(parsed_files[i]);
  52. }
  53. search.save(appDataPath + '/search.json', function(error){
  54. console.log("> SEARCH DB SAVED")
  55. });
  56. }
  57. }])