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/models | |
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 'lib/models')
-rw-r--r-- | lib/models/note.js | 38 |
1 files changed, 32 insertions, 6 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: { |