diff options
author | Cheng-Han, Wu | 2016-06-21 21:42:03 +0800 |
---|---|---|
committer | Cheng-Han, Wu | 2016-06-21 21:42:03 +0800 |
commit | 558304ff62a648e604b03afe3372ef9566aea850 (patch) | |
tree | f01521782685784508e4e5017d25a9c6b7768d06 /lib | |
parent | ad6982e77e1948bd7b74538af0c5a24120a38370 (diff) |
Update to support new metadata: title, description, tags and google-analytics (GA) and refactor render publish slide response function
Diffstat (limited to '')
-rw-r--r-- | lib/models/note.js | 38 | ||||
-rw-r--r-- | lib/response.js | 50 |
2 files changed, 60 insertions, 28 deletions
diff --git a/lib/models/note.js b/lib/models/note.js index a442f889..db0493b2 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -5,6 +5,7 @@ var fs = require('fs'); var path = require('path'); var LZString = require('lz-string'); var marked = require('marked'); +var metaMarked = require('meta-marked'); var cheerio = require('cheerio'); var shortId = require('shortid'); var Sequelize = require("sequelize"); @@ -187,13 +188,24 @@ module.exports = function (sequelize, DataTypes) { }); }, parseNoteTitle: function (body) { - var $ = cheerio.load(marked(body)); - var h1s = $("h1"); var title = ""; - if (h1s.length > 0 && h1s.first().text().split('\n').length == 1) - title = h1s.first().text(); - else - title = "Untitled"; + var meta = null; + try { + var obj = metaMarked(body); + body = obj.markdown; + meta = obj.meta; + } catch (err) { + //na + } + if (meta && meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) { + title = meta.title; + } else { + var $ = cheerio.load(marked(body)); + var h1s = $("h1"); + if (h1s.length > 0 && h1s.first().text().split('\n').length == 1) + title = h1s.first().text(); + } + if (!title) title = "Untitled"; return title; }, decodeTitle: function (title) { @@ -205,6 +217,20 @@ module.exports = function (sequelize, DataTypes) { generateWebTitle: function (title) { title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD"; return title; + }, + parseMeta: function (meta) { + var _meta = {}; + if (meta) { + if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) + _meta.title = meta.title; + if (meta.description && (typeof meta.description == "string" || typeof meta.description == "number")) + _meta.description = meta.description; + if (meta.robots && (typeof meta.robots == "string" || typeof meta.robots == "number")) + _meta.robots = meta.robots; + if (meta.GA && (typeof meta.GA == "string" || typeof meta.GA == "number")) + _meta.GA = meta.GA; + } + return _meta; } }, hooks: { diff --git a/lib/response.js b/lib/response.js index 5d7fc0a1..133d7a37 100644 --- a/lib/response.js +++ b/lib/response.js @@ -21,7 +21,7 @@ var models = require("./models"); var md = require('reveal.js/plugin/markdown/markdown'); //reveal.js -var opts = { +var slideOptions = { template: fs.readFileSync(config.slidepath).toString(), theme: 'css/theme/black.css', highlightTheme: 'zenburn', @@ -107,7 +107,7 @@ function responseHackMD(res, note) { var body = LZString.decompressFromBase64(note.content); var meta = null; try { - meta = metaMarked(body).meta; + meta = models.Note.parseMeta(metaMarked(body).meta); } catch(err) { //na } @@ -121,7 +121,7 @@ function responseHackMD(res, note) { var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); var html = compiled({ url: config.serverurl, - title: title, + title: meta.title || title, useCDN: config.usecdn, facebook: config.facebook, twitter: config.twitter, @@ -212,7 +212,7 @@ function showPublishNote(req, res, next) { var body = LZString.decompressFromBase64(note.content); var meta = null; try { - meta = metaMarked(body).meta; + meta = models.Note.parseMeta(metaMarked(body).meta); } catch(err) { //na } @@ -223,7 +223,8 @@ function showPublishNote(req, res, next) { title = models.Note.generateWebTitle(title); var origin = config.serverurl; var data = { - title: title, + title: meta.title || title, + description: meta.description, viewcount: note.viewcount, createtime: createtime, updatetime: updatetime, @@ -231,7 +232,8 @@ function showPublishNote(req, res, next) { body: text, useCDN: config.usecdn, lastchangeuserprofile: note.lastchangeuser ? models.User.parseProfile(note.lastchangeuser.profile) : null, - robots: (meta && meta.robots) || false //default allow robots + robots: meta.robots || false, //default allow robots + GA: meta.GA }; return renderPublish(data, res); }).catch(function (err) { @@ -527,14 +529,28 @@ function showPublishSlide(req, res, next) { } var body = LZString.decompressFromBase64(note.content); try { - body = metaMarked(body).markdown; + var obj = metaMarked(body); + body = obj.markdown; + meta = models.Note.parseMeta(obj.meta); } catch(err) { //na } + var text = S(body).escapeHTML().s; var title = models.Note.decodeTitle(note.title); title = models.Note.generateWebTitle(title); - var text = S(body).escapeHTML().s; - render(res, title, text); + var slides = md.slidify(text, slideOptions); + var origin = config.serverurl; + var data = { + url: origin, + title: meta.title || title, + description: meta.description, + theme: slideOptions.theme, + highlightTheme: slideOptions.highlightTheme, + slides: slides, + options: JSON.stringify(slideOptions.revealOptions, null, 2), + GA: meta.GA + }; + return renderPublishSlide(data, res); }).catch(function (err) { logger.error(err); return response.errorInternalError(res); @@ -542,24 +558,14 @@ function showPublishSlide(req, res, next) { }); } -//reveal.js render -var render = function (res, title, markdown) { - var slides = md.slidify(markdown, opts); - +function renderPublishSlide(data, res) { var template = config.slidepath; var options = { cache: !config.debug, filename: template }; var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); - var html = compiled({ - url: config.serverurl, - title: title, - theme: opts.theme, - highlightTheme: opts.highlightTheme, - slides: slides, - options: JSON.stringify(opts.revealOptions, null, 2) - }); + var html = compiled(data); var buf = html; res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8', @@ -567,6 +573,6 @@ var render = function (res, title, markdown) { 'Content-Length': buf.length }); res.end(buf); -}; +} module.exports = response; |