savePost.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. SavePost.js
  3. This script save the post to the given location
  4. Require paramters:
  5. - filepath
  6. - title
  7. - tags
  8. - content (HTML, not markdown)
  9. */
  10. //Require libraries
  11. includes("config.js")
  12. function main(){
  13. //Get a safe title from removing all special characters
  14. var safeTitle = title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
  15. var saveFilename = getBlogPostStore() + "private/" + Date.now() + "-" + safeTitle + ".json"
  16. if (filepath == "" || !filelib.fileExists(filepath)){
  17. //This is a new file or the original file not exists. Assign a filename for it
  18. filepath = saveFilename;
  19. }
  20. //Generate tags array
  21. var tagArray = [];
  22. tags = tags.split(",");
  23. for(var i = 0; i < tags.length; i++){
  24. var thisTag = tags[i].trim();
  25. if (thisTag != ""){
  26. tagArray.push(thisTag);
  27. }
  28. }
  29. //Generate the blog post storage object
  30. var blogObject = {
  31. Title: title,
  32. Tags: tagArray,
  33. Content: content,
  34. }
  35. //Write to file
  36. var postObjectEncoded = JSON.stringify(blogObject);
  37. filelib.writeFile(filepath, postObjectEncoded);
  38. //Send back the filepath for save confirmation
  39. sendResp(filepath);
  40. }
  41. main();