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.
 
 
 

100 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. summary = title.substring(eol+1);
  63. title = title.substring(0, eol);
  64. }
  65. return {
  66. title: title,
  67. summary: summary
  68. };
  69. };
  70. var post = getPostMeta(content);
  71. post.id = id;
  72. post.token = editToken;
  73. post.created = created ? new Date(created) : new Date();
  74. post.client = "Chrome";
  75. return post;
  76. },
  77. getTitleStrict: function(content) {
  78. var eol = content.indexOf("\n");
  79. var title = "";
  80. var newContent = content;
  81. if (content.indexOf("# ") === 0) {
  82. // Title is in the format:
  83. // # Some title
  84. if (eol !== -1) {
  85. // First line should start with # and end with \n
  86. newContent = content.substring(eol).leftTrim();
  87. title = content.substring("# ".length, eol);
  88. }
  89. }
  90. return {
  91. title: title,
  92. content: newContent
  93. };
  94. },
  95. };