/* SavePost.js This script save the post to the given location Require paramters: - filepath - title - tags - content (HTML, not markdown) */ //Require libraries includes("config.js") function main(){ //Get a safe title from removing all special characters var safeTitle = title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); var saveFilename = getBlogPostStore() + "private/" + Date.now() + "-" + safeTitle + ".json" if (filepath == "" || !filelib.fileExists(filepath)){ //This is a new file or the original file not exists. Assign a filename for it filepath = saveFilename; } //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); //Send back the filepath for save confirmation sendResp(filepath); } main();