diff options
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/note.js | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/lib/models/note.js b/lib/models/note.js index 3478538f..08ef083d 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -218,8 +218,23 @@ module.exports = function (sequelize, DataTypes) { return callback(null, null); }); }, + 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)); + return { + title: Note.extractNoteTitle(meta, $), + tags: Note.extractNoteTags(meta, $) + }; + }, parseNoteTitle: function (body) { - var title = ""; var meta = null; try { var obj = metaMarked(body); @@ -229,10 +244,14 @@ module.exports = function (sequelize, DataTypes) { //na } if (!meta) meta = {}; + var $ = cheerio.load(md.render(body)); + return Note.extractNoteTitle(meta, $); + }, + extractNoteTitle: function (meta, $) { + var title = ""; if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) { title = meta.title; } else { - var $ = cheerio.load(md.render(body)); var h1s = $("h1"); if (h1s.length > 0 && h1s.first().text().split('\n').length == 1) title = S(h1s.first().text()).stripTags().s; @@ -250,6 +269,40 @@ module.exports = function (sequelize, DataTypes) { title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD"; return title; }, + extractNoteTags: function (meta, $) { + var tags = []; + var rawtags = []; + if (meta.tags && (typeof meta.tags == "string" || typeof meta.tags == "number")) { + var metaTags = ('' + meta.tags).split(','); + for (var i = 0; i < metaTags.length; i++) { + var text = metaTags[i].trim(); + if (text) rawtags.push(text); + } + } else { + var h6s = $("h6"); + h6s.each(function (key, value) { + if (/^tags/gmi.test($(value).text())) { + var codes = $(value).find("code"); + for (var i = 0; i < codes.length; i++) { + var text = $(codes[i]).html().trim(); + if (text) rawtags.push(text); + } + } + }); + } + for (var i = 0; i < rawtags.length; i++) { + var found = false; + for (var j = 0; j < tags.length; j++) { + if (tags[j] == rawtags[i]) { + found = true; + break; + } + } + if (!found) + tags.push(rawtags[i]); + } + return tags; + }, parseMeta: function (meta) { var _meta = {}; if (meta) { |