summaryrefslogtreecommitdiff
path: root/lib/models/note.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/models/note.js')
-rw-r--r--lib/models/note.js38
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: {