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.js84
1 files changed, 47 insertions, 37 deletions
diff --git a/lib/models/note.js b/lib/models/note.js
index 5727046c..132f8b1e 100644
--- a/lib/models/note.js
+++ b/lib/models/note.js
@@ -52,13 +52,31 @@ module.exports = function (sequelize, DataTypes) {
defaultValue: 0
},
title: {
- type: DataTypes.TEXT
+ type: DataTypes.TEXT,
+ get: function () {
+ return sequelize.processData(this.getDataValue('title'), "");
+ },
+ set: function (value) {
+ this.setDataValue('title', sequelize.stripNullByte(value));
+ }
},
content: {
- type: DataTypes.TEXT
+ type: DataTypes.TEXT,
+ get: function () {
+ return sequelize.processData(this.getDataValue('content'), "");
+ },
+ set: function (value) {
+ this.setDataValue('content', sequelize.stripNullByte(value));
+ }
},
authorship: {
- type: DataTypes.TEXT
+ type: DataTypes.TEXT,
+ get: function () {
+ return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
+ },
+ set: function (value) {
+ this.setDataValue('authorship', JSON.stringify(value));
+ }
},
lastchangeAt: {
type: DataTypes.DATE
@@ -124,8 +142,6 @@ module.exports = function (sequelize, DataTypes) {
var body = fs.readFileSync(filePath, 'utf8');
var contentLength = body.length;
var title = Note.parseNoteTitle(body);
- body = LZString.compressToBase64(body);
- title = LZString.compressToBase64(title);
if (fsModifiedTime.isAfter(dbModifiedTime) && note.content !== body) {
note.update({
title: title,
@@ -135,14 +151,14 @@ module.exports = function (sequelize, DataTypes) {
sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
if (err) return _callback(err, null);
// update authorship on after making revision of docs
- var patch = dmp.patch_fromText(LZString.decompressFromBase64(revision.patch));
+ var patch = dmp.patch_fromText(revision.patch);
var operations = Note.transformPatchToOperations(patch, contentLength);
- var authorship = note.authorship ? JSON.parse(LZString.decompressFromBase64(note.authorship)) : [];
+ var authorship = note.authorship;
for (var i = 0; i < operations.length; i++) {
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship);
}
note.update({
- authorship: LZString.compressToBase64(JSON.stringify(authorship))
+ authorship: JSON.stringify(authorship)
}).then(function (note) {
return callback(null, note.id);
}).catch(function (err) {
@@ -220,33 +236,17 @@ module.exports = function (sequelize, DataTypes) {
});
},
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));
+ var parsed = Note.extractMeta(body);
+ var $ = cheerio.load(md.render(parsed.markdown));
return {
- title: Note.extractNoteTitle(meta, $),
- tags: Note.extractNoteTags(meta, $)
+ title: Note.extractNoteTitle(parsed.meta, $),
+ tags: Note.extractNoteTags(parsed.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, $);
+ var parsed = Note.extractMeta(body);
+ var $ = cheerio.load(md.render(parsed.markdown));
+ return Note.extractNoteTitle(parsed.meta, $);
},
extractNoteTitle: function (meta, $) {
var title = "";
@@ -264,10 +264,7 @@ module.exports = function (sequelize, DataTypes) {
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ');
},
decodeTitle: function (title) {
- var decodedTitle = LZString.decompressFromBase64(title);
- if (decodedTitle) title = decodedTitle;
- else title = 'Untitled';
- return title;
+ return title ? title : 'Untitled';
},
generateWebTitle: function (title) {
title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD";
@@ -307,6 +304,19 @@ module.exports = function (sequelize, DataTypes) {
}
return tags;
},
+ extractMeta: function (content) {
+ try {
+ var obj = metaMarked(content);
+ if (!obj.markdown) obj.markdown = "";
+ if (!obj.meta) obj.meta = {};
+ } catch (err) {
+ var obj = {
+ markdown: content,
+ meta: {}
+ };
+ }
+ return obj;
+ },
parseMeta: function (meta) {
var _meta = {};
if (meta) {
@@ -496,8 +506,8 @@ module.exports = function (sequelize, DataTypes) {
if (Note.checkFileExist(filePath)) {
var fsCreatedTime = moment(fs.statSync(filePath).ctime);
body = fs.readFileSync(filePath, 'utf8');
- note.title = LZString.compressToBase64(Note.parseNoteTitle(body));
- note.content = LZString.compressToBase64(body);
+ note.title = Note.parseNoteTitle(body);
+ note.content = body;
if (filePath !== config.defaultnotepath) {
note.createdAt = fsCreatedTime;
}