Merge branch 'master' into frontend-next

This commit is contained in:
Yukai Huang 2017-01-07 00:05:15 +08:00
commit 3566d71aea
5 changed files with 76 additions and 86 deletions

View file

@ -236,33 +236,17 @@ module.exports = function (sequelize, DataTypes) {
}); });
}, },
parseNoteInfo: function (body) { parseNoteInfo: function (body) {
var meta = null; var parsed = Note.extractMeta(body);
try { var $ = cheerio.load(md.render(parsed.markdown));
var obj = metaMarked(body);
body = obj.markdown;
meta = obj.meta;
} catch (err) {
//na
}
if (!meta) meta = {};
var $ = cheerio.load(md.render(body));
return { return {
title: Note.extractNoteTitle(meta, $), title: Note.extractNoteTitle(parsed.meta, $),
tags: Note.extractNoteTags(meta, $) tags: Note.extractNoteTags(parsed.meta, $)
}; };
}, },
parseNoteTitle: function (body) { parseNoteTitle: function (body) {
var meta = null; var parsed = Note.extractMeta(body);
try { var $ = cheerio.load(md.render(parsed.markdown));
var obj = metaMarked(body); return Note.extractNoteTitle(parsed.meta, $);
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, $) { extractNoteTitle: function (meta, $) {
var title = ""; var title = "";
@ -320,6 +304,19 @@ module.exports = function (sequelize, DataTypes) {
} }
return tags; 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) { parseMeta: function (meta) {
var _meta = {}; var _meta = {};
if (meta) { if (meta) {

View file

@ -6,7 +6,6 @@ var markdownpdf = require("markdown-pdf");
var LZString = require('lz-string'); var LZString = require('lz-string');
var S = require('string'); var S = require('string');
var shortId = require('shortid'); var shortId = require('shortid');
var metaMarked = require('meta-marked');
var querystring = require('querystring'); var querystring = require('querystring');
var request = require('request'); var request = require('request');
var moment = require('moment'); var moment = require('moment');
@ -76,13 +75,8 @@ function showIndex(req, res, next) {
function responseHackMD(res, note) { function responseHackMD(res, note) {
var body = note.content; var body = note.content;
var meta = null; var extracted = models.Note.extractMeta(body);
try { var meta = models.Note.parseMeta(extracted.meta);
meta = models.Note.parseMeta(metaMarked(body).meta);
} catch(err) {
//na
}
if (!meta) meta = {};
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title); title = models.Note.generateWebTitle(meta.title || title);
res.set({ res.set({
@ -192,16 +186,9 @@ function showPublishNote(req, res, next) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
var body = note.content; var body = note.content;
var meta = null; var extracted = models.Note.extractMeta(body);
var markdown = null; markdown = extracted.markdown;
try { meta = models.Note.parseMeta(extracted.meta);
var obj = metaMarked(body);
markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
}
if (!meta) meta = {};
var createtime = note.createdAt; var createtime = note.createdAt;
var updatetime = note.lastchangeAt; var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
@ -266,16 +253,9 @@ function actionDownload(req, res, note) {
function actionInfo(req, res, note) { function actionInfo(req, res, note) {
var body = note.content; var body = note.content;
var meta = null; var extracted = models.Note.extractMeta(body);
var markdown = null; var markdown = extracted.markdown;
try { var meta = models.Note.parseMeta(extracted.meta);
var obj = metaMarked(body);
markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
}
if (!meta) meta = {};
var createtime = note.createdAt; var createtime = note.createdAt;
var updatetime = note.lastchangeAt; var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
@ -298,18 +278,14 @@ function actionInfo(req, res, note) {
function actionPDF(req, res, note) { function actionPDF(req, res, note) {
var body = note.content; var body = note.content;
try { var extracted = models.Note.extractMeta(body);
body = metaMarked(body).markdown;
} catch(err) {
//na
}
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
if (!fs.existsSync(config.tmppath)) { if (!fs.existsSync(config.tmppath)) {
fs.mkdirSync(config.tmppath); fs.mkdirSync(config.tmppath);
} }
var path = config.tmppath + '/' + Date.now() + '.pdf'; var path = config.tmppath + '/' + Date.now() + '.pdf';
markdownpdf().from.string(body).to(path, function () { markdownpdf().from.string(extracted.markdown).to(path, function () {
var stream = fs.createReadStream(path); var stream = fs.createReadStream(path);
var filename = title; var filename = title;
// Be careful of special characters // Be careful of special characters
@ -580,16 +556,9 @@ function showPublishSlide(req, res, next) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
var body = note.content; var body = note.content;
var meta = null; var extracted = models.Note.extractMeta(body);
var markdown = null; markdown = extracted.markdown;
try { meta = models.Note.parseMeta(extracted.meta);
var obj = metaMarked(body);
markdown = obj.markdown;
meta = models.Note.parseMeta(obj.meta);
} catch(err) {
//na
}
if (!meta) meta = {};
var createtime = note.createdAt; var createtime = note.createdAt;
var updatetime = note.lastchangeAt; var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title); var title = models.Note.decodeTitle(note.title);
@ -603,7 +572,7 @@ function showPublishSlide(req, res, next) {
updatetime: updatetime, updatetime: updatetime,
url: origin, url: origin,
body: markdown, body: markdown,
meta: JSON.stringify(obj.meta || {}), meta: JSON.stringify(extracted.meta),
useCDN: config.usecdn, useCDN: config.usecdn,
owner: note.owner ? note.owner.id : null, owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null, ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,

View file

@ -13,6 +13,7 @@ import { saveAs } from 'file-saver';
require('./common'); require('./common');
require('../vendor/md-toc'); require('../vendor/md-toc');
var Viz = require("viz.js");
//auto update last change //auto update last change
window.createtime = null; window.createtime = null;
@ -314,6 +315,7 @@ export function finishView(view) {
svg[0].setAttribute('preserveAspectRatio', 'xMidYMid meet'); svg[0].setAttribute('preserveAspectRatio', 'xMidYMid meet');
} catch (err) { } catch (err) {
$value.unwrap(); $value.unwrap();
$value.parent().append('<div class="alert alert-warning">' + err + '</div>');
console.warn(err); console.warn(err);
} }
}); });
@ -337,26 +339,36 @@ export function finishView(view) {
$value.children().unwrap().unwrap(); $value.children().unwrap().unwrap();
} catch (err) { } catch (err) {
$value.unwrap(); $value.unwrap();
$value.parent().append('<div class="alert alert-warning">' + err + '</div>');
console.warn(err); console.warn(err);
} }
}); });
//graphviz //graphviz
const Viz = require("viz.js"); var graphvizs = view.find("div.graphviz.raw").removeClass("raw");
const graphvizs = view.find("div.graphviz.raw").removeClass("raw"); function parseGraphviz(key, value) {
graphvizs.each((key, value) => { var $value = $(value);
var $ele = $(value).parent().parent();
var graphviz = Viz($value.text());
if (!graphviz) throw Error('viz.js output empty graph');
$value.html(graphviz);
$ele.addClass('graphviz');
$value.children().unwrap().unwrap();
}
graphvizs.each(function (key, value) {
try { try {
var $value = $(value); parseGraphviz(key, value);
const $ele = $(value).parent().parent();
const graphviz = Viz($value.text());
if (!graphviz) throw Error('viz.js output empty graph');
$value.html(graphviz);
$ele.addClass('graphviz');
$value.children().unwrap().unwrap();
} catch (err) { } catch (err) {
$value.unwrap(); // workaround for graphviz not recover from error
console.warn(err); try {
parseGraphviz(key, value);
} catch (err) {
var $value = $(value);
$value.unwrap();
$value.parent().append('<div class="alert alert-warning">' + err + '</div>');
console.warn(err);
}
} }
}); });
//mermaid //mermaid
@ -376,11 +388,11 @@ export function finishView(view) {
$ele.html($value.text()); $ele.html($value.text());
mermaid.init(undefined, $ele); mermaid.init(undefined, $ele);
} else { } else {
$value.unwrap(); throw new Error(mermaidError);
console.warn(mermaidError);
} }
} catch (err) { } catch (err) {
$value.unwrap(); $value.unwrap();
$value.parent().append('<div class="alert alert-warning">' + err + '</div>');
console.warn(err); console.warn(err);
} }
}); });
@ -539,6 +551,16 @@ export function postProcess(code) {
}); });
} }
} }
// show yaml meta paring error
if (md.metaError) {
var warning = result.find('div#meta-error');
if (warning && warning.length > 0) {
warning.text(md.metaError)
} else {
warning = $('<div id="meta-error" class="alert alert-warning">' + md.metaError + '</div>')
result.prepend(warning);
}
}
return result; return result;
} }
window.postProcess = postProcess; window.postProcess = postProcess;
@ -1103,7 +1125,9 @@ function meta(state, start, end, silent) {
try { try {
md.meta = jsyaml.safeLoad(data.join('\n')) || {}; md.meta = jsyaml.safeLoad(data.join('\n')) || {};
delete md.metaError;
} catch(err) { } catch(err) {
md.metaError = err;
console.warn(err); console.warn(err);
return false; return false;
} }

2
public/vendor/ot/codemirror-adapter.js vendored Normal file → Executable file
View file

@ -328,7 +328,7 @@ ot.CodeMirrorAdapter = (function (global) {
return { return {
clear: function () { clear: function () {
for (var i = 0; i < selectionObjects.length; i++) { for (var i = 0; i < selectionObjects.length; i++) {
selectionObjects[i].clear(); if (selectionObjects[i]) selectionObjects[i].clear();
} }
} }
}; };

File diff suppressed because one or more lines are too long