diff --git a/lib/models/note.js b/lib/models/note.js index 3478538fd..08ef083d1 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -218,8 +218,7 @@ module.exports = function (sequelize, DataTypes) { return callback(null, null); }); }, - parseNoteTitle: function (body) { - var title = ""; + parseNoteInfo: function (body) { var meta = null; try { var obj = metaMarked(body); @@ -229,10 +228,30 @@ module.exports = function (sequelize, DataTypes) { //na } if (!meta) meta = {}; + var $ = cheerio.load(md.render(body)); + return { + title: Note.extractNoteTitle(meta, $), + tags: Note.extractNoteTags(meta, $) + }; + }, + parseNoteTitle: 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 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) { diff --git a/lib/realtime.js b/lib/realtime.js index c9c6543c0..a05c1f41c 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -13,6 +13,7 @@ var moment = require('moment'); //core var config = require("./config.js"); var logger = require("./logger.js"); +var history = require("./history.js"); var models = require("./models"); //ot @@ -390,6 +391,12 @@ function finishConnection(socket, note, user) { note.server.setName(socket, user.name); note.server.setColor(socket, user.color); + // update user note history + setTimeout(function () { + var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id); + history.updateHistory(user.userid, noteId, note.server.document); + }, 0); + emitOnlineUsers(socket); emitRefresh(socket); @@ -468,6 +475,7 @@ function startConnection(socket) { notes[noteId] = { id: noteId, + alias: note.alias, owner: owner, ownerprofile: ownerprofile, permission: note.permission, @@ -652,6 +660,12 @@ function operationCallback(socket, operation) { return logger.error('operation callback failed: ' + err); }); } + // update user note history + setTimeout(function() { + var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id); + history.updateHistory(userId, noteId, note.server.document); + }, 0); + } // save authorship note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship); diff --git a/public/js/history.js b/public/js/history.js index f9ce2267b..9be681048 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -139,7 +139,8 @@ function removeHistory(id, notehistory) { function writeHistory(view) { checkIfAuth( function () { - writeHistoryToServer(view); + // no need to do this anymore, this will count from server-side + // writeHistoryToServer(view); }, function () { writeHistoryToStorage(view); @@ -365,4 +366,29 @@ function parseToHistory(list, notehistory, callback) { } } callback(list, notehistory); +} + +function postHistoryToServer(noteId, data, callback) { + $.post(serverurl + '/history/' + noteId, data) + .done(function (result) { + return callback(null, result); + }) + .fail(function (xhr, status, error) { + console.error(xhr.responseText); + return callback(error, null); + }); +} + +function deleteServerHistory(noteId, callback) { + $.ajax({ + url: serverurl + '/history' + (noteId ? '/' + noteId : ""), + type: 'DELETE' + }) + .done(function (result) { + return callback(null, result); + }) + .fail(function (xhr, status, error) { + console.error(xhr.responseText); + return callback(error, null); + }); } \ No newline at end of file