indexManager.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Index Manager
  3. This script handle adding and removing blog post from the index list.
  4. Use with include only. Call directly via AGI return nothing
  5. */
  6. includes("helper.js");
  7. //Get the current post in the system
  8. function getIndexList(webroot){
  9. webroot = filepathClean(webroot);
  10. var indexFile = webroot + "/blog/posts.json"
  11. if (!filelib.fileExists(indexFile)){
  12. //Create it
  13. filelib.writeFile(indexFile, JSON.stringify([]));
  14. //Return an empty list
  15. return [];
  16. }else{
  17. //Get the index file
  18. var content = filelib.readFile(indexFile);
  19. return JSON.parse(content);
  20. }
  21. }
  22. //Write new index list into the list
  23. function writeIndexList(webroot, newListObject){
  24. webroot = filepathClean(webroot);
  25. var indexFile = webroot + "/blog/posts.json"
  26. return filelib.writeFile(indexFile, JSON.stringify(newListObject));
  27. }
  28. //This inject a new title into the post list
  29. function injectTitleIntoPostList(webroot, postObjectName){
  30. var currentIndexList = getIndexList(webroot);
  31. var postObjectPath = filepathClean(getBlogPostStore()) + "/posts/" + postObjectName + ".json";
  32. var summary = extractPostInfo(postObjectPath);
  33. var newIndexList = [];
  34. for (var i = 0; i < currentIndexList.length; i++){
  35. if (currentIndexList[i].name != postObjectName){
  36. //Only push index list that is not this one
  37. newIndexList.push(currentIndexList[i]);
  38. }
  39. }
  40. currentIndexList = newIndexList;
  41. //Push to the list
  42. currentIndexList.push({
  43. name: postObjectName,
  44. summary: summary
  45. });
  46. writeIndexList(webroot,currentIndexList);
  47. }
  48. //This remove a post from the post list base on title
  49. function removeTitleFromPostList(webroot, postObjectName){
  50. var currentIndexList = getIndexList(webroot);
  51. var postExists = false;
  52. for (var i = 0; i < currentIndexList.length; i++){
  53. if (currentIndexList[i].name == postObjectName){
  54. postExists = true;
  55. }
  56. }
  57. if (postExists){
  58. var newarray = [];
  59. for (var i = 0; i < currentIndexList.length; i++){
  60. var thisIndex = currentIndexList[i];
  61. if (thisIndex.name != postObjectName){
  62. newarray.push(thisIndex);
  63. }
  64. }
  65. writeIndexList(webroot, newarray);
  66. }
  67. }