/* Index Manager This script handle adding and removing blog post from the index list. Use with include only. Call directly via AGI return nothing */ includes("helper.js"); //Get the current post in the system function getIndexList(webroot){ webroot = filepathClean(webroot); var indexFile = webroot + "/blog/posts.json" if (!filelib.fileExists(indexFile)){ //Create it filelib.writeFile(indexFile, JSON.stringify([])); //Return an empty list return []; }else{ //Get the index file var content = filelib.readFile(indexFile); return JSON.parse(content); } } //Write new index list into the list function writeIndexList(webroot, newListObject){ webroot = filepathClean(webroot); var indexFile = webroot + "/blog/posts.json" return filelib.writeFile(indexFile, JSON.stringify(newListObject)); } //This inject a new title into the post list function injectTitleIntoPostList(webroot, postObjectName){ var currentIndexList = getIndexList(webroot); var postObjectPath = filepathClean(getBlogPostStore()) + "/posts/" + postObjectName + ".json"; var summary = extractPostInfo(postObjectPath); var newIndexList = []; for (var i = 0; i < currentIndexList.length; i++){ if (currentIndexList[i].name != postObjectName){ //Only push index list that is not this one newIndexList.push(currentIndexList[i]); } } currentIndexList = newIndexList; //Push to the list currentIndexList.push({ name: postObjectName, summary: summary }); writeIndexList(webroot,currentIndexList); } //This remove a post from the post list base on title function removeTitleFromPostList(webroot, postObjectName){ var currentIndexList = getIndexList(webroot); var postExists = false; for (var i = 0; i < currentIndexList.length; i++){ if (currentIndexList[i].name == postObjectName){ postExists = true; } } if (postExists){ var newarray = []; for (var i = 0; i < currentIndexList.length; i++){ var thisIndex = currentIndexList[i]; if (thisIndex.name != postObjectName){ newarray.push(thisIndex); } } writeIndexList(webroot, newarray); } }