12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /*
- 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);
- }
- }
|