From 71a356552fff1985b33156ecebda98782d283fc5 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Sat, 26 Nov 2016 23:04:29 +0800 Subject: Update to auto generate meta description based on content in publish note and slide --- lib/models/note.js | 3 +++ lib/response.js | 21 ++++++++++++++------- 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, -- cgit v1.2.3