summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYukai Huang2017-01-07 00:05:15 +0800
committerYukai Huang2017-01-07 00:05:15 +0800
commit3566d71aea5486709f73ba7fac5a421993b41f00 (patch)
tree487cec8110bdb7f004d761dcdb5c719bc5303cd6 /lib
parente2694d7a62f9a9b626794d2eeef0235c8a348ad3 (diff)
parent23a12dd927b66880fa991b377d450455851b69a9 (diff)
Merge branch 'master' into frontend-next
Diffstat (limited to '')
-rw-r--r--lib/models/note.js43
-rwxr-xr-xlib/response.js59
2 files changed, 34 insertions, 68 deletions
diff --git a/lib/models/note.js b/lib/models/note.js
index 5c63dc1a..132f8b1e 100644
--- a/lib/models/note.js
+++ b/lib/models/note.js
@@ -236,33 +236,17 @@ module.exports = function (sequelize, DataTypes) {
});
},
parseNoteInfo: function (body) {
- var meta = null;
- try {
- var obj = metaMarked(body);
- body = obj.markdown;
- meta = obj.meta;
- } catch (err) {
- //na
- }
- if (!meta) meta = {};
- var $ = cheerio.load(md.render(body));
+ var parsed = Note.extractMeta(body);
+ var $ = cheerio.load(md.render(parsed.markdown));
return {
- title: Note.extractNoteTitle(meta, $),
- tags: Note.extractNoteTags(meta, $)
+ title: Note.extractNoteTitle(parsed.meta, $),
+ tags: Note.extractNoteTags(parsed.meta, $)
};
},
parseNoteTitle: function (body) {
- var meta = null;
- try {
- var obj = metaMarked(body);
- body = obj.markdown;
- meta = obj.meta;
- } catch (err) {
- //na
- }
- if (!meta) meta = {};
- var $ = cheerio.load(md.render(body));
- return Note.extractNoteTitle(meta, $);
+ var parsed = Note.extractMeta(body);
+ var $ = cheerio.load(md.render(parsed.markdown));
+ return Note.extractNoteTitle(parsed.meta, $);
},
extractNoteTitle: function (meta, $) {
var title = "";
@@ -320,6 +304,19 @@ module.exports = function (sequelize, DataTypes) {
}
return tags;
},
+ extractMeta: function (content) {
+ try {
+ var obj = metaMarked(content);
+ if (!obj.markdown) obj.markdown = "";
+ if (!obj.meta) obj.meta = {};
+ } catch (err) {
+ var obj = {
+ markdown: content,
+ meta: {}
+ };
+ }
+ return obj;
+ },
parseMeta: function (meta) {
var _meta = {};
if (meta) {
diff --git a/lib/response.js b/lib/response.js
index 54e2a337..a0dc8b1f 100755
--- a/lib/response.js
+++ b/lib/response.js
@@ -6,7 +6,6 @@ var markdownpdf = require("markdown-pdf");
var LZString = require('lz-string');
var S = require('string');
var shortId = require('shortid');
-var metaMarked = require('meta-marked');
var querystring = require('querystring');
var request = require('request');
var moment = require('moment');
@@ -76,13 +75,8 @@ function showIndex(req, res, next) {
function responseHackMD(res, note) {
var body = note.content;
- var meta = null;
- try {
- meta = models.Note.parseMeta(metaMarked(body).meta);
- } catch(err) {
- //na
- }
- if (!meta) meta = {};
+ var extracted = models.Note.extractMeta(body);
+ var meta = models.Note.parseMeta(extracted.meta);
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title);
res.set({
@@ -192,16 +186,9 @@ function showPublishNote(req, res, next) {
return response.errorNotFound(res);
}
var body = note.content;
- var meta = null;
- var markdown = null;
- try {
- var obj = metaMarked(body);
- markdown = obj.markdown;
- meta = models.Note.parseMeta(obj.meta);
- } catch(err) {
- //na
- }
- if (!meta) meta = {};
+ var extracted = models.Note.extractMeta(body);
+ markdown = extracted.markdown;
+ meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
@@ -266,16 +253,9 @@ function actionDownload(req, res, note) {
function actionInfo(req, res, note) {
var body = note.content;
- var meta = null;
- var markdown = null;
- try {
- var obj = metaMarked(body);
- markdown = obj.markdown;
- meta = models.Note.parseMeta(obj.meta);
- } catch(err) {
- //na
- }
- if (!meta) meta = {};
+ var extracted = models.Note.extractMeta(body);
+ var markdown = extracted.markdown;
+ var meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
@@ -298,18 +278,14 @@ function actionInfo(req, res, note) {
function actionPDF(req, res, note) {
var body = note.content;
- try {
- body = metaMarked(body).markdown;
- } catch(err) {
- //na
- }
+ var extracted = models.Note.extractMeta(body);
var title = models.Note.decodeTitle(note.title);
if (!fs.existsSync(config.tmppath)) {
fs.mkdirSync(config.tmppath);
}
var path = config.tmppath + '/' + Date.now() + '.pdf';
- markdownpdf().from.string(body).to(path, function () {
+ markdownpdf().from.string(extracted.markdown).to(path, function () {
var stream = fs.createReadStream(path);
var filename = title;
// Be careful of special characters
@@ -580,16 +556,9 @@ function showPublishSlide(req, res, next) {
return response.errorNotFound(res);
}
var body = note.content;
- var meta = null;
- var markdown = null;
- try {
- var obj = metaMarked(body);
- markdown = obj.markdown;
- meta = models.Note.parseMeta(obj.meta);
- } catch(err) {
- //na
- }
- if (!meta) meta = {};
+ var extracted = models.Note.extractMeta(body);
+ markdown = extracted.markdown;
+ meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
@@ -603,7 +572,7 @@ function showPublishSlide(req, res, next) {
updatetime: updatetime,
url: origin,
body: markdown,
- meta: JSON.stringify(obj.meta || {}),
+ meta: JSON.stringify(extracted.meta),
useCDN: config.usecdn,
owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,