summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorWu Cheng-Han2016-11-26 23:04:29 +0800
committerWu Cheng-Han2016-11-26 23:04:29 +0800
commit71a356552fff1985b33156ecebda98782d283fc5 (patch)
tree36e4e3b2f161c2afa640d9404e44f86e1cc836c9 /lib
parent8c10c393cec641753b33de101bdc3b2f1604f3ad (diff)
Update to auto generate meta description based on content in publish note and slide
Diffstat (limited to 'lib')
-rw-r--r--lib/models/note.js3
-rwxr-xr-xlib/response.js21
2 files changed, 17 insertions, 7 deletions
diff --git a/lib/models/note.js b/lib/models/note.js
index 05ed04c0..5727046c 100644
--- a/lib/models/note.js
+++ b/lib/models/note.js
@@ -260,6 +260,9 @@ module.exports = function (sequelize, DataTypes) {
if (!title) title = "Untitled";
return title;
},
+ generateDescription: function (markdown) {
+ return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ');
+ },
decodeTitle: function (title) {
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
diff --git a/lib/response.js b/lib/response.js
index b2d13988..0004f824 100755
--- a/lib/response.js
+++ b/lib/response.js
@@ -178,8 +178,11 @@ function showPublishNote(req, res, next) {
}
var body = LZString.decompressFromBase64(note.content);
var meta = null;
+ var markdown = null;
try {
- meta = models.Note.parseMeta(metaMarked(body).meta);
+ var obj = metaMarked(body);
+ markdown = obj.markdown;
+ meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
}
@@ -191,7 +194,7 @@ function showPublishNote(req, res, next) {
var origin = config.serverurl;
var data = {
title: title,
- description: meta.description,
+ description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
@@ -249,8 +252,11 @@ function actionDownload(req, res, note) {
function actionInfo(req, res, note) {
var body = LZString.decompressFromBase64(note.content);
var meta = null;
+ var markdown = null;
try {
- meta = models.Note.parseMeta(metaMarked(body).meta);
+ var obj = metaMarked(body);
+ markdown = obj.markdown;
+ meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
}
@@ -260,7 +266,7 @@ function actionInfo(req, res, note) {
var title = models.Note.decodeTitle(note.title);
var data = {
title: meta.title || title,
- description: meta.description,
+ description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime
@@ -560,9 +566,10 @@ function showPublishSlide(req, res, next) {
}
var body = LZString.decompressFromBase64(note.content);
var meta = null;
+ var markdown = null;
try {
var obj = metaMarked(body);
- body = obj.markdown;
+ markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
@@ -575,12 +582,12 @@ function showPublishSlide(req, res, next) {
var origin = config.serverurl;
var data = {
title: title,
- description: meta.description,
+ description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
url: origin,
- body: body,
+ body: markdown,
meta: JSON.stringify(obj.meta || {}),
useCDN: config.usecdn,
owner: note.owner ? note.owner.id : null,