123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /*
- SavePost.js
- This script save the post to the given location
- Require paramters:
- - filepath
- - title
- - tags
- - content (HTML, not markdown)
- - webroot (Web deploy root)
- */
- //Require libraries
- includes("config.js")
- includes("helper.js")
- function main(){
- //Get a safe title from removing all special characters
- var safeTitle = title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
- var postObjectName = Date.now() + "-" + safeTitle;
- var saveFilename = getBlogPostStore() + "posts/" + postObjectName + ".json"
- if (filepath == "" || !filelib.fileExists(filepath)){
- //This is a new file or the original file not exists. Assign a filename for it
- filepath = saveFilename;
- }else{
- //Extract the post object name from the path
- var tmp = filepath.split("/").pop();
- var fname = tmp.split(".");
- fname.pop();
- postObjectName = fname.join(".");
- console.log(postObjectName);
- }
- //Generate tags array
- var tagArray = [];
- tags = tags.split(",");
- for(var i = 0; i < tags.length; i++){
- var thisTag = tags[i].trim();
- if (thisTag != ""){
- tagArray.push(thisTag);
- }
- }
- //Generate the blog post storage object
- var blogObject = {
- Title: title,
- Tags: tagArray,
- Content: content,
- }
- //Write to file
- var postObjectEncoded = JSON.stringify(blogObject);
- filelib.writeFile(filepath, postObjectEncoded);
- //Check if this is published. IF yes, update the render as well
- var publishWebPosition = filepathClean(webroot) + "/blog/posts/" + postObjectName + ".html";
- if (filelib.fileExists(publishWebPosition)){
- includes("publish.js");
- var runAsFunc = true;
- publish(filepath, webroot)
- }
- //Send back the filepath for save confirmation
- sendResp(filepath);
- }
- main();
|