A Chrome extension for Write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
2.4 KiB

  1. var H = {
  2. save: function($el, key) {
  3. this.set(key, $el.value);
  4. },
  5. load: function($el, key) {
  6. $el.value = this.get(key, "");
  7. },
  8. set: function(key, value) {
  9. localStorage.setItem(key, value);
  10. },
  11. get: function(key, defaultValue) {
  12. var val = localStorage.getItem(key);
  13. if (val == null) {
  14. val = defaultValue;
  15. }
  16. return val;
  17. },
  18. createPost: function(id, editToken, content, created) {
  19. var summaryLen = 200;
  20. var titleLen = 80;
  21. var getPostMeta = function(content) {
  22. var eol = content.indexOf("\n");
  23. if (content.indexOf("# ") === 0) {
  24. // Title is in the format:
  25. //
  26. // # Some title
  27. var summary = content.substring(eol).trim();
  28. if (summary.length > summaryLen) {
  29. summary = summary.substring(0, summaryLen) + "...";
  30. }
  31. return {
  32. title: content.substring("# ".length, eol),
  33. summary: summary,
  34. };
  35. }
  36. var blankLine = content.indexOf("\n\n");
  37. if (blankLine !== -1 && blankLine <= eol && blankLine <= titleLen) {
  38. // Title is in the format:
  39. //
  40. // Some title
  41. //
  42. // The body starts after that blank line above it.
  43. var summary = content.substring(blankLine).trim();
  44. if (summary.length > summaryLen) {
  45. summary = summary.substring(0, summaryLen) + "...";
  46. }
  47. return {
  48. title: content.substring(0, blankLine),
  49. summary: summary,
  50. };
  51. }
  52. var title = content.trim();
  53. var summary = "";
  54. if (title.length > titleLen) {
  55. // Content can't fit in the title, so figure out the summary
  56. summary = title;
  57. title = "";
  58. if (summary.length > summaryLen) {
  59. summary = summary.substring(0, summaryLen) + "...";
  60. }
  61. } else if (eol > 0) {
  62. title = title.substring(0, eol);
  63. }
  64. return {
  65. title: title,
  66. summary: summary
  67. };
  68. };
  69. var post = getPostMeta(content);
  70. post.id = id;
  71. post.token = editToken;
  72. post.created = created ? new Date(created) : new Date();
  73. post.client = "Chrome";
  74. return post;
  75. },
  76. getTitleStrict: function(content) {
  77. var eol = content.indexOf("\n");
  78. var title = "";
  79. var newContent = content;
  80. if (content.indexOf("# ") === 0) {
  81. // Title is in the format:
  82. // # Some title
  83. if (eol !== -1) {
  84. // First line should start with # and end with \n
  85. newContent = content.substring(eol).leftTrim();
  86. title = content.substring("# ".length, eol);
  87. }
  88. }
  89. return {
  90. title: title,
  91. content: newContent
  92. };
  93. },
  94. };