2015-06-09 01:04:34 +00:00
|
|
|
var H = {
|
|
|
|
save: function($el, key) {
|
|
|
|
this.set(key, $el.value);
|
|
|
|
},
|
|
|
|
load: function($el, key) {
|
|
|
|
$el.value = this.get(key, "");
|
|
|
|
},
|
|
|
|
set: function(key, value) {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
},
|
|
|
|
get: function(key, defaultValue) {
|
|
|
|
var val = localStorage.getItem(key);
|
|
|
|
if (val == null) {
|
|
|
|
val = defaultValue;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
},
|
2015-09-09 19:00:21 +00:00
|
|
|
createPost: function(id, editToken, content, created) {
|
2015-09-09 15:42:05 +00:00
|
|
|
var summaryLen = 200;
|
|
|
|
var titleLen = 80;
|
|
|
|
var getPostMeta = function(content) {
|
|
|
|
var eol = content.indexOf("\n");
|
|
|
|
if (content.indexOf("# ") === 0) {
|
|
|
|
// Title is in the format:
|
|
|
|
//
|
|
|
|
// # Some title
|
|
|
|
var summary = content.substring(eol).trim();
|
|
|
|
if (summary.length > summaryLen) {
|
|
|
|
summary = summary.substring(0, summaryLen) + "...";
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: content.substring("# ".length, eol),
|
|
|
|
summary: summary,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var blankLine = content.indexOf("\n\n");
|
|
|
|
if (blankLine !== -1 && blankLine <= eol && blankLine <= titleLen) {
|
|
|
|
// Title is in the format:
|
|
|
|
//
|
|
|
|
// Some title
|
|
|
|
//
|
|
|
|
// The body starts after that blank line above it.
|
|
|
|
var summary = content.substring(blankLine).trim();
|
|
|
|
if (summary.length > summaryLen) {
|
|
|
|
summary = summary.substring(0, summaryLen) + "...";
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: content.substring(0, blankLine),
|
|
|
|
summary: summary,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var title = content.trim();
|
|
|
|
var summary = "";
|
|
|
|
if (title.length > titleLen) {
|
|
|
|
// Content can't fit in the title, so figure out the summary
|
|
|
|
summary = title;
|
|
|
|
title = "";
|
|
|
|
if (summary.length > summaryLen) {
|
|
|
|
summary = summary.substring(0, summaryLen) + "...";
|
|
|
|
}
|
|
|
|
} else if (eol > 0) {
|
|
|
|
title = title.substring(0, eol);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: title,
|
|
|
|
summary: summary
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var post = getPostMeta(content);
|
|
|
|
post.id = id;
|
|
|
|
post.token = editToken;
|
2015-09-09 19:00:21 +00:00
|
|
|
post.created = created ? new Date(created) : new Date();
|
2015-09-09 19:19:30 +00:00
|
|
|
post.client = "Chrome";
|
2015-09-09 15:42:05 +00:00
|
|
|
|
|
|
|
return post;
|
|
|
|
},
|
2016-06-15 03:44:56 +00:00
|
|
|
getTitleStrict: function(content) {
|
|
|
|
var eol = content.indexOf("\n");
|
|
|
|
var title = "";
|
|
|
|
var newContent = content;
|
|
|
|
if (content.indexOf("# ") === 0) {
|
|
|
|
// Title is in the format:
|
|
|
|
// # Some title
|
|
|
|
if (eol !== -1) {
|
|
|
|
// First line should start with # and end with \n
|
|
|
|
newContent = content.substring(eol).leftTrim();
|
|
|
|
title = content.substring("# ".length, eol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: title,
|
|
|
|
content: newContent
|
|
|
|
};
|
|
|
|
},
|
2015-06-09 01:04:34 +00:00
|
|
|
};
|