indexManager.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. function getIndexList(webroot){
  8. webroot = filepathClean(webroot);
  9. var indexFile = webroot + "/blog/posts.json"
  10. if (!filelib.fileExists(indexFile)){
  11. //Create it
  12. filelib.writeFile(indexFile, JSON.stringify([]));
  13. //Return an empty list
  14. return [];
  15. }else{
  16. //Get the index file
  17. var content = filelib.readFile(indexFile);
  18. return JSON.parse(content);
  19. }
  20. }
  21. function writeIndexList(webroot, newListObject){
  22. webroot = filepathClean(webroot);
  23. var indexFile = webroot + "/blog/posts.json"
  24. return filelib.writeFile(indexFile, JSON.stringify(newListObject));
  25. }
  26. //This inject a new title into the post list
  27. function injectTitleIntoPostList(webroot, postObjectName){
  28. var currentIndexList = getIndexList(webroot);
  29. if (currentIndexList.indexOf(postObjectName) > 0){
  30. //Already exists in the list. No need to add it in
  31. }else{
  32. //Push to the list
  33. currentIndexList.push(postObjectName);
  34. writeIndexList(webroot,currentIndexList);
  35. }
  36. }
  37. //This remove a post from the post list base on title
  38. function removeTitleFromPostList(webroot, postObjectName){
  39. var currentIndexList = getIndexList(webroot);
  40. if (currentIndexList.indexOf(postObjectName) > 0){
  41. //Already removed
  42. }else{
  43. var newarray = [];
  44. for (var i = 0; i < currentIndexList.length; i++){
  45. var thisIndex = currentIndexList[i];
  46. if (thisIndex != postObjectName){
  47. newarray.push(thisIndex);
  48. }
  49. }
  50. writeIndexList(webroot, newarray);
  51. }
  52. }
  53. //Get the current post in the system
  54. function getCurrentPostList(webroot, ){
  55. }