diff options
author | Wu Cheng-Han | 2017-01-04 23:57:16 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2017-01-04 23:57:16 +0800 |
commit | c1b5e74cf915345bc608f71c8fb09e4f3f044d33 (patch) | |
tree | f0165632253f2d6e2806c4c102b4da12be8ee349 /lib/models | |
parent | ac2b100d35bd9dd9bcbca8e622a5e7ed48222598 (diff) |
Fix and refactor extracting content using metaMarked directly might lead in invalid object
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/note.js | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/lib/models/note.js b/lib/models/note.js index 5c63dc1a..132f8b1e 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -236,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 = ""; @@ -320,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) { |