Update to auto generate meta description based on content in publish note and slide

This commit is contained in:
Wu Cheng-Han 2016-11-26 23:04:29 +08:00
parent 8c10c393ce
commit 71a356552f
2 changed files with 17 additions and 7 deletions

View File

@ -260,6 +260,9 @@ module.exports = function (sequelize, DataTypes) {
if (!title) title = "Untitled"; if (!title) title = "Untitled";
return title; return title;
}, },
generateDescription: function (markdown) {
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ');
},
decodeTitle: function (title) { decodeTitle: function (title) {
var decodedTitle = LZString.decompressFromBase64(title); var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle; if (decodedTitle) title = decodedTitle;

View File

@ -178,8 +178,11 @@ function showPublishNote(req, res, next) {
} }
var body = LZString.decompressFromBase64(note.content); var body = LZString.decompressFromBase64(note.content);
var meta = null; var meta = null;
var markdown = null;
try { try {
meta = models.Note.parseMeta(metaMarked(body).meta); var obj = metaMarked(body);
markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) { } catch(err) {
//na //na
} }
@ -191,7 +194,7 @@ function showPublishNote(req, res, next) {
var origin = config.serverurl; var origin = config.serverurl;
var data = { var data = {
title: title, title: title,
description: meta.description, description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount, viewcount: note.viewcount,
createtime: createtime, createtime: createtime,
updatetime: updatetime, updatetime: updatetime,
@ -249,8 +252,11 @@ function actionDownload(req, res, note) {
function actionInfo(req, res, note) { function actionInfo(req, res, note) {
var body = LZString.decompressFromBase64(note.content); var body = LZString.decompressFromBase64(note.content);
var meta = null; var meta = null;
var markdown = null;
try { try {
meta = models.Note.parseMeta(metaMarked(body).meta); var obj = metaMarked(body);
markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) { } catch(err) {
//na //na
} }
@ -260,7 +266,7 @@ function actionInfo(req, res, note) {
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
var data = { var data = {
title: meta.title || title, title: meta.title || title,
description: meta.description, description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount, viewcount: note.viewcount,
createtime: createtime, createtime: createtime,
updatetime: updatetime updatetime: updatetime
@ -560,9 +566,10 @@ function showPublishSlide(req, res, next) {
} }
var body = LZString.decompressFromBase64(note.content); var body = LZString.decompressFromBase64(note.content);
var meta = null; var meta = null;
var markdown = null;
try { try {
var obj = metaMarked(body); var obj = metaMarked(body);
body = obj.markdown; markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta); meta = models.Note.parseMeta(obj.meta);
} catch(err) { } catch(err) {
//na //na
@ -575,12 +582,12 @@ function showPublishSlide(req, res, next) {
var origin = config.serverurl; var origin = config.serverurl;
var data = { var data = {
title: title, title: title,
description: meta.description, description: meta.description || markdown ? models.Note.generateDescription(markdown) : null,
viewcount: note.viewcount, viewcount: note.viewcount,
createtime: createtime, createtime: createtime,
updatetime: updatetime, updatetime: updatetime,
url: origin, url: origin,
body: body, body: markdown,
meta: JSON.stringify(obj.meta || {}), meta: JSON.stringify(obj.meta || {}),
useCDN: config.usecdn, useCDN: config.usecdn,
owner: note.owner ? note.owner.id : null, owner: note.owner ? note.owner.id : null,