From 2ecec3b59aabe2fd6156338cd4cbab7672d4f9b1 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Tue, 12 Jan 2016 08:01:42 -0600 Subject: Support show last change user with profile and support YAML config inside the note with robots, lang, dir, breaks options --- lib/response.js | 182 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 101 insertions(+), 81 deletions(-) (limited to 'lib/response.js') diff --git a/lib/response.js b/lib/response.js index 6a180f2b..a30df470 100644 --- a/lib/response.js +++ b/lib/response.js @@ -8,6 +8,7 @@ var markdownpdf = require("markdown-pdf"); var LZString = require('lz-string'); var S = require('string'); var shortId = require('shortid'); +var metaMarked = require('meta-marked'); //core var config = require("../config.js"); @@ -15,6 +16,7 @@ var config = require("../config.js"); //others var db = require("./db.js"); var Note = require("./note.js"); +var User = require("./user.js"); //slides var md = require('reveal.js/plugin/markdown/markdown'); @@ -104,6 +106,13 @@ function responseHackMD(res, noteId) { responseError(res, "404", "Not Found", "oops."); return; } + var body = LZString.decompressFromBase64(data.rows[0].content); + var meta = null; + try { + meta = metaMarked(body).meta; + } catch(err) { + //na + } var title = data.rows[0].title; var decodedTitle = LZString.decompressFromBase64(title); if (decodedTitle) title = decodedTitle; @@ -116,7 +125,8 @@ function responseHackMD(res, noteId) { var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); var html = compiled({ title: title, - useCDN: config.usecdn + useCDN: config.usecdn, + robots: (meta && meta.robots) || false //default allow robots }); var buf = html; res.writeHead(200, { @@ -192,34 +202,47 @@ function showPublishNote(req, res, next) { return; } var body = LZString.decompressFromBase64(data.rows[0].content); + var meta = null; + try { + meta = metaMarked(body).meta; + } catch(err) { + //na + } var updatetime = data.rows[0].update_time; var text = S(body).escapeHTML().s; var title = data.rows[0].title; var decodedTitle = LZString.decompressFromBase64(title); if (decodedTitle) title = decodedTitle; title = Note.generateWebTitle(title); - var template = config.prettypath; - var options = { - cache: !config.debug, - filename: template - }; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); var origin = config.getserverurl(); - var html = compiled({ + var data = { title: title, viewcount: note.viewcount, updatetime: updatetime, url: origin, body: text, - useCDN: config.usecdn - }); - var buf = html; - res.writeHead(200, { - 'Content-Type': 'text/html; charset=UTF-8', - 'Cache-Control': 'private', - 'Content-Length': buf.length - }); - res.end(buf); + useCDN: config.usecdn, + lastchangeuserprofile: null, + robots: (meta && meta.robots) || false //default allow robots + }; + if (note.lastchangeuser) { + //find last change user profile if lastchangeuser exists + User.findUser(note.lastchangeuser, function (err, user) { + if (!err && user && user.profile) { + var profile = JSON.parse(user.profile); + if (profile) { + data.lastchangeuserprofile = { + name: profile.displayName || profile.username, + photo: User.parsePhotoByProfile(profile) + } + renderPublish(data, res); + } + } + }); + } else { + renderPublish(data, res); + } + }); }); }); @@ -228,6 +251,23 @@ function showPublishNote(req, res, next) { } } +function renderPublish(data, res) { + var template = config.prettypath; + var options = { + cache: !config.debug, + filename: template + }; + var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); + var html = compiled(data); + var buf = html; + res.writeHead(200, { + 'Content-Type': 'text/html; charset=UTF-8', + 'Cache-Control': 'private', + 'Content-Length': buf.length + }); + res.end(buf); +} + function actionPublish(req, res, noteId) { db.readFromDB(noteId, function (err, data) { if (err) { @@ -269,36 +309,6 @@ function actionSlide(req, res, noteId) { }); }); } -//pretty api is deprecated -function actionPretty(req, res, noteId) { - db.readFromDB(noteId, function (err, data) { - if (err) { - responseError(res, "404", "Not Found", "oops."); - return; - } - var body = LZString.decompressFromBase64(data.rows[0].content); - var text = S(body).escapeHTML().s; - var title = data.rows[0].title; - var decodedTitle = LZString.decompressFromBase64(title); - if (decodedTitle) title = decodedTitle; - title = Note.generateWebTitle(title); - var template = config.prettypath; - var compiled = ejs.compile(fs.readFileSync(template, 'utf8')); - var origin = config.getserverurl(); - var html = compiled({ - title: title, - url: origin, - body: text - }); - var buf = html; - res.writeHead(200, { - 'Content-Type': 'text/html; charset=UTF-8', - 'Cache-Control': 'private', - 'Content-Length': buf.length - }); - res.end(buf); - }); -} function actionDownload(req, res, noteId) { db.readFromDB(noteId, function (err, data) { @@ -325,6 +335,11 @@ function actionPDF(req, res, noteId) { return; } var body = LZString.decompressFromBase64(data.rows[0].content); + try { + body = metaMarked(body).markdown; + } catch(err) { + //na + } var title = Note.getNoteTitle(body); if (!fs.existsSync(config.tmppath)) { @@ -361,46 +376,46 @@ function noteActions(req, res, next) { } var action = req.params.action; switch (action) { - case "publish": - case "pretty": //pretty deprecated - actionPublish(req, res, noteId); - break; - case "slide": - actionSlide(req, res, noteId); - break; - case "download": - actionDownload(req, res, noteId); - break; - case "pdf": - actionPDF(req, res, noteId); - break; - default: - if (noteId != config.featuresnotename) - res.redirect('/' + LZString.compressToBase64(noteId)); - else - res.redirect('/' + noteId); - break; + case "publish": + case "pretty": //pretty deprecated + actionPublish(req, res, noteId); + break; + case "slide": + actionSlide(req, res, noteId); + break; + case "download": + actionDownload(req, res, noteId); + break; + case "pdf": + actionPDF(req, res, noteId); + break; + default: + if (noteId != config.featuresnotename) + res.redirect('/' + LZString.compressToBase64(noteId)); + else + res.redirect('/' + noteId); + break; } } function publishNoteActions(req, res, next) { var action = req.params.action; switch (action) { - case "edit": - var shortid = req.params.shortid; - if (shortId.isValid(shortid)) { - Note.findNote(shortid, function (err, note) { - if (err || !note) { - responseError(res, "404", "Not Found", "oops."); - return; - } - if (note.id != config.featuresnotename) - res.redirect('/' + LZString.compressToBase64(note.id)); - else - res.redirect('/' + note.id); - }); - } - break; + case "edit": + var shortid = req.params.shortid; + if (shortId.isValid(shortid)) { + Note.findNote(shortid, function (err, note) { + if (err || !note) { + responseError(res, "404", "Not Found", "oops."); + return; + } + if (note.id != config.featuresnotename) + res.redirect('/' + LZString.compressToBase64(note.id)); + else + res.redirect('/' + note.id); + }); + } + break; } } @@ -424,6 +439,11 @@ function showPublishSlide(req, res, next) { return; } var body = LZString.decompressFromBase64(data.rows[0].content); + try { + body = metaMarked(body).markdown; + } catch(err) { + //na + } var title = data.rows[0].title; var decodedTitle = LZString.decompressFromBase64(title); if (decodedTitle) title = decodedTitle; -- cgit v1.2.3