Merge branch 'use_javascript_standard_style'

Introduce JavaScript Standard Style

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2017-03-13 02:09:52 +08:00
commit befa89be8b
40 changed files with 10529 additions and 10834 deletions

598
app.js
View file

@ -1,44 +1,46 @@
// app
// external modules
var express = require('express');
var toobusy = require('toobusy-js');
var ejs = require('ejs');
var passport = require('passport');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var express = require('express')
var toobusy = require('toobusy-js')
var ejs = require('ejs')
var passport = require('passport')
var methodOverride = require('method-override')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var compression = require('compression')
var session = require('express-session');
var SequelizeStore = require('connect-session-sequelize')(session.Store);
var fs = require('fs');
var url = require('url');
var path = require('path');
var imgur = require('imgur');
var formidable = require('formidable');
var morgan = require('morgan');
var passportSocketIo = require("passport.socketio");
var helmet = require('helmet');
var i18n = require('i18n');
var flash = require('connect-flash');
var validator = require('validator');
var session = require('express-session')
var SequelizeStore = require('connect-session-sequelize')(session.Store)
var fs = require('fs')
var url = require('url')
var path = require('path')
var imgur = require('imgur')
var formidable = require('formidable')
var morgan = require('morgan')
var passportSocketIo = require('passport.socketio')
var helmet = require('helmet')
var i18n = require('i18n')
var flash = require('connect-flash')
var validator = require('validator')
// core
var config = require("./lib/config.js");
var logger = require("./lib/logger.js");
var auth = require("./lib/auth.js");
var response = require("./lib/response.js");
var models = require("./lib/models");
var config = require('./lib/config.js')
var logger = require('./lib/logger.js')
var auth = require('./lib/auth.js')
var response = require('./lib/response.js')
var models = require('./lib/models')
// server setup
var app = express()
var server = null
if (config.usessl) {
var ca = (function () {
var i, len, results;
results = [];
var i, len, results
results = []
for (i = 0, len = config.sslcapath.length; i < len; i++) {
results.push(fs.readFileSync(config.sslcapath[i], 'utf8'));
results.push(fs.readFileSync(config.sslcapath[i], 'utf8'))
}
return results;
})();
return results
})()
var options = {
key: fs.readFileSync(config.sslkeypath, 'utf8'),
cert: fs.readFileSync(config.sslcertpath, 'utf8'),
@ -46,74 +48,67 @@ if (config.usessl) {
dhparam: fs.readFileSync(config.dhparampath, 'utf8'),
requestCert: false,
rejectUnauthorized: false
};
var app = express();
var server = require('https').createServer(options, app);
}
server = require('https').createServer(options, app)
} else {
var app = express();
var server = require('http').createServer(app);
server = require('http').createServer(app)
}
// logger
app.use(morgan('combined', {
"stream": logger.stream
}));
'stream': logger.stream
}))
// socket io
var io = require('socket.io')(server);
var io = require('socket.io')(server)
io.engine.ws = new (require('uws').Server)({
noServer: true,
perMessageDeflate: false
});
})
// others
var realtime = require("./lib/realtime.js");
var realtime = require('./lib/realtime.js')
// assign socket io to realtime
realtime.io = io;
realtime.io = io
// methodOverride
app.use(methodOverride('_method'));
// create application/json parser
var jsonParser = bodyParser.json({
limit: 1024 * 1024 * 10 // 10 mb
});
app.use(methodOverride('_method'))
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({
extended: false,
limit: 1024 * 1024 * 10 // 10 mb
});
})
// session store
var sessionStore = new SequelizeStore({
db: models.sequelize
});
})
// compression
app.use(compression());
app.use(compression())
// use hsts to tell https users stick to this
app.use(helmet.hsts({
maxAge: 31536000 * 1000, // 365 days
includeSubdomains: true,
preload: true
}));
}))
i18n.configure({
locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo'],
cookie: 'locale',
directory: __dirname + '/locales'
});
directory: path.join(__dirname, '/locales')
})
app.use(cookieParser());
app.use(cookieParser())
app.use(i18n.init);
app.use(i18n.init)
// routes without sessions
// static files
app.use('/', express.static(__dirname + '/public', { maxAge: config.staticcachetime }));
app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticcachetime }))
// session
app.use(session({
@ -126,278 +121,282 @@ app.use(session({
maxAge: config.sessionlife
},
store: sessionStore
}));
}))
// session resumption
var tlsSessionStore = {};
var tlsSessionStore = {}
server.on('newSession', function (id, data, cb) {
tlsSessionStore[id.toString('hex')] = data;
cb();
});
tlsSessionStore[id.toString('hex')] = data
cb()
})
server.on('resumeSession', function (id, cb) {
cb(null, tlsSessionStore[id.toString('hex')] || null);
});
cb(null, tlsSessionStore[id.toString('hex')] || null)
})
// middleware which blocks requests when we're too busy
app.use(function (req, res, next) {
if (toobusy()) {
response.errorServiceUnavailable(res);
response.errorServiceUnavailable(res)
} else {
next();
next()
}
});
})
app.use(flash());
app.use(flash())
// passport
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.initialize())
app.use(passport.session())
auth.registerAuthMethod()
// serialize and deserialize
passport.serializeUser(function (user, done) {
logger.info('serializeUser: ' + user.id);
return done(null, user.id);
});
logger.info('serializeUser: ' + user.id)
return done(null, user.id)
})
passport.deserializeUser(function (id, done) {
models.User.findOne({
where: {
id: id
}
}).then(function (user) {
logger.info('deserializeUser: ' + user.id);
return done(null, user);
logger.info('deserializeUser: ' + user.id)
return done(null, user)
}).catch(function (err) {
logger.error(err);
return done(err, null);
});
});
logger.error(err)
return done(err, null)
})
})
// check uri is valid before going further
app.use(function (req, res, next) {
try {
decodeURIComponent(req.path);
decodeURIComponent(req.path)
} catch (err) {
logger.error(err);
return response.errorBadRequest(res);
logger.error(err)
return response.errorBadRequest(res)
}
next();
});
next()
})
// redirect url without trailing slashes
app.use(function (req, res, next) {
if ("GET" == req.method && req.path.substr(-1) == '/' && req.path.length > 1) {
var query = req.url.slice(req.path.length);
var urlpath = req.path.slice(0, -1);
var serverurl = config.serverurl;
if (config.urlpath) serverurl = serverurl.slice(0, -(config.urlpath.length + 1));
res.redirect(301, serverurl + urlpath + query);
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
var query = req.url.slice(req.path.length)
var urlpath = req.path.slice(0, -1)
var serverurl = config.serverurl
if (config.urlpath) serverurl = serverurl.slice(0, -(config.urlpath.length + 1))
res.redirect(301, serverurl + urlpath + query)
} else {
next();
next()
}
});
})
// routes need sessions
// template files
app.set('views', __dirname + '/public/views');
app.set('views', path.join(__dirname, '/public/views'))
// set render engine
app.engine('ejs', ejs.renderFile);
app.engine('ejs', ejs.renderFile)
// set view engine
app.set('view engine', 'ejs');
app.set('view engine', 'ejs')
// get index
app.get("/", response.showIndex);
app.get('/', response.showIndex)
// get 403 forbidden
app.get("/403", function (req, res) {
response.errorForbidden(res);
});
app.get('/403', function (req, res) {
response.errorForbidden(res)
})
// get 404 not found
app.get("/404", function (req, res) {
response.errorNotFound(res);
});
app.get('/404', function (req, res) {
response.errorNotFound(res)
})
// get 500 internal error
app.get("/500", function (req, res) {
response.errorInternalError(res);
});
app.get('/500', function (req, res) {
response.errorInternalError(res)
})
// get status
app.get("/status", function (req, res, next) {
app.get('/status', function (req, res, next) {
realtime.getStatus(function (data) {
res.set({
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
res.send(data);
});
});
})
res.send(data)
})
})
// get status
app.get("/temp", function (req, res) {
var host = req.get('host');
if (config.alloworigin.indexOf(host) == -1)
response.errorForbidden(res);
else {
var tempid = req.query.tempid;
if (!tempid)
response.errorForbidden(res);
else {
app.get('/temp', function (req, res) {
var host = req.get('host')
if (config.alloworigin.indexOf(host) === -1) {
response.errorForbidden(res)
} else {
var tempid = req.query.tempid
if (!tempid) {
response.errorForbidden(res)
} else {
models.Temp.findOne({
where: {
id: tempid
}
}).then(function (temp) {
if (!temp)
response.errorNotFound(res);
else {
res.header("Access-Control-Allow-Origin", "*");
if (!temp) {
response.errorNotFound(res)
} else {
res.header('Access-Control-Allow-Origin', '*')
res.send({
temp: temp.data
});
})
temp.destroy().catch(function (err) {
if (err)
logger.error('remove temp failed: ' + err);
});
if (err) {
logger.error('remove temp failed: ' + err)
}
})
}
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
logger.error(err)
return response.errorInternalError(res)
})
}
}
});
})
// post status
app.post("/temp", urlencodedParser, function (req, res) {
var host = req.get('host');
if (config.alloworigin.indexOf(host) == -1)
response.errorForbidden(res);
else {
var data = req.body.data;
if (!data)
response.errorForbidden(res);
else {
if (config.debug)
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data);
app.post('/temp', urlencodedParser, function (req, res) {
var host = req.get('host')
if (config.alloworigin.indexOf(host) === -1) {
response.errorForbidden(res)
} else {
var data = req.body.data
if (!data) {
response.errorForbidden(res)
} else {
if (config.debug) {
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data)
}
models.Temp.create({
data: data
}).then(function (temp) {
if (temp) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Origin', '*')
res.send({
status: 'ok',
id: temp.id
});
} else
response.errorInternalError(res);
})
} else {
response.errorInternalError(res)
}
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
logger.error(err)
return response.errorInternalError(res)
})
}
}
});
})
function setReturnToFromReferer (req) {
var referer = req.get('referer');
if (!req.session) req.session = {};
req.session.returnTo = referer;
var referer = req.get('referer')
if (!req.session) req.session = {}
req.session.returnTo = referer
}
// facebook auth
if (config.facebook) {
app.get('/auth/facebook', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('facebook')(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('facebook')(req, res, next)
})
// facebook auth callback
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
}
// twitter auth
if (config.twitter) {
app.get('/auth/twitter', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('twitter')(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('twitter')(req, res, next)
})
// twitter auth callback
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
}
// github auth
if (config.github) {
app.get('/auth/github', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('github')(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('github')(req, res, next)
})
// github auth callback
app.get('/auth/github/callback',
passport.authenticate('github', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
// github callback actions
app.get('/auth/github/callback/:noteId/:action', response.githubActions);
app.get('/auth/github/callback/:noteId/:action', response.githubActions)
}
// gitlab auth
if (config.gitlab) {
app.get('/auth/gitlab', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('gitlab')(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('gitlab')(req, res, next)
})
// gitlab auth callback
app.get('/auth/gitlab/callback',
passport.authenticate('gitlab', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
// gitlab callback actions
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions);
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions)
}
// dropbox auth
if (config.dropbox) {
app.get('/auth/dropbox', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('dropbox-oauth2')(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('dropbox-oauth2')(req, res, next)
})
// dropbox auth callback
app.get('/auth/dropbox/callback',
passport.authenticate('dropbox-oauth2', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
}
// google auth
if (config.google) {
app.get('/auth/google', function (req, res, next) {
setReturnToFromReferer(req);
passport.authenticate('google', { scope: ['profile'] })(req, res, next);
});
setReturnToFromReferer(req)
passport.authenticate('google', { scope: ['profile'] })(req, res, next)
})
// google auth callback
app.get('/auth/google/callback',
passport.authenticate('google', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/'
}));
}))
}
// ldap auth
if (config.ldap) {
app.post('/auth/ldap', urlencodedParser, function (req, res, next) {
if (!req.body.username || !req.body.password) return response.errorBadRequest(res);
setReturnToFromReferer(req);
if (!req.body.username || !req.body.password) return response.errorBadRequest(res)
setReturnToFromReferer(req)
passport.authenticate('ldapauth', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/',
failureFlash: true
})(req, res, next);
});
})(req, res, next)
})
}
// email auth
if (config.email) {
if (config.allowemailregister)
if (config.allowemailregister) {
app.post('/register', urlencodedParser, function (req, res, next) {
if (!req.body.email || !req.body.password) return response.errorBadRequest(res);
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res);
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
models.User.findOrCreate({
where: {
email: req.body.email
@ -408,51 +407,55 @@ if (config.email) {
}).spread(function (user, created) {
if (user) {
if (created) {
if (config.debug) logger.info('user registered: ' + user.id);
req.flash('info', "You've successfully registered, please signin.");
if (config.debug) {
logger.info('user registered: ' + user.id)
}
req.flash('info', "You've successfully registered, please signin.")
} else {
if (config.debug) logger.info('user found: ' + user.id);
req.flash('error', "This email has been used, please try another one.");
if (config.debug) {
logger.info('user found: ' + user.id)
}
return res.redirect(config.serverurl + '/');
req.flash('error', 'This email has been used, please try another one.')
}
req.flash('error', "Failed to register your account, please try again.");
return res.redirect(config.serverurl + '/');
return res.redirect(config.serverurl + '/')
}
req.flash('error', 'Failed to register your account, please try again.')
return res.redirect(config.serverurl + '/')
}).catch(function (err) {
logger.error('auth callback failed: ' + err);
return response.errorInternalError(res);
});
});
logger.error('auth callback failed: ' + err)
return response.errorInternalError(res)
})
})
}
app.post('/login', urlencodedParser, function (req, res, next) {
if (!req.body.email || !req.body.password) return response.errorBadRequest(res);
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res);
setReturnToFromReferer(req);
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
setReturnToFromReferer(req)
passport.authenticate('local', {
successReturnToOrRedirect: config.serverurl + '/',
failureRedirect: config.serverurl + '/',
failureFlash: 'Invalid email or password.'
})(req, res, next);
});
})(req, res, next)
})
}
// logout
app.get('/logout', function (req, res) {
if (config.debug && req.isAuthenticated())
logger.info('user logout: ' + req.user.id);
req.logout();
res.redirect(config.serverurl + '/');
});
var history = require("./lib/history.js");
if (config.debug && req.isAuthenticated()) { logger.info('user logout: ' + req.user.id) }
req.logout()
res.redirect(config.serverurl + '/')
})
var history = require('./lib/history.js')
// get history
app.get('/history', history.historyGet);
app.get('/history', history.historyGet)
// post history
app.post('/history', urlencodedParser, history.historyPost);
app.post('/history', urlencodedParser, history.historyPost)
// post history by note id
app.post('/history/:noteId', urlencodedParser, history.historyPost);
app.post('/history/:noteId', urlencodedParser, history.historyPost)
// delete history
app.delete('/history', history.historyDelete);
app.delete('/history', history.historyDelete)
// delete history by note id
app.delete('/history/:noteId', history.historyDelete);
app.delete('/history/:noteId', history.historyDelete)
// get me info
app.get('/me', function (req, res) {
if (req.isAuthenticated()) {
@ -461,126 +464,125 @@ app.get('/me', function (req, res) {
id: req.user.id
}
}).then(function (user) {
if (!user)
return response.errorNotFound(res);
var profile = models.User.getProfile(user);
if (!user) { return response.errorNotFound(res) }
var profile = models.User.getProfile(user)
res.send({
status: 'ok',
id: req.user.id,
name: profile.name,
photo: profile.photo
});
})
}).catch(function (err) {
logger.error('read me failed: ' + err);
return response.errorInternalError(res);
});
logger.error('read me failed: ' + err)
return response.errorInternalError(res)
})
} else {
res.send({
status: 'forbidden'
});
})
}
});
})
// upload image
app.post('/uploadimage', function (req, res) {
var form = new formidable.IncomingForm();
var form = new formidable.IncomingForm()
form.keepExtensions = true;
form.keepExtensions = true
if (config.imageUploadType === 'filesystem') {
form.uploadDir = "public/uploads";
form.uploadDir = 'public/uploads'
}
form.parse(req, function (err, fields, files) {
if (err || !files.image || !files.image.path) {
response.errorForbidden(res);
response.errorForbidden(res)
} else {
if (config.debug)
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
if (config.debug) { logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)) }
try {
switch (config.imageUploadType) {
case 'filesystem':
res.send({
link: url.resolve(config.serverurl + '/', files.image.path.match(/^public\/(.+$)/)[1])
});
})
break;
break
case 's3':
var AWS = require('aws-sdk');
var awsConfig = new AWS.Config(config.s3);
var s3 = new AWS.S3(awsConfig);
var AWS = require('aws-sdk')
var awsConfig = new AWS.Config(config.s3)
var s3 = new AWS.S3(awsConfig)
fs.readFile(files.image.path, function (err, buffer) {
if (err) {
logger.error(err)
res.status(500).end('upload image error')
return
}
var params = {
Bucket: config.s3bucket,
Key: path.join('uploads', path.basename(files.image.path)),
Body: buffer
};
}
s3.putObject(params, function (err, data) {
if (err) {
logger.error(err);
res.status(500).end('upload image error');
} else {
logger.error(err)
res.status(500).end('upload image error')
return
}
res.send({
link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}`
});
}
});
});
break;
})
})
})
break
case 'imgur':
default:
imgur.setClientId(config.imgur.clientID);
imgur.setClientId(config.imgur.clientID)
imgur.uploadFile(files.image.path)
.then(function (json) {
if (config.debug)
logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
if (config.debug) { logger.info('SERVER uploadimage success: ' + JSON.stringify(json)) }
res.send({
link: json.data.link.replace(/^http:\/\//i, 'https://')
});
})
})
.catch(function (err) {
logger.error(err);
return res.status(500).end('upload image error');
});
break;
logger.error(err)
return res.status(500).end('upload image error')
})
break
}
} catch (err) {
logger.error(err);
return res.status(500).end('upload image error');
logger.error(err)
return res.status(500).end('upload image error')
}
}
});
});
})
})
// get new note
app.get("/new", response.newNote);
app.get('/new', response.newNote)
// get publish note
app.get("/s/:shortid", response.showPublishNote);
app.get('/s/:shortid', response.showPublishNote)
// publish note actions
app.get("/s/:shortid/:action", response.publishNoteActions);
app.get('/s/:shortid/:action', response.publishNoteActions)
// get publish slide
app.get("/p/:shortid", response.showPublishSlide);
app.get('/p/:shortid', response.showPublishSlide)
// publish slide actions
app.get("/p/:shortid/:action", response.publishSlideActions);
app.get('/p/:shortid/:action', response.publishSlideActions)
// get note by id
app.get("/:noteId", response.showNote);
app.get('/:noteId', response.showNote)
// note actions
app.get("/:noteId/:action", response.noteActions);
app.get('/:noteId/:action', response.noteActions)
// note actions with action id
app.get("/:noteId/:action/:actionId", response.noteActions);
app.get('/:noteId/:action/:actionId', response.noteActions)
// response not found if no any route matches
app.get('*', function (req, res) {
response.errorNotFound(res);
});
response.errorNotFound(res)
})
// socket.io secure
io.use(realtime.secure);
io.use(realtime.secure)
// socket.io auth
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
@ -589,20 +591,20 @@ io.use(passportSocketIo.authorize({
store: sessionStore,
success: realtime.onAuthorizeSuccess,
fail: realtime.onAuthorizeFail
}));
}))
// socket.io heartbeat
io.set('heartbeat interval', config.heartbeatinterval);
io.set('heartbeat timeout', config.heartbeattimeout);
io.set('heartbeat interval', config.heartbeatinterval)
io.set('heartbeat timeout', config.heartbeattimeout)
// socket.io connection
io.sockets.on('connection', realtime.connection);
io.sockets.on('connection', realtime.connection)
// listen
function startListen () {
server.listen(config.port, function () {
var schema = config.usessl ? 'HTTPS' : 'HTTP';
logger.info('%s Server listening at port %d', schema, config.port);
config.maintenance = false;
});
var schema = config.usessl ? 'HTTPS' : 'HTTP'
logger.info('%s Server listening at port %d', schema, config.port)
config.maintenance = false
})
}
// sync db then start listen
@ -610,45 +612,45 @@ models.sequelize.sync().then(function () {
// check if realtime is ready
if (realtime.isReady()) {
models.Revision.checkAllNotesRevision(function (err, notes) {
if (err) throw new Error(err);
if (!notes || notes.length <= 0) return startListen();
});
if (err) throw new Error(err)
if (!notes || notes.length <= 0) return startListen()
})
} else {
throw new Error('server still not ready after db synced');
throw new Error('server still not ready after db synced')
}
});
})
// log uncaught exception
process.on('uncaughtException', function (err) {
logger.error('An uncaught exception has occured.');
logger.error(err);
logger.error('Process will exit now.');
process.exit(1);
});
logger.error('An uncaught exception has occured.')
logger.error(err)
logger.error('Process will exit now.')
process.exit(1)
})
// install exit handler
function handleTermSignals () {
config.maintenance = true;
config.maintenance = true
// disconnect all socket.io clients
Object.keys(io.sockets.sockets).forEach(function (key) {
var socket = io.sockets.sockets[key];
var socket = io.sockets.sockets[key]
// notify client server going into maintenance status
socket.emit('maintenance');
socket.emit('maintenance')
setTimeout(function () {
socket.disconnect(true);
}, 0);
});
socket.disconnect(true)
}, 0)
})
var checkCleanTimer = setInterval(function () {
if (realtime.isReady()) {
models.Revision.checkAllNotesRevision(function (err, notes) {
if (err) return logger.error(err);
if (err) return logger.error(err)
if (!notes || notes.length <= 0) {
clearInterval(checkCleanTimer);
return process.exit(0);
clearInterval(checkCleanTimer)
return process.exit(0)
}
});
})
}
}, 100);
}, 100)
}
process.on('SIGINT', handleTermSignals);
process.on('SIGTERM', handleTermSignals);
process.on('SIGINT', handleTermSignals)
process.on('SIGTERM', handleTermSignals)

View file

@ -1,24 +1,24 @@
// auth
// external modules
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GithubStrategy = require('passport-github').Strategy;
var GitlabStrategy = require('passport-gitlab2').Strategy;
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy;
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var LdapStrategy = require('passport-ldapauth');
var LocalStrategy = require('passport-local').Strategy;
var validator = require('validator');
var passport = require('passport')
var FacebookStrategy = require('passport-facebook').Strategy
var TwitterStrategy = require('passport-twitter').Strategy
var GithubStrategy = require('passport-github').Strategy
var GitlabStrategy = require('passport-gitlab2').Strategy
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy
var GoogleStrategy = require('passport-google-oauth20').Strategy
var LdapStrategy = require('passport-ldapauth')
var LocalStrategy = require('passport-local').Strategy
var validator = require('validator')
// core
var config = require('./config.js');
var logger = require("./logger.js");
var models = require("./models");
var config = require('./config.js')
var logger = require('./logger.js')
var models = require('./models')
function callback (accessToken, refreshToken, profile, done) {
// logger.info(profile.displayName || profile.username);
var stringifiedProfile = JSON.stringify(profile);
var stringifiedProfile = JSON.stringify(profile)
models.User.findOrCreate({
where: {
profileid: profile.id.toString()
@ -30,44 +30,43 @@ function callback(accessToken, refreshToken, profile, done) {
}
}).spread(function (user, created) {
if (user) {
var needSave = false;
if (user.profile != stringifiedProfile) {
user.profile = stringifiedProfile;
needSave = true;
var needSave = false
if (user.profile !== stringifiedProfile) {
user.profile = stringifiedProfile
needSave = true
}
if (user.accessToken != accessToken) {
user.accessToken = accessToken;
needSave = true;
if (user.accessToken !== accessToken) {
user.accessToken = accessToken
needSave = true
}
if (user.refreshToken != refreshToken) {
user.refreshToken = refreshToken;
needSave = true;
if (user.refreshToken !== refreshToken) {
user.refreshToken = refreshToken
needSave = true
}
if (needSave) {
user.save().then(function () {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
});
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
})
} else {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
}
}
}).catch(function (err) {
logger.error('auth callback failed: ' + err);
return done(err, null);
});
logger.error('auth callback failed: ' + err)
return done(err, null)
})
}
function registerAuthMethod () {
// facebook
if (config.facebook) {
module.exports = passport.use(new FacebookStrategy({
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.serverurl + '/auth/facebook/callback'
}, callback));
}, callback))
}
// twitter
if (config.twitter) {
@ -75,7 +74,7 @@ if (config.twitter) {
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
callbackURL: config.serverurl + '/auth/twitter/callback'
}, callback));
}, callback))
}
// github
if (config.github) {
@ -83,7 +82,7 @@ if (config.github) {
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.serverurl + '/auth/github/callback'
}, callback));
}, callback))
}
// gitlab
if (config.gitlab) {
@ -92,7 +91,7 @@ if (config.gitlab) {
clientID: config.gitlab.clientID,
clientSecret: config.gitlab.clientSecret,
callbackURL: config.serverurl + '/auth/gitlab/callback'
}, callback));
}, callback))
}
// dropbox
if (config.dropbox) {
@ -101,7 +100,7 @@ if (config.dropbox) {
clientID: config.dropbox.clientID,
clientSecret: config.dropbox.clientSecret,
callbackURL: config.serverurl + '/auth/dropbox/callback'
}, callback));
}, callback))
}
// google
if (config.google) {
@ -109,7 +108,7 @@ if (config.google) {
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.serverurl + '/auth/google/callback'
}, callback));
}, callback))
}
// ldap
if (config.ldap) {
@ -122,7 +121,7 @@ if (config.ldap) {
searchFilter: config.ldap.searchFilter || null,
searchAttributes: config.ldap.searchAttributes || null,
tlsOptions: config.ldap.tlsOptions || null
},
}
},
function (user, done) {
var profile = {
@ -132,40 +131,38 @@ if (config.ldap) {
emails: user.mail ? [user.mail] : [],
avatarUrl: null,
profileUrl: null,
provider: 'ldap',
provider: 'ldap'
}
var stringifiedProfile = JSON.stringify(profile);
var stringifiedProfile = JSON.stringify(profile)
models.User.findOrCreate({
where: {
profileid: profile.id.toString()
},
defaults: {
profile: stringifiedProfile,
profile: stringifiedProfile
}
}).spread(function (user, created) {
if (user) {
var needSave = false;
if (user.profile != stringifiedProfile) {
user.profile = stringifiedProfile;
needSave = true;
var needSave = false
if (user.profile !== stringifiedProfile) {
user.profile = stringifiedProfile
needSave = true
}
if (needSave) {
user.save().then(function () {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
});
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
})
} else {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
}
}
}).catch(function (err) {
logger.error('ldap auth failed: ' + err);
return done(err, null);
});
}));
logger.error('ldap auth failed: ' + err)
return done(err, null)
})
}))
}
// email
if (config.email) {
@ -173,18 +170,23 @@ if (config.email) {
usernameField: 'email'
},
function (email, password, done) {
if (!validator.isEmail(email)) return done(null, false);
if (!validator.isEmail(email)) return done(null, false)
models.User.findOne({
where: {
email: email
}
}).then(function (user) {
if (!user) return done(null, false);
if (!user.verifyPassword(password)) return done(null, false);
return done(null, user);
if (!user) return done(null, false)
if (!user.verifyPassword(password)) return done(null, false)
return done(null, user)
}).catch(function (err) {
logger.error(err);
return done(err);
});
}));
logger.error(err)
return done(err)
})
}))
}
}
module.exports = {
registerAuthMethod: registerAuthMethod
}

View file

@ -1,118 +1,117 @@
// external modules
var fs = require('fs');
var path = require('path');
var fs = require('fs');
var fs = require('fs')
var path = require('path')
// configs
var env = process.env.NODE_ENV || 'development';
var config = require(path.join(__dirname, '..', 'config.json'))[env];
var debug = process.env.DEBUG ? (process.env.DEBUG === 'true') : ((typeof config.debug === 'boolean') ? config.debug : (env === 'development'));
var env = process.env.NODE_ENV || 'development'
var config = require(path.join(__dirname, '..', 'config.json'))[env]
var debug = process.env.DEBUG ? (process.env.DEBUG === 'true') : ((typeof config.debug === 'boolean') ? config.debug : (env === 'development'))
// Create function that reads docker secrets but fails fast in case of a non docker environment
var handleDockerSecret = fs.existsSync('/run/secrets/') ? function (secret) {
return fs.existsSync('/run/secrets/' + secret) ? fs.readFileSync('/run/secrets/' + secret) : null;
return fs.existsSync('/run/secrets/' + secret) ? fs.readFileSync('/run/secrets/' + secret) : null
} : function () {
return null
};
// url
var domain = process.env.DOMAIN || process.env.HMD_DOMAIN || config.domain || '';
var urlpath = process.env.URL_PATH || process.env.HMD_URL_PATH || config.urlpath || '';
var port = process.env.PORT || process.env.HMD_PORT || config.port || 3000;
var alloworigin = process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : (config.alloworigin || ['localhost']);
var usessl = !!config.usessl;
var protocolusessl = (usessl === true && typeof process.env.HMD_PROTOCOL_USESSL === 'undefined' && typeof config.protocolusessl === 'undefined')
? true : (process.env.HMD_PROTOCOL_USESSL ? (process.env.HMD_PROTOCOL_USESSL === 'true') : !!config.protocolusessl);
var urladdport = process.env.HMD_URL_ADDPORT ? (process.env.HMD_URL_ADDPORT === 'true') : !!config.urladdport;
var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((typeof config.usecdn === 'boolean') ? config.usecdn : true);
var allowanonymous = process.env.HMD_ALLOW_ANONYMOUS ? (process.env.HMD_ALLOW_ANONYMOUS === 'true') : ((typeof config.allowanonymous === 'boolean') ? config.allowanonymous : true);
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl;
var permissions = ['editable', 'limited', 'locked', 'protected', 'private'];
if (allowanonymous) {
permissions.unshift('freely');
}
var defaultpermission = process.env.HMD_DEFAULT_PERMISSION || config.defaultpermission;
defaultpermission = permissions.indexOf(defaultpermission) != -1 ? defaultpermission : 'editable';
// url
var domain = process.env.DOMAIN || process.env.HMD_DOMAIN || config.domain || ''
var urlpath = process.env.URL_PATH || process.env.HMD_URL_PATH || config.urlpath || ''
var port = process.env.PORT || process.env.HMD_PORT || config.port || 3000
var alloworigin = process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : (config.alloworigin || ['localhost'])
var usessl = !!config.usessl
var protocolusessl = (usessl === true && typeof process.env.HMD_PROTOCOL_USESSL === 'undefined' && typeof config.protocolusessl === 'undefined')
? true : (process.env.HMD_PROTOCOL_USESSL ? (process.env.HMD_PROTOCOL_USESSL === 'true') : !!config.protocolusessl)
var urladdport = process.env.HMD_URL_ADDPORT ? (process.env.HMD_URL_ADDPORT === 'true') : !!config.urladdport
var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((typeof config.usecdn === 'boolean') ? config.usecdn : true)
var allowanonymous = process.env.HMD_ALLOW_ANONYMOUS ? (process.env.HMD_ALLOW_ANONYMOUS === 'true') : ((typeof config.allowanonymous === 'boolean') ? config.allowanonymous : true)
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl
var permissions = ['editable', 'limited', 'locked', 'protected', 'private']
if (allowanonymous) {
permissions.unshift('freely')
}
var defaultpermission = process.env.HMD_DEFAULT_PERMISSION || config.defaultpermission
defaultpermission = permissions.indexOf(defaultpermission) !== -1 ? defaultpermission : 'editable'
// db
var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl;
var db = config.db || {};
var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl
var db = config.db || {}
// ssl path
var sslkeypath = (fs.existsSync('/run/secrets/key.pem') ? '/run/secrets/key.pem' : null) || config.sslkeypath || '';
var sslcertpath = (fs.existsSync('/run/secrets/cert.pem') ? '/run/secrets/cert.pem' : null) || config.sslcertpath || '';
var sslcapath = (fs.existsSync('/run/secrets/ca.pem') ? '/run/secrets/ca.pem' : null) || config.sslcapath || '';
var dhparampath = (fs.existsSync('/run/secrets/dhparam.pem') ? '/run/secrets/dhparam.pem' : null) || config.dhparampath || '';
var sslkeypath = (fs.existsSync('/run/secrets/key.pem') ? '/run/secrets/key.pem' : null) || config.sslkeypath || ''
var sslcertpath = (fs.existsSync('/run/secrets/cert.pem') ? '/run/secrets/cert.pem' : null) || config.sslcertpath || ''
var sslcapath = (fs.existsSync('/run/secrets/ca.pem') ? '/run/secrets/ca.pem' : null) || config.sslcapath || ''
var dhparampath = (fs.existsSync('/run/secrets/dhparam.pem') ? '/run/secrets/dhparam.pem' : null) || config.dhparampath || ''
// other path
var tmppath = config.tmppath || './tmp';
var defaultnotepath = config.defaultnotepath || './public/default.md';
var docspath = config.docspath || './public/docs';
var indexpath = config.indexpath || './public/views/index.ejs';
var hackmdpath = config.hackmdpath || './public/views/hackmd.ejs';
var errorpath = config.errorpath || './public/views/error.ejs';
var prettypath = config.prettypath || './public/views/pretty.ejs';
var slidepath = config.slidepath || './public/views/slide.ejs';
var tmppath = config.tmppath || './tmp'
var defaultnotepath = config.defaultnotepath || './public/default.md'
var docspath = config.docspath || './public/docs'
var indexpath = config.indexpath || './public/views/index.ejs'
var hackmdpath = config.hackmdpath || './public/views/hackmd.ejs'
var errorpath = config.errorpath || './public/views/error.ejs'
var prettypath = config.prettypath || './public/views/pretty.ejs'
var slidepath = config.slidepath || './public/views/slide.ejs'
// session
var sessionname = config.sessionname || 'connect.sid';
var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret';
var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000; //14 days
var sessionname = config.sessionname || 'connect.sid'
var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret'
var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000 // 14 days
// static files
var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000; // 1 day
var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000 // 1 day
// socket.io
var heartbeatinterval = config.heartbeatinterval || 5000;
var heartbeattimeout = config.heartbeattimeout || 10000;
var heartbeatinterval = config.heartbeatinterval || 5000
var heartbeattimeout = config.heartbeattimeout || 10000
// document
var documentmaxlength = config.documentmaxlength || 100000;
var documentmaxlength = config.documentmaxlength || 100000
// image upload setting, available options are imgur/s3/filesystem
var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur';
var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur'
config.s3 = config.s3 || {};
config.s3 = config.s3 || {}
var s3 = {
accessKeyId: handleDockerSecret('s3_acccessKeyId') || process.env.HMD_S3_ACCESS_KEY_ID || config.s3.accessKeyId,
secretAccessKey: handleDockerSecret('s3_secretAccessKey') || process.env.HMD_S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey,
region: process.env.HMD_S3_REGION || config.s3.region
}
var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket;
var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket
// auth
var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET || fs.existsSync('/run/secrets/facebook_clientID') && fs.existsSync('/run/secrets/facebook_clientSecret')) ? {
var facebook = ((process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) || (fs.existsSync('/run/secrets/facebook_clientID') && fs.existsSync('/run/secrets/facebook_clientSecret'))) ? {
clientID: handleDockerSecret('facebook_clientID') || process.env.HMD_FACEBOOK_CLIENTID,
clientSecret: handleDockerSecret('facebook_clientSecret') || process.env.HMD_FACEBOOK_CLIENTSECRET
} : config.facebook || false;
var twitter = (process.env.HMD_TWITTER_CONSUMERKEY && process.env.HMD_TWITTER_CONSUMERSECRET || fs.existsSync('/run/secrets/twitter_consumerKey') && fs.existsSync('/run/secrets/twitter_consumerSecret')) ? {
} : config.facebook || false
var twitter = ((process.env.HMD_TWITTER_CONSUMERKEY && process.env.HMD_TWITTER_CONSUMERSECRET) || (fs.existsSync('/run/secrets/twitter_consumerKey') && fs.existsSync('/run/secrets/twitter_consumerSecret'))) ? {
consumerKey: handleDockerSecret('twitter_consumerKey') || process.env.HMD_TWITTER_CONSUMERKEY,
consumerSecret: handleDockerSecret('twitter_consumerSecret') || process.env.HMD_TWITTER_CONSUMERSECRET
} : config.twitter || false;
var github = (process.env.HMD_GITHUB_CLIENTID && process.env.HMD_GITHUB_CLIENTSECRET || fs.existsSync('/run/secrets/github_clientID') && fs.existsSync('/run/secrets/github_clientSecret')) ? {
} : config.twitter || false
var github = ((process.env.HMD_GITHUB_CLIENTID && process.env.HMD_GITHUB_CLIENTSECRET) || (fs.existsSync('/run/secrets/github_clientID') && fs.existsSync('/run/secrets/github_clientSecret'))) ? {
clientID: handleDockerSecret('github_clientID') || process.env.HMD_GITHUB_CLIENTID,
clientSecret: handleDockerSecret('github_clientSecret') || process.env.HMD_GITHUB_CLIENTSECRET
} : config.github || false;
var gitlab = (process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET || fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret')) ? {
} : config.github || false
var gitlab = ((process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET) || (fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret'))) ? {
baseURL: process.env.HMD_GITLAB_BASEURL,
clientID: handleDockerSecret('gitlab_clientID') || process.env.HMD_GITLAB_CLIENTID,
clientSecret: handleDockerSecret('gitlab_clientSecret') || process.env.HMD_GITLAB_CLIENTSECRET
} : config.gitlab || false;
} : config.gitlab || false
var dropbox = ((process.env.HMD_DROPBOX_CLIENTID && process.env.HMD_DROPBOX_CLIENTSECRET) || (fs.existsSync('/run/secrets/dropbox_clientID') && fs.existsSync('/run/secrets/dropbox_clientSecret'))) ? {
clientID: handleDockerSecret('dropbox_clientID') || process.env.HMD_DROPBOX_CLIENTID,
clientSecret: handleDockerSecret('dropbox_clientSecret') || process.env.HMD_DROPBOX_CLIENTSECRET
} : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false;
var google = ((process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSECRET)
|| (fs.existsSync('/run/secrets/google_clientID') && fs.existsSync('/run/secrets/google_clientSecret'))) ? {
} : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false
var google = ((process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSECRET) ||
(fs.existsSync('/run/secrets/google_clientID') && fs.existsSync('/run/secrets/google_clientSecret'))) ? {
clientID: handleDockerSecret('google_clientID') || process.env.HMD_GOOGLE_CLIENTID,
clientSecret: handleDockerSecret('google_clientSecret') || process.env.HMD_GOOGLE_CLIENTSECRET
} : (config.google && config.google.clientID && config.google.clientSecret && config.google) || false;
} : (config.google && config.google.clientID && config.google.clientSecret && config.google) || false
var ldap = config.ldap || ((
process.env.HMD_LDAP_URL ||
process.env.HMD_LDAP_BINDDN ||
@ -123,59 +122,50 @@ var ldap = config.ldap || ((
process.env.HMD_LDAP_SEARCHATTRIBUTES ||
process.env.HMD_LDAP_TLS_CA ||
process.env.HMD_LDAP_PROVIDERNAME
) ? {} : false);
if (process.env.HMD_LDAP_URL)
ldap.url = process.env.HMD_LDAP_URL;
if (process.env.HMD_LDAP_BINDDN)
ldap.bindDn = process.env.HMD_LDAP_BINDDN;
if (process.env.HMD_LDAP_BINDCREDENTIALS)
ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS;
if (process.env.HMD_LDAP_TOKENSECRET)
ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET;
if (process.env.HMD_LDAP_SEARCHBASE)
ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE;
if (process.env.HMD_LDAP_SEARCHFILTER)
ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER;
if (process.env.HMD_LDAP_SEARCHATTRIBUTES)
ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES;
) ? {} : false)
if (process.env.HMD_LDAP_URL) { ldap.url = process.env.HMD_LDAP_URL }
if (process.env.HMD_LDAP_BINDDN) { ldap.bindDn = process.env.HMD_LDAP_BINDDN }
if (process.env.HMD_LDAP_BINDCREDENTIALS) { ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS }
if (process.env.HMD_LDAP_TOKENSECRET) { ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET }
if (process.env.HMD_LDAP_SEARCHBASE) { ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE }
if (process.env.HMD_LDAP_SEARCHFILTER) { ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER }
if (process.env.HMD_LDAP_SEARCHATTRIBUTES) { ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES }
if (process.env.HMD_LDAP_TLS_CA) {
var ca = {
ca: process.env.HMD_LDAP_TLS_CA.split(',')
}
ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca;
ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca
if (Array.isArray(ldap.tlsOptions.ca) && ldap.tlsOptions.ca.length > 0) {
var i, len, results;
results = [];
var i, len, results
results = []
for (i = 0, len = ldap.tlsOptions.ca.length; i < len; i++) {
results.push(fs.readFileSync(ldap.tlsOptions.ca[i], 'utf8'));
results.push(fs.readFileSync(ldap.tlsOptions.ca[i], 'utf8'))
}
ldap.tlsOptions.ca = results;
ldap.tlsOptions.ca = results
}
}
if (process.env.HMD_LDAP_PROVIDERNAME) {
ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME;
ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME
}
var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false;
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email;
var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true);
var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email
var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true)
function getserverurl () {
var url = '';
var url = ''
if (domain) {
var protocol = protocolusessl ? 'https://' : 'http://';
url = protocol + domain;
if (urladdport && ((usessl && port != 443) || (!usessl && port != 80)))
url += ':' + port;
var protocol = protocolusessl ? 'https://' : 'http://'
url = protocol + domain
if (urladdport && ((usessl && port !== 443) || (!usessl && port !== 80))) { url += ':' + port }
}
if (urlpath)
url += '/' + urlpath;
return url;
if (urlpath) { url += '/' + urlpath }
return url
}
var version = '0.5.0';
var minimumCompatibleVersion = '0.5.0';
var maintenance = true;
var cwd = path.join(__dirname, '..');
var version = '0.5.0'
var minimumCompatibleVersion = '0.5.0'
var maintenance = true
var cwd = path.join(__dirname, '..')
module.exports = {
version: version,
@ -225,4 +215,4 @@ module.exports = {
imageUploadType: imageUploadType,
s3: s3,
s3bucket: s3bucket
};
}

View file

@ -1,12 +1,11 @@
// history
// external modules
var async = require('async');
// core
var config = require("./config.js");
var logger = require("./logger.js");
var response = require("./response.js");
var models = require("./models");
var config = require('./config.js')
var logger = require('./logger.js')
var response = require('./response.js')
var models = require('./models')
// public
var History = {
@ -14,7 +13,7 @@ var History = {
historyPost: historyPost,
historyDelete: historyDelete,
updateHistory: updateHistory
};
}
function getHistory (userid, callback) {
models.User.findOne({
@ -22,18 +21,21 @@ function getHistory(userid, callback) {
id: userid
}
}).then(function (user) {
if (!user)
return callback(null, null);
var history = {};
if (user.history)
history = parseHistoryToObject(JSON.parse(user.history));
if (config.debug)
logger.info('read history success: ' + user.id);
return callback(null, history);
if (!user) {
return callback(null, null)
}
var history = {}
if (user.history) {
history = parseHistoryToObject(JSON.parse(user.history))
}
if (config.debug) {
logger.info('read history success: ' + user.id)
}
return callback(null, history)
}).catch(function (err) {
logger.error('read history failed: ' + err);
return callback(err, null);
});
logger.error('read history failed: ' + err)
return callback(err, null)
})
}
function setHistory (userid, history, callback) {
@ -44,129 +46,130 @@ function setHistory(userid, history, callback) {
id: userid
}
}).then(function (count) {
return callback(null, count);
return callback(null, count)
}).catch(function (err) {
logger.error('set history failed: ' + err);
return callback(err, null);
});
logger.error('set history failed: ' + err)
return callback(err, null)
})
}
function updateHistory (userid, noteId, document, time) {
if (userid && noteId && typeof document !== 'undefined') {
getHistory(userid, function (err, history) {
if (err || !history) return;
if (err || !history) return
if (!history[noteId]) {
history[noteId] = {};
history[noteId] = {}
}
var noteHistory = history[noteId];
var noteInfo = models.Note.parseNoteInfo(document);
noteHistory.id = noteId;
noteHistory.text = noteInfo.title;
noteHistory.time = time || Date.now();
noteHistory.tags = noteInfo.tags;
var noteHistory = history[noteId]
var noteInfo = models.Note.parseNoteInfo(document)
noteHistory.id = noteId
noteHistory.text = noteInfo.title
noteHistory.time = time || Date.now()
noteHistory.tags = noteInfo.tags
setHistory(userid, history, function (err, count) {
return;
});
});
if (err) {
logger.log(err)
}
})
})
}
}
function parseHistoryToArray (history) {
var _history = [];
var _history = []
Object.keys(history).forEach(function (key) {
var item = history[key];
_history.push(item);
});
return _history;
var item = history[key]
_history.push(item)
})
return _history
}
function parseHistoryToObject (history) {
var _history = {};
var _history = {}
for (var i = 0, l = history.length; i < l; i++) {
var item = history[i];
_history[item.id] = item;
var item = history[i]
_history[item.id] = item
}
return _history;
return _history
}
function historyGet (req, res) {
if (req.isAuthenticated()) {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
res.send({
history: parseHistoryToArray(history)
});
});
})
})
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
}
function historyPost (req, res) {
if (req.isAuthenticated()) {
var noteId = req.params.noteId;
var noteId = req.params.noteId
if (!noteId) {
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res);
if (config.debug)
logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history);
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res)
if (config.debug) { logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history) }
try {
var history = JSON.parse(req.body.history);
var history = JSON.parse(req.body.history)
} catch (err) {
return response.errorBadRequest(res);
return response.errorBadRequest(res)
}
if (Array.isArray(history)) {
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
if (err) return response.errorInternalError(res)
res.end()
})
} else {
return response.errorBadRequest(res);
return response.errorBadRequest(res)
}
} else {
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res);
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res)
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
if (!history[noteId]) return response.errorNotFound(res);
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
if (!history[noteId]) return response.errorNotFound(res)
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
history[noteId].pinned = (req.body.pinned === 'true');
history[noteId].pinned = (req.body.pinned === 'true')
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
if (err) return response.errorInternalError(res)
res.end()
})
} else {
return response.errorBadRequest(res);
return response.errorBadRequest(res)
}
});
})
}
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
}
function historyDelete (req, res) {
if (req.isAuthenticated()) {
var noteId = req.params.noteId;
var noteId = req.params.noteId
if (!noteId) {
setHistory(req.user.id, [], function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
if (err) return response.errorInternalError(res)
res.end()
})
} else {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
delete history[noteId];
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
delete history[noteId]
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
});
if (err) return response.errorInternalError(res)
res.end()
})
})
}
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
}
module.exports = History;
module.exports = History

View file

@ -1,25 +1,23 @@
"use strict";
// external modules
var randomcolor = require('randomcolor');
var randomcolor = require('randomcolor')
// core
module.exports = function (name) {
var color = randomcolor({
seed: name,
luminosity: 'dark'
});
var letter = name.substring(0, 1).toUpperCase();
})
var letter = name.substring(0, 1).toUpperCase()
var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">';
svg += '<g>';
svg += '<rect width="96" height="96" fill="' + color + '" />';
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">';
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>';
svg += '</text>';
svg += '</g>';
svg += '</svg>';
var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">'
svg += '<g>'
svg += '<rect width="96" height="96" fill="' + color + '" />'
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">'
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>'
svg += '</text>'
svg += '</g>'
svg += '</svg>'
return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64');
};
return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64')
}

View file

@ -1,5 +1,5 @@
var winston = require('winston');
winston.emitErrs = true;
var winston = require('winston')
winston.emitErrs = true
var logger = new winston.Logger({
transports: [
@ -12,11 +12,11 @@ var logger = new winston.Logger({
})
],
exitOnError: false
});
})
module.exports = logger;
module.exports = logger
module.exports.stream = {
write: function (message, encoding) {
logger.info(message);
logger.info(message)
}
}
};

View file

@ -1,15 +1,11 @@
"use strict";
module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Users', 'accessToken', Sequelize.STRING);
queryInterface.addColumn('Users', 'refreshToken', Sequelize.STRING);
return;
queryInterface.addColumn('Users', 'accessToken', Sequelize.STRING)
queryInterface.addColumn('Users', 'refreshToken', Sequelize.STRING)
},
down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Users', 'accessToken');
queryInterface.removeColumn('Users', 'refreshToken');
return;
queryInterface.removeColumn('Users', 'accessToken')
queryInterface.removeColumn('Users', 'refreshToken')
}
}
};

View file

@ -1,8 +1,6 @@
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'savedAt', Sequelize.DATE);
queryInterface.addColumn('Notes', 'savedAt', Sequelize.DATE)
queryInterface.createTable('Revisions', {
id: {
type: Sequelize.UUID,
@ -15,13 +13,11 @@ module.exports = {
length: Sequelize.INTEGER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE
});
return;
})
},
down: function (queryInterface, Sequelize) {
queryInterface.dropTable('Revisions');
queryInterface.removeColumn('Notes', 'savedAt');
return;
queryInterface.dropTable('Revisions')
queryInterface.removeColumn('Notes', 'savedAt')
}
}
};

View file

@ -1,9 +1,7 @@
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'authorship', Sequelize.TEXT);
queryInterface.addColumn('Revisions', 'authorship', Sequelize.TEXT);
queryInterface.addColumn('Notes', 'authorship', Sequelize.TEXT)
queryInterface.addColumn('Revisions', 'authorship', Sequelize.TEXT)
queryInterface.createTable('Authors', {
id: {
type: Sequelize.INTEGER,
@ -15,14 +13,12 @@ module.exports = {
userId: Sequelize.UUID,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE
});
return;
})
},
down: function (queryInterface, Sequelize) {
queryInterface.dropTable('Authors');
queryInterface.removeColumn('Revisions', 'authorship');
queryInterface.removeColumn('Notes', 'authorship');
return;
queryInterface.dropTable('Authors')
queryInterface.removeColumn('Revisions', 'authorship')
queryInterface.removeColumn('Notes', 'authorship')
}
}
};

View file

@ -1,11 +1,9 @@
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE);
queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE)
},
down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Notes', 'deletedAt');
queryInterface.removeColumn('Notes', 'deletedAt')
}
}
};

View file

@ -1,13 +1,11 @@
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Users', 'email', Sequelize.TEXT);
queryInterface.addColumn('Users', 'password', Sequelize.TEXT);
queryInterface.addColumn('Users', 'email', Sequelize.TEXT)
queryInterface.addColumn('Users', 'password', Sequelize.TEXT)
},
down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Users', 'email');
queryInterface.removeColumn('Users', 'password');
queryInterface.removeColumn('Users', 'email')
queryInterface.removeColumn('Users', 'password')
}
}
};

View file

@ -1,13 +1,8 @@
"use strict";
// external modules
var Sequelize = require("sequelize");
// core
var logger = require("../logger.js");
var Sequelize = require('sequelize')
module.exports = function (sequelize, DataTypes) {
var Author = sequelize.define("Author", {
var Author = sequelize.define('Author', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
@ -26,18 +21,17 @@ module.exports = function (sequelize, DataTypes) {
classMethods: {
associate: function (models) {
Author.belongsTo(models.Note, {
foreignKey: "noteId",
as: "note",
foreignKey: 'noteId',
as: 'note',
constraints: false
});
})
Author.belongsTo(models.User, {
foreignKey: "userId",
as: "user",
foreignKey: 'userId',
as: 'user',
constraints: false
});
})
}
}
});
return Author;
};
})
return Author
}

View file

@ -1,57 +1,55 @@
"use strict";
// external modules
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var fs = require('fs')
var path = require('path')
var Sequelize = require('sequelize')
// core
var config = require('../config.js');
var logger = require("../logger.js");
var config = require('../config.js')
var logger = require('../logger.js')
var dbconfig = config.db;
dbconfig.logging = config.debug ? logger.info : false;
var dbconfig = config.db
dbconfig.logging = config.debug ? logger.info : false
var sequelize = null;
var sequelize = null
// Heroku specific
if (config.dburl)
sequelize = new Sequelize(config.dburl, dbconfig);
else
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig);
if (config.dburl) {
sequelize = new Sequelize(config.dburl, dbconfig)
} else {
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
}
// [Postgres] Handling NULL bytes
// https://github.com/sequelize/sequelize/issues/6485
function stripNullByte (value) {
return value ? value.replace(/\u0000/g, "") : value;
return value ? value.replace(/\u0000/g, '') : value
}
sequelize.stripNullByte = stripNullByte;
sequelize.stripNullByte = stripNullByte
function processData (data, _default, process) {
if (data === undefined) return data;
else return data === null ? _default : (process ? process(data) : data);
if (data === undefined) return data
else return data === null ? _default : (process ? process(data) : data)
}
sequelize.processData = processData;
sequelize.processData = processData
var db = {};
var db = {}
fs
.readdirSync(__dirname)
fs.readdirSync(__dirname)
.filter(function (file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
return (file.indexOf('.') !== 0) && (file !== 'index.js')
})
.forEach(function (file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
var model = sequelize.import(path.join(__dirname, file))
db[model.name] = model
})
Object.keys(db).forEach(function (modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
if ('associate' in db[modelName]) {
db[modelName].associate(db)
}
});
})
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db;
module.exports = db

View file

@ -1,32 +1,30 @@
"use strict";
// external modules
var fs = require('fs');
var path = require('path');
var LZString = require('lz-string');
var md = require('markdown-it')();
var metaMarked = require('meta-marked');
var cheerio = require('cheerio');
var shortId = require('shortid');
var Sequelize = require("sequelize");
var async = require('async');
var moment = require('moment');
var DiffMatchPatch = require('diff-match-patch');
var dmp = new DiffMatchPatch();
var S = require('string');
var fs = require('fs')
var path = require('path')
var LZString = require('lz-string')
var md = require('markdown-it')()
var metaMarked = require('meta-marked')
var cheerio = require('cheerio')
var shortId = require('shortid')
var Sequelize = require('sequelize')
var async = require('async')
var moment = require('moment')
var DiffMatchPatch = require('diff-match-patch')
var dmp = new DiffMatchPatch()
var S = require('string')
// core
var config = require("../config.js");
var logger = require("../logger.js");
var config = require('../config.js')
var logger = require('../logger.js')
// ot
var ot = require("../ot/index.js");
var ot = require('../ot/index.js')
// permission types
var permissionTypes = ["freely", "editable", "limited", "locked", "protected", "private"];
var permissionTypes = ['freely', 'editable', 'limited', 'locked', 'protected', 'private']
module.exports = function (sequelize, DataTypes) {
var Note = sequelize.define("Note", {
var Note = sequelize.define('Note', {
id: {
type: DataTypes.UUID,
primaryKey: true,
@ -54,28 +52,28 @@ module.exports = function (sequelize, DataTypes) {
title: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('title'), "");
return sequelize.processData(this.getDataValue('title'), '')
},
set: function (value) {
this.setDataValue('title', sequelize.stripNullByte(value));
this.setDataValue('title', sequelize.stripNullByte(value))
}
},
content: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('content'), "");
return sequelize.processData(this.getDataValue('content'), '')
},
set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value));
this.setDataValue('content', sequelize.stripNullByte(value))
}
},
authorship: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
},
set: function (value) {
this.setDataValue('authorship', JSON.stringify(value));
this.setDataValue('authorship', JSON.stringify(value))
}
},
lastchangeAt: {
@ -89,39 +87,36 @@ module.exports = function (sequelize, DataTypes) {
classMethods: {
associate: function (models) {
Note.belongsTo(models.User, {
foreignKey: "ownerId",
as: "owner",
foreignKey: 'ownerId',
as: 'owner',
constraints: false
});
})
Note.belongsTo(models.User, {
foreignKey: "lastchangeuserId",
as: "lastchangeuser",
foreignKey: 'lastchangeuserId',
as: 'lastchangeuser',
constraints: false
});
})
Note.hasMany(models.Revision, {
foreignKey: "noteId",
foreignKey: 'noteId',
constraints: false
});
})
Note.hasMany(models.Author, {
foreignKey: "noteId",
as: "authors",
foreignKey: 'noteId',
as: 'authors',
constraints: false
});
})
},
checkFileExist: function (filePath) {
try {
return fs.statSync(filePath).isFile();
return fs.statSync(filePath).isFile()
} catch (err) {
return false;
return false
}
},
checkNoteIdValid: function (id) {
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
var result = id.match(uuidRegex);
if (result && result.length == 1)
return true;
else
return false;
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
var result = id.match(uuidRegex)
if (result && result.length === 1) { return true } else { return false }
},
parseNoteId: function (noteId, callback) {
async.series({
@ -133,15 +128,15 @@ module.exports = function (sequelize, DataTypes) {
}
}).then(function (note) {
if (note) {
var filePath = path.join(config.docspath, noteId + '.md');
let filePath = path.join(config.docspath, noteId + '.md')
if (Note.checkFileExist(filePath)) {
// if doc in filesystem have newer modified time than last change time
// then will update the doc in db
var fsModifiedTime = moment(fs.statSync(filePath).mtime);
var dbModifiedTime = moment(note.lastchangeAt || note.createdAt);
var body = fs.readFileSync(filePath, 'utf8');
var contentLength = body.length;
var title = Note.parseNoteTitle(body);
var fsModifiedTime = moment(fs.statSync(filePath).mtime)
var dbModifiedTime = moment(note.lastchangeAt || note.createdAt)
var body = fs.readFileSync(filePath, 'utf8')
var contentLength = body.length
var title = Note.parseNoteTitle(body)
if (fsModifiedTime.isAfter(dbModifiedTime) && note.content !== body) {
note.update({
title: title,
@ -149,61 +144,58 @@ module.exports = function (sequelize, DataTypes) {
lastchangeAt: fsModifiedTime
}).then(function (note) {
sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
if (err) return _callback(err, null);
if (err) return _callback(err, null)
// update authorship on after making revision of docs
var patch = dmp.patch_fromText(revision.patch);
var operations = Note.transformPatchToOperations(patch, contentLength);
var authorship = note.authorship;
for (var i = 0; i < operations.length; i++) {
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship);
var patch = dmp.patch_fromText(revision.patch)
var operations = Note.transformPatchToOperations(patch, contentLength)
var authorship = note.authorship
for (let i = 0; i < operations.length; i++) {
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship)
}
note.update({
authorship: JSON.stringify(authorship)
}).then(function (note) {
return callback(null, note.id);
return callback(null, note.id)
}).catch(function (err) {
return _callback(err, null);
});
});
return _callback(err, null)
})
})
}).catch(function (err) {
return _callback(err, null);
});
return _callback(err, null)
})
} else {
return callback(null, note.id);
return callback(null, note.id)
}
} else {
return callback(null, note.id);
return callback(null, note.id)
}
} else {
var filePath = path.join(config.docspath, noteId + '.md');
var filePath = path.join(config.docspath, noteId + '.md')
if (Note.checkFileExist(filePath)) {
Note.create({
alias: noteId,
owner: null,
permission: 'locked'
}).then(function (note) {
return callback(null, note.id);
return callback(null, note.id)
}).catch(function (err) {
return _callback(err, null);
});
return _callback(err, null)
})
} else {
return _callback(null, null);
return _callback(null, null)
}
}
}).catch(function (err) {
return _callback(err, null);
});
return _callback(err, null)
})
},
parseNoteIdByLZString: function (_callback) {
// try to parse note id by LZString Base64
try {
var id = LZString.decompressFromBase64(noteId);
if (id && Note.checkNoteIdValid(id))
return callback(null, id);
else
return _callback(null, null);
var id = LZString.decompressFromBase64(noteId)
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
} catch (err) {
return _callback(err, null);
return _callback(err, null)
}
},
parseNoteIdByShortId: function (_callback) {
@ -215,321 +207,318 @@ module.exports = function (sequelize, DataTypes) {
shortid: noteId
}
}).then(function (note) {
if (!note) return _callback(null, null);
return callback(null, note.id);
if (!note) return _callback(null, null)
return callback(null, note.id)
}).catch(function (err) {
return _callback(err, null);
});
return _callback(err, null)
})
} else {
return _callback(null, null);
return _callback(null, null)
}
} catch (err) {
return _callback(err, null);
return _callback(err, null)
}
}
}, function (err, result) {
if (err) {
logger.error(err);
return callback(err, null);
logger.error(err)
return callback(err, null)
}
return callback(null, null);
});
return callback(null, null)
})
},
parseNoteInfo: function (body) {
var parsed = Note.extractMeta(body);
var $ = cheerio.load(md.render(parsed.markdown));
var parsed = Note.extractMeta(body)
var $ = cheerio.load(md.render(parsed.markdown))
return {
title: Note.extractNoteTitle(parsed.meta, $),
tags: Note.extractNoteTags(parsed.meta, $)
};
}
},
parseNoteTitle: function (body) {
var parsed = Note.extractMeta(body);
var $ = cheerio.load(md.render(parsed.markdown));
return Note.extractNoteTitle(parsed.meta, $);
var parsed = Note.extractMeta(body)
var $ = cheerio.load(md.render(parsed.markdown))
return Note.extractNoteTitle(parsed.meta, $)
},
extractNoteTitle: function (meta, $) {
var title = "";
if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) {
title = meta.title;
var title = ''
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
title = meta.title
} else {
var h1s = $("h1");
if (h1s.length > 0 && h1s.first().text().split('\n').length == 1)
title = S(h1s.first().text()).stripTags().s;
var h1s = $('h1')
if (h1s.length > 0 && h1s.first().text().split('\n').length === 1) { title = S(h1s.first().text()).stripTags().s }
}
if (!title) title = "Untitled";
return title;
if (!title) title = 'Untitled'
return title
},
generateDescription: function (markdown) {
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ');
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ')
},
decodeTitle: function (title) {
return title ? title : 'Untitled';
return title || 'Untitled'
},
generateWebTitle: function (title) {
title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD";
return title;
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);
var tags = []
var rawtags = []
if (meta.tags && (typeof meta.tags === 'string' || typeof meta.tags === 'number')) {
var metaTags = ('' + meta.tags).split(',')
for (let i = 0; i < metaTags.length; i++) {
var text = metaTags[i].trim()
if (text) rawtags.push(text)
}
} else {
var h6s = $("h6");
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 = S($(codes[i]).text().trim()).stripTags().s;
if (text) rawtags.push(text);
var codes = $(value).find('code')
for (let i = 0; i < codes.length; i++) {
var text = S($(codes[i]).text().trim()).stripTags().s
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;
for (let i = 0; i < rawtags.length; i++) {
var found = false
for (let j = 0; j < tags.length; j++) {
if (tags[j] === rawtags[i]) {
found = true
break
}
}
if (!found)
tags.push(rawtags[i]);
if (!found) { tags.push(rawtags[i]) }
}
return tags;
return tags
},
extractMeta: function (content) {
var obj = null
try {
var obj = metaMarked(content);
if (!obj.markdown) obj.markdown = "";
if (!obj.meta) obj.meta = {};
obj = metaMarked(content)
if (!obj.markdown) obj.markdown = ''
if (!obj.meta) obj.meta = {}
} catch (err) {
var obj = {
obj = {
markdown: content,
meta: {}
};
}
return obj;
}
return obj
},
parseMeta: function (meta) {
var _meta = {};
var _meta = {}
if (meta) {
if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number"))
_meta.title = meta.title;
if (meta.description && (typeof meta.description == "string" || typeof meta.description == "number"))
_meta.description = meta.description;
if (meta.robots && (typeof meta.robots == "string" || typeof meta.robots == "number"))
_meta.robots = meta.robots;
if (meta.GA && (typeof meta.GA == "string" || typeof meta.GA == "number"))
_meta.GA = meta.GA;
if (meta.disqus && (typeof meta.disqus == "string" || typeof meta.disqus == "number"))
_meta.disqus = meta.disqus;
if (meta.slideOptions && (typeof meta.slideOptions == "object"))
_meta.slideOptions = meta.slideOptions;
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) { _meta.title = meta.title }
if (meta.description && (typeof meta.description === 'string' || typeof meta.description === 'number')) { _meta.description = meta.description }
if (meta.robots && (typeof meta.robots === 'string' || typeof meta.robots === 'number')) { _meta.robots = meta.robots }
if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) { _meta.GA = meta.GA }
if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) { _meta.disqus = meta.disqus }
if (meta.slideOptions && (typeof meta.slideOptions === 'object')) { _meta.slideOptions = meta.slideOptions }
}
return _meta;
return _meta
},
updateAuthorshipByOperation: function (operation, userId, authorships) {
var index = 0;
var timestamp = Date.now();
for (var i = 0; i < operation.length; i++) {
var op = operation[i];
var index = 0
var timestamp = Date.now()
for (let i = 0; i < operation.length; i++) {
var op = operation[i]
if (ot.TextOperation.isRetain(op)) {
index += op;
index += op
} else if (ot.TextOperation.isInsert(op)) {
var opStart = index;
var opEnd = index + op.length;
var inserted = false;
let opStart = index
let opEnd = index + op.length
var inserted = false
// authorship format: [userId, startPos, endPos, createdAt, updatedAt]
if (authorships.length <= 0) authorships.push([userId, opStart, opEnd, timestamp, timestamp]);
if (authorships.length <= 0) authorships.push([userId, opStart, opEnd, timestamp, timestamp])
else {
for (var j = 0; j < authorships.length; j++) {
var authorship = authorships[j];
for (let j = 0; j < authorships.length; j++) {
let authorship = authorships[j]
if (!inserted) {
var nextAuthorship = authorships[j + 1] || -1;
if (nextAuthorship != -1 && nextAuthorship[1] >= opEnd || j >= authorships.length - 1) {
let nextAuthorship = authorships[j + 1] || -1
if ((nextAuthorship !== -1 && nextAuthorship[1] >= opEnd) || j >= authorships.length - 1) {
if (authorship[1] < opStart && authorship[2] > opStart) {
// divide
var postLength = authorship[2] - opStart;
authorship[2] = opStart;
authorship[4] = timestamp;
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp]);
authorships.splice(j + 2, 0, [authorship[0], opEnd, opEnd + postLength, authorship[3], timestamp]);
j += 2;
inserted = true;
let postLength = authorship[2] - opStart
authorship[2] = opStart
authorship[4] = timestamp
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp])
authorships.splice(j + 2, 0, [authorship[0], opEnd, opEnd + postLength, authorship[3], timestamp])
j += 2
inserted = true
} else if (authorship[1] >= opStart) {
authorships.splice(j, 0, [userId, opStart, opEnd, timestamp, timestamp]);
j += 1;
inserted = true;
authorships.splice(j, 0, [userId, opStart, opEnd, timestamp, timestamp])
j += 1
inserted = true
} else if (authorship[2] <= opStart) {
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp]);
j += 1;
inserted = true;
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp])
j += 1
inserted = true
}
}
}
if (authorship[1] >= opStart) {
authorship[1] += op.length;
authorship[2] += op.length;
authorship[1] += op.length
authorship[2] += op.length
}
}
}
index += op.length;
index += op.length
} else if (ot.TextOperation.isDelete(op)) {
var opStart = index;
var opEnd = index - op;
if (operation.length == 1) {
authorships = [];
let opStart = index
let opEnd = index - op
if (operation.length === 1) {
authorships = []
} else if (authorships.length > 0) {
for (var j = 0; j < authorships.length; j++) {
var authorship = authorships[j];
for (let j = 0; j < authorships.length; j++) {
let authorship = authorships[j]
if (authorship[1] >= opStart && authorship[1] <= opEnd && authorship[2] >= opStart && authorship[2] <= opEnd) {
authorships.splice(j, 1);
j -= 1;
authorships.splice(j, 1)
j -= 1
} else if (authorship[1] < opStart && authorship[1] < opEnd && authorship[2] > opStart && authorship[2] > opEnd) {
authorship[2] += op;
authorship[4] = timestamp;
authorship[2] += op
authorship[4] = timestamp
} else if (authorship[2] >= opStart && authorship[2] <= opEnd) {
authorship[2] = opStart;
authorship[4] = timestamp;
authorship[2] = opStart
authorship[4] = timestamp
} else if (authorship[1] >= opStart && authorship[1] <= opEnd) {
authorship[1] = opEnd;
authorship[4] = timestamp;
authorship[1] = opEnd
authorship[4] = timestamp
}
if (authorship[1] >= opEnd) {
authorship[1] += op;
authorship[2] += op;
authorship[1] += op
authorship[2] += op
}
}
}
index += op;
index += op
}
}
// merge
for (var j = 0; j < authorships.length; j++) {
var authorship = authorships[j];
for (var k = j + 1; k < authorships.length; k++) {
var nextAuthorship = authorships[k];
for (let j = 0; j < authorships.length; j++) {
let authorship = authorships[j]
for (let k = j + 1; k < authorships.length; k++) {
let nextAuthorship = authorships[k]
if (nextAuthorship && authorship[0] === nextAuthorship[0] && authorship[2] === nextAuthorship[1]) {
var minTimestamp = Math.min(authorship[3], nextAuthorship[3]);
var maxTimestamp = Math.max(authorship[3], nextAuthorship[3]);
authorships.splice(j, 1, [authorship[0], authorship[1], nextAuthorship[2], minTimestamp, maxTimestamp]);
authorships.splice(k, 1);
j -= 1;
break;
let minTimestamp = Math.min(authorship[3], nextAuthorship[3])
let maxTimestamp = Math.max(authorship[3], nextAuthorship[3])
authorships.splice(j, 1, [authorship[0], authorship[1], nextAuthorship[2], minTimestamp, maxTimestamp])
authorships.splice(k, 1)
j -= 1
break
}
}
}
// clear
for (var j = 0; j < authorships.length; j++) {
var authorship = authorships[j];
for (let j = 0; j < authorships.length; j++) {
let authorship = authorships[j]
if (!authorship[0]) {
authorships.splice(j, 1);
j -= 1;
authorships.splice(j, 1)
j -= 1
}
}
return authorships;
return authorships
},
transformPatchToOperations: function (patch, contentLength) {
var operations = [];
var operations = []
if (patch.length > 0) {
// calculate original content length
for (var j = patch.length - 1; j >= 0; j--) {
var p = patch[j];
for (var i = 0; i < p.diffs.length; i++) {
var diff = p.diffs[i];
for (let j = patch.length - 1; j >= 0; j--) {
var p = patch[j]
for (let i = 0; i < p.diffs.length; i++) {
var diff = p.diffs[i]
switch (diff[0]) {
case 1: // insert
contentLength -= diff[1].length;
break;
contentLength -= diff[1].length
break
case -1: // delete
contentLength += diff[1].length;
break;
contentLength += diff[1].length
break
}
}
}
// generate operations
var bias = 0;
var lengthBias = 0;
for (var j = 0; j < patch.length; j++) {
var operation = [];
var p = patch[j];
var currIndex = p.start1;
var currLength = contentLength - bias;
for (var i = 0; i < p.diffs.length; i++) {
var diff = p.diffs[i];
var bias = 0
var lengthBias = 0
for (let j = 0; j < patch.length; j++) {
var operation = []
let p = patch[j]
var currIndex = p.start1
var currLength = contentLength - bias
for (let i = 0; i < p.diffs.length; i++) {
let diff = p.diffs[i]
switch (diff[0]) {
case 0: // retain
if (i == 0) // first
operation.push(currIndex + diff[1].length);
else if (i != p.diffs.length - 1) // mid
operation.push(diff[1].length);
else // last
operation.push(currLength + lengthBias - currIndex);
currIndex += diff[1].length;
break;
if (i === 0) {
// first
operation.push(currIndex + diff[1].length)
} else if (i !== p.diffs.length - 1) {
// mid
operation.push(diff[1].length)
} else {
// last
operation.push(currLength + lengthBias - currIndex)
}
currIndex += diff[1].length
break
case 1: // insert
operation.push(diff[1]);
lengthBias += diff[1].length;
currIndex += diff[1].length;
break;
operation.push(diff[1])
lengthBias += diff[1].length
currIndex += diff[1].length
break
case -1: // delete
operation.push(-diff[1].length);
bias += diff[1].length;
currIndex += diff[1].length;
break;
operation.push(-diff[1].length)
bias += diff[1].length
currIndex += diff[1].length
break
}
}
operations.push(operation);
operations.push(operation)
}
}
return operations;
return operations
}
},
hooks: {
beforeCreate: function (note, options, callback) {
// if no content specified then use default note
if (!note.content) {
var body = null;
var filePath = null;
var body = null
let filePath = null
if (!note.alias) {
filePath = config.defaultnotepath;
filePath = config.defaultnotepath
} else {
filePath = path.join(config.docspath, note.alias + '.md');
filePath = path.join(config.docspath, note.alias + '.md')
}
if (Note.checkFileExist(filePath)) {
var fsCreatedTime = moment(fs.statSync(filePath).ctime);
body = fs.readFileSync(filePath, 'utf8');
note.title = Note.parseNoteTitle(body);
note.content = body;
var fsCreatedTime = moment(fs.statSync(filePath).ctime)
body = fs.readFileSync(filePath, 'utf8')
note.title = Note.parseNoteTitle(body)
note.content = body
if (filePath !== config.defaultnotepath) {
note.createdAt = fsCreatedTime;
note.createdAt = fsCreatedTime
}
}
}
// if no permission specified and have owner then give default permission in config, else default permission is freely
if (!note.permission) {
if (note.ownerId) {
note.permission = config.defaultpermission;
note.permission = config.defaultpermission
} else {
note.permission = "freely";
note.permission = 'freely'
}
}
return callback(null, note);
return callback(null, note)
},
afterCreate: function (note, options, callback) {
sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
callback(err, note);
});
callback(err, note)
})
}
}
});
})
return Note;
};
return Note
}

View file

@ -1,58 +1,56 @@
"use strict";
// external modules
var Sequelize = require("sequelize");
var async = require('async');
var moment = require('moment');
var childProcess = require('child_process');
var shortId = require('shortid');
var Sequelize = require('sequelize')
var async = require('async')
var moment = require('moment')
var childProcess = require('child_process')
var shortId = require('shortid')
// core
var config = require("../config.js");
var logger = require("../logger.js");
var config = require('../config.js')
var logger = require('../logger.js')
var dmpWorker = createDmpWorker();
var dmpCallbackCache = {};
var dmpWorker = createDmpWorker()
var dmpCallbackCache = {}
function createDmpWorker () {
var worker = childProcess.fork("./lib/workers/dmpWorker.js", {
var worker = childProcess.fork('./lib/workers/dmpWorker.js', {
stdio: 'ignore'
});
if (config.debug) logger.info('dmp worker process started');
})
if (config.debug) logger.info('dmp worker process started')
worker.on('message', function (data) {
if (!data || !data.msg || !data.cacheKey) {
return logger.error('dmp worker error: not enough data on message');
return logger.error('dmp worker error: not enough data on message')
}
var cacheKey = data.cacheKey;
var cacheKey = data.cacheKey
switch (data.msg) {
case 'error':
dmpCallbackCache[cacheKey](data.error, null);
break;
dmpCallbackCache[cacheKey](data.error, null)
break
case 'check':
dmpCallbackCache[cacheKey](null, data.result);
break;
dmpCallbackCache[cacheKey](null, data.result)
break
}
delete dmpCallbackCache[cacheKey];
});
delete dmpCallbackCache[cacheKey]
})
worker.on('close', function (code) {
dmpWorker = null;
if (config.debug) logger.info('dmp worker process exited with code ' + code);
});
return worker;
dmpWorker = null
if (config.debug) logger.info('dmp worker process exited with code ' + code)
})
return worker
}
function sendDmpWorker (data, callback) {
if (!dmpWorker) dmpWorker = createDmpWorker();
var cacheKey = Date.now() + '_' + shortId.generate();
dmpCallbackCache[cacheKey] = callback;
if (!dmpWorker) dmpWorker = createDmpWorker()
var cacheKey = Date.now() + '_' + shortId.generate()
dmpCallbackCache[cacheKey] = callback
data = Object.assign(data, {
cacheKey: cacheKey
});
dmpWorker.send(data);
})
dmpWorker.send(data)
}
module.exports = function (sequelize, DataTypes) {
var Revision = sequelize.define("Revision", {
var Revision = sequelize.define('Revision', {
id: {
type: DataTypes.UUID,
primaryKey: true,
@ -61,28 +59,28 @@ module.exports = function (sequelize, DataTypes) {
patch: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('patch'), "");
return sequelize.processData(this.getDataValue('patch'), '')
},
set: function (value) {
this.setDataValue('patch', sequelize.stripNullByte(value));
this.setDataValue('patch', sequelize.stripNullByte(value))
}
},
lastContent: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('lastContent'), "");
return sequelize.processData(this.getDataValue('lastContent'), '')
},
set: function (value) {
this.setDataValue('lastContent', sequelize.stripNullByte(value));
this.setDataValue('lastContent', sequelize.stripNullByte(value))
}
},
content: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('content'), "");
return sequelize.processData(this.getDataValue('content'), '')
},
set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value));
this.setDataValue('content', sequelize.stripNullByte(value))
}
},
length: {
@ -91,20 +89,20 @@ module.exports = function (sequelize, DataTypes) {
authorship: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
},
set: function (value) {
this.setDataValue('authorship', value ? JSON.stringify(value) : value);
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
}
}
}, {
classMethods: {
associate: function (models) {
Revision.belongsTo(models.Note, {
foreignKey: "noteId",
as: "note",
foreignKey: 'noteId',
as: 'note',
constraints: false
});
})
},
getNoteRevisions: function (note, callback) {
Revision.findAll({
@ -113,18 +111,18 @@ module.exports = function (sequelize, DataTypes) {
},
order: '"createdAt" DESC'
}).then(function (revisions) {
var data = [];
var data = []
for (var i = 0, l = revisions.length; i < l; i++) {
var revision = revisions[i];
var revision = revisions[i]
data.push({
time: moment(revision.createdAt).valueOf(),
length: revision.length
});
})
}
callback(null, data);
callback(null, data)
}).catch(function (err) {
callback(err, null);
});
callback(err, null)
})
},
getPatchedNoteRevisionByTime: function (note, time, callback) {
// find all revisions to prepare for all possible calculation
@ -134,7 +132,7 @@ module.exports = function (sequelize, DataTypes) {
},
order: '"createdAt" DESC'
}).then(function (revisions) {
if (revisions.length <= 0) return callback(null, null);
if (revisions.length <= 0) return callback(null, null)
// measure target revision position
Revision.count({
where: {
@ -145,28 +143,28 @@ module.exports = function (sequelize, DataTypes) {
},
order: '"createdAt" DESC'
}).then(function (count) {
if (count <= 0) return callback(null, null);
if (count <= 0) return callback(null, null)
sendDmpWorker({
msg: 'get revision',
revisions: revisions,
count: count
}, callback);
}, callback)
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
},
checkAllNotesRevision: function (callback) {
Revision.saveAllNotesRevision(function (err, notes) {
if (err) return callback(err, null);
if (err) return callback(err, null)
if (!notes || notes.length <= 0) {
return callback(null, notes);
return callback(null, notes)
} else {
Revision.checkAllNotesRevision(callback);
Revision.checkAllNotesRevision(callback)
}
});
})
},
saveAllNotesRevision: function (callback) {
sequelize.models.Note.findAll({
@ -195,35 +193,37 @@ module.exports = function (sequelize, DataTypes) {
]
}
}).then(function (notes) {
if (notes.length <= 0) return callback(null, notes);
var savedNotes = [];
if (notes.length <= 0) return callback(null, notes)
var savedNotes = []
async.each(notes, function (note, _callback) {
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
if (note.lastchangeAt && note.savedAt) {
var lastchangeAt = moment(note.lastchangeAt);
var savedAt = moment(note.savedAt);
var lastchangeAt = moment(note.lastchangeAt)
var savedAt = moment(note.savedAt)
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
savedNotes.push(note)
Revision.saveNoteRevision(note, _callback)
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
savedNotes.push(note)
Revision.saveNoteRevision(note, _callback)
} else {
return _callback(null, null);
return _callback(null, null)
}
} else {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
savedNotes.push(note)
Revision.saveNoteRevision(note, _callback)
}
}, function (err) {
if (err) return callback(err, null);
if (err) {
return callback(err, null)
}
// return null when no notes need saving at this moment but have delayed tasks to be done
var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes;
return callback(null, result);
});
var result = ((savedNotes.length === 0) && (notes.length > savedNotes.length)) ? null : savedNotes
return callback(null, result)
})
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
},
saveNoteRevision: function (note, callback) {
Revision.findAll({
@ -240,30 +240,30 @@ module.exports = function (sequelize, DataTypes) {
length: note.content.length,
authorship: note.authorship
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback);
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
} else {
var latestRevision = revisions[0];
var lastContent = latestRevision.content || latestRevision.lastContent;
var content = note.content;
var latestRevision = revisions[0]
var lastContent = latestRevision.content || latestRevision.lastContent
var content = note.content
sendDmpWorker({
msg: 'create patch',
lastDoc: lastContent,
currDoc: content,
currDoc: content
}, function (err, patch) {
if (err) logger.error('save note revision error', err);
if (err) logger.error('save note revision error', err)
if (!patch) {
// if patch is empty (means no difference) then just update the latest revision updated time
latestRevision.changed('updatedAt', true);
latestRevision.changed('updatedAt', true)
latestRevision.update({
updatedAt: Date.now()
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback);
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
} else {
Revision.create({
noteId: note.id,
@ -276,31 +276,31 @@ module.exports = function (sequelize, DataTypes) {
latestRevision.update({
content: null
}).then(function () {
Revision.finishSaveNoteRevision(note, revision, callback);
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
}
});
})
}
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
},
finishSaveNoteRevision: function (note, revision, callback) {
note.update({
savedAt: revision.updatedAt
}).then(function () {
return callback(null, revision);
return callback(null, revision)
}).catch(function (err) {
return callback(err, null);
});
return callback(err, null)
})
}
}
});
})
return Revision;
};
return Revision
}

View file

@ -1,10 +1,8 @@
"use strict";
// external modules
var shortId = require('shortid');
var shortId = require('shortid')
module.exports = function (sequelize, DataTypes) {
var Temp = sequelize.define("Temp", {
var Temp = sequelize.define('Temp', {
id: {
type: DataTypes.STRING,
primaryKey: true,
@ -13,7 +11,7 @@ module.exports = function (sequelize, DataTypes) {
data: {
type: DataTypes.TEXT
}
});
})
return Temp;
};
return Temp
}

View file

@ -1,16 +1,14 @@
"use strict";
// external modules
var md5 = require("blueimp-md5");
var Sequelize = require("sequelize");
var scrypt = require('scrypt');
var md5 = require('blueimp-md5')
var Sequelize = require('sequelize')
var scrypt = require('scrypt')
// core
var logger = require("../logger.js");
var letterAvatars = require('../letter-avatars.js');
var logger = require('../logger.js')
var letterAvatars = require('../letter-avatars.js')
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
var User = sequelize.define('User', {
id: {
type: DataTypes.UUID,
primaryKey: true,
@ -41,40 +39,40 @@ module.exports = function (sequelize, DataTypes) {
password: {
type: Sequelize.TEXT,
set: function (value) {
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString("hex");
this.setDataValue('password', hash);
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
this.setDataValue('password', hash)
}
}
}, {
instanceMethods: {
verifyPassword: function (attempt) {
if (scrypt.verifyKdfSync(new Buffer(this.password, "hex"), attempt)) {
return this;
if (scrypt.verifyKdfSync(new Buffer(this.password, 'hex'), attempt)) {
return this
} else {
return false;
return false
}
}
},
classMethods: {
associate: function (models) {
User.hasMany(models.Note, {
foreignKey: "ownerId",
foreignKey: 'ownerId',
constraints: false
});
})
User.hasMany(models.Note, {
foreignKey: "lastchangeuserId",
foreignKey: 'lastchangeuserId',
constraints: false
});
})
},
getProfile: function (user) {
return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null);
return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null)
},
parseProfile: function (profile) {
try {
var profile = JSON.parse(profile);
profile = JSON.parse(profile)
} catch (err) {
logger.error(err);
profile = null;
logger.error(err)
profile = null
}
if (profile) {
profile = {
@ -83,67 +81,67 @@ module.exports = function (sequelize, DataTypes) {
biggerphoto: User.parsePhotoByProfile(profile, true)
}
}
return profile;
return profile
},
parsePhotoByProfile: function (profile, bigger) {
var photo = null;
var photo = null
switch (profile.provider) {
case "facebook":
photo = 'https://graph.facebook.com/' + profile.id + '/picture';
if (bigger) photo += '?width=400';
else photo += '?width=96';
break;
case "twitter":
photo = 'https://twitter.com/' + profile.username + '/profile_image';
if (bigger) photo += '?size=original';
else photo += '?size=bigger';
break;
case "github":
photo = 'https://avatars.githubusercontent.com/u/' + profile.id;
if (bigger) photo += '?s=400';
else photo += '?s=96';
break;
case "gitlab":
photo = profile.avatarUrl;
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400');
else photo = photo.replace(/(\?s=)\d*$/i, '$196');
break;
case "dropbox":
case 'facebook':
photo = 'https://graph.facebook.com/' + profile.id + '/picture'
if (bigger) photo += '?width=400'
else photo += '?width=96'
break
case 'twitter':
photo = 'https://twitter.com/' + profile.username + '/profile_image'
if (bigger) photo += '?size=original'
else photo += '?size=bigger'
break
case 'github':
photo = 'https://avatars.githubusercontent.com/u/' + profile.id
if (bigger) photo += '?s=400'
else photo += '?s=96'
break
case 'gitlab':
photo = profile.avatarUrl
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400')
else photo = photo.replace(/(\?s=)\d*$/i, '$196')
break
case 'dropbox':
// no image api provided, use gravatar
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value);
if (bigger) photo += '?s=400';
else photo += '?s=96';
break;
case "google":
photo = profile.photos[0].value;
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400');
else photo = photo.replace(/(\?sz=)\d*$/i, '$196');
break;
case "ldap":
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value)
if (bigger) photo += '?s=400'
else photo += '?s=96'
break
case 'google':
photo = profile.photos[0].value
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
else photo = photo.replace(/(\?sz=)\d*$/i, '$196')
break
case 'ldap':
// no image api provided,
// use gravatar if email exists,
// otherwise generate a letter avatar
if (profile.emails[0]) {
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]);
if (bigger) photo += '?s=400';
else photo += '?s=96';
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
if (bigger) photo += '?s=400'
else photo += '?s=96'
} else {
photo = letterAvatars(profile.username);
photo = letterAvatars(profile.username)
}
break;
break
}
return photo;
return photo
},
parseProfileByEmail: function (email) {
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email);
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email)
return {
name: email.substring(0, email.lastIndexOf("@")),
photo: photoUrl += '?s=96',
biggerphoto: photoUrl += '?s=400'
};
name: email.substring(0, email.lastIndexOf('@')),
photo: photoUrl + '?s=96',
biggerphoto: photoUrl + '?s=400'
}
}
});
}
})
return User;
};
return User
}

File diff suppressed because it is too large Load diff

View file

@ -1,36 +1,34 @@
// response
// external modules
var fs = require('fs');
var path = require('path');
var markdownpdf = require("markdown-pdf");
var LZString = require('lz-string');
var S = require('string');
var shortId = require('shortid');
var querystring = require('querystring');
var request = require('request');
var moment = require('moment');
var fs = require('fs')
var markdownpdf = require('markdown-pdf')
var LZString = require('lz-string')
var shortId = require('shortid')
var querystring = require('querystring')
var request = require('request')
var moment = require('moment')
// core
var config = require("./config.js");
var logger = require("./logger.js");
var models = require("./models");
var config = require('./config.js')
var logger = require('./logger.js')
var models = require('./models')
// public
var response = {
errorForbidden: function (res) {
responseError(res, "403", "Forbidden", "oh no.");
responseError(res, '403', 'Forbidden', 'oh no.')
},
errorNotFound: function (res) {
responseError(res, "404", "Not Found", "oops.");
responseError(res, '404', 'Not Found', 'oops.')
},
errorBadRequest: function (res) {
responseError(res, "400", "Bad Request", "something not right.");
responseError(res, '400', 'Bad Request', 'something not right.')
},
errorInternalError: function (res) {
responseError(res, "500", "Internal Error", "wtf.");
responseError(res, '500', 'Internal Error', 'wtf.')
},
errorServiceUnavailable: function (res) {
res.status(503).send("I'm busy right now, try again later.");
res.status(503).send("I'm busy right now, try again later.")
},
newNote: newNote,
showNote: showNote,
@ -42,7 +40,7 @@ var response = {
publishSlideActions: publishSlideActions,
githubActions: githubActions,
gitlabActions: gitlabActions
};
}
function responseError (res, code, detail, msg) {
res.status(code).render(config.errorpath, {
@ -52,7 +50,7 @@ function responseError(res, code, detail, msg) {
detail: detail,
msg: msg,
useCDN: config.usecdn
});
})
}
function showIndex (req, res, next) {
@ -72,19 +70,19 @@ function showIndex(req, res, next) {
signin: req.isAuthenticated(),
infoMessage: req.flash('info'),
errorMessage: req.flash('error')
});
})
}
function responseHackMD (res, note) {
var body = note.content;
var extracted = models.Note.extractMeta(body);
var meta = models.Note.parseMeta(extracted.meta);
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title);
var body = note.content
var extracted = models.Note.extractMeta(body)
var meta = models.Note.parseMeta(extracted.meta)
var title = models.Note.decodeTitle(note.title)
title = models.Note.generateWebTitle(meta.title || title)
res.set({
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
})
res.render(config.hackmdpath, {
url: config.serverurl,
title: title,
@ -99,47 +97,44 @@ function responseHackMD(res, note) {
ldap: config.ldap,
email: config.email,
allowemailregister: config.allowemailregister
});
})
}
function newNote (req, res, next) {
var owner = null;
var owner = null
if (req.isAuthenticated()) {
owner = req.user.id;
owner = req.user.id
} else if (!config.allowanonymous) {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
models.Note.create({
ownerId: owner,
alias: req.alias ? req.alias : null
}).then(function (note) {
return res.redirect(config.serverurl + "/" + LZString.compressToBase64(note.id));
return res.redirect(config.serverurl + '/' + LZString.compressToBase64(note.id))
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
logger.error(err)
return response.errorInternalError(res)
})
}
function checkViewPermission (req, note) {
if (note.permission == 'private') {
if (!req.isAuthenticated() || note.ownerId != req.user.id)
return false;
else
return true;
} else if (note.permission == 'limited' || note.permission == 'protected') {
if(!req.isAuthenticated())
return false;
else
return true;
if (note.permission === 'private') {
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
} else if (note.permission === 'limited' || note.permission === 'protected') {
if (!req.isAuthenticated()) { return false } else { return true }
} else {
return true;
return true
}
}
function findNote (req, res, callback, include) {
var noteId = req.params.noteId;
var id = req.params.noteId || req.params.shortid;
var noteId = req.params.noteId
var id = req.params.noteId || req.params.shortid
models.Note.parseNoteId(id, function (err, _id) {
if (err) {
logger.log(err)
}
models.Note.findOne({
where: {
id: _id
@ -148,61 +143,61 @@ function findNote(req, res, callback, include) {
}).then(function (note) {
if (!note) {
if (config.allowfreeurl && noteId) {
req.alias = noteId;
return newNote(req, res);
req.alias = noteId
return newNote(req, res)
} else {
return response.errorNotFound(res);
return response.errorNotFound(res)
}
}
if (!checkViewPermission(req, note)) {
return response.errorForbidden(res);
return response.errorForbidden(res)
} else {
return callback(note);
return callback(note)
}
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
});
logger.error(err)
return response.errorInternalError(res)
})
})
}
function showNote (req, res, next) {
findNote(req, res, function (note) {
// force to use note id
var noteId = req.params.noteId;
var id = LZString.compressToBase64(note.id);
if ((note.alias && noteId != note.alias) || (!note.alias && noteId != id))
return res.redirect(config.serverurl + "/" + (note.alias || id));
return responseHackMD(res, note);
});
var noteId = req.params.noteId
var id = LZString.compressToBase64(note.id)
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverurl + '/' + (note.alias || id)) }
return responseHackMD(res, note)
})
}
function showPublishNote (req, res, next) {
var include = [{
model: models.User,
as: "owner"
as: 'owner'
}, {
model: models.User,
as: "lastchangeuser"
}];
as: 'lastchangeuser'
}]
findNote(req, res, function (note) {
// force to use short id
var shortid = req.params.shortid;
if ((note.alias && shortid != note.alias) || (!note.alias && shortid != note.shortid))
return res.redirect(config.serverurl + "/s/" + (note.alias || note.shortid));
var shortid = req.params.shortid
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
return res.redirect(config.serverurl + '/s/' + (note.alias || note.shortid))
}
note.increment('viewcount').then(function (note) {
if (!note) {
return response.errorNotFound(res);
return response.errorNotFound(res)
}
var body = note.content;
var extracted = models.Note.extractMeta(body);
markdown = extracted.markdown;
meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title);
var origin = config.serverurl;
var body = note.content
var extracted = models.Note.extractMeta(body)
var markdown = extracted.markdown
var meta = models.Note.parseMeta(extracted.meta)
var createtime = note.createdAt
var updatetime = note.lastchangeAt
var title = models.Note.decodeTitle(note.title)
title = models.Note.generateWebTitle(meta.title || title)
var origin = config.serverurl
var data = {
title: title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
@ -219,35 +214,35 @@ function showPublishNote(req, res, next) {
robots: meta.robots || false, // default allow robots
GA: meta.GA,
disqus: meta.disqus
};
return renderPublish(data, res);
}
return renderPublish(data, res)
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
}, include);
logger.error(err)
return response.errorInternalError(res)
})
}, include)
}
function renderPublish (data, res) {
res.set({
'Cache-Control': 'private' // only cache by client
});
res.render(config.prettypath, data);
})
res.render(config.prettypath, data)
}
function actionPublish (req, res, note) {
res.redirect(config.serverurl + "/s/" + (note.alias || note.shortid));
res.redirect(config.serverurl + '/s/' + (note.alias || note.shortid))
}
function actionSlide (req, res, note) {
res.redirect(config.serverurl + "/p/" + (note.alias || note.shortid));
res.redirect(config.serverurl + '/p/' + (note.alias || note.shortid))
}
function actionDownload (req, res, note) {
var body = note.content;
var title = models.Note.decodeTitle(note.title);
var filename = title;
filename = encodeURIComponent(filename);
var body = note.content
var title = models.Note.decodeTitle(note.title)
var filename = title
filename = encodeURIComponent(filename)
res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API
'Access-Control-Allow-Headers': 'Range',
@ -256,82 +251,82 @@ function actionDownload(req, res, note) {
'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md',
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
res.send(body);
})
res.send(body)
}
function actionInfo (req, res, note) {
var body = note.content;
var extracted = models.Note.extractMeta(body);
var markdown = extracted.markdown;
var meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
var body = note.content
var extracted = models.Note.extractMeta(body)
var markdown = extracted.markdown
var meta = models.Note.parseMeta(extracted.meta)
var createtime = note.createdAt
var updatetime = note.lastchangeAt
var title = models.Note.decodeTitle(note.title)
var data = {
title: meta.title || title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime
};
}
res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API
'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
res.send(data);
})
res.send(data)
}
function actionPDF (req, res, note) {
var body = note.content;
var extracted = models.Note.extractMeta(body);
var title = models.Note.decodeTitle(note.title);
var body = note.content
var extracted = models.Note.extractMeta(body)
var title = models.Note.decodeTitle(note.title)
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(extracted.markdown).to(path, function () {
var stream = fs.createReadStream(path);
var filename = title;
var stream = fs.createReadStream(path)
var filename = title
// Be careful of special characters
filename = encodeURIComponent(filename);
filename = encodeURIComponent(filename)
// Ideally this should strip them
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '.pdf"');
res.setHeader('Cache-Control', 'private');
res.setHeader('Content-Type', 'application/pdf; charset=UTF-8');
res.setHeader('X-Robots-Tag', 'noindex, nofollow'); // prevent crawling
stream.pipe(res);
fs.unlink(path);
});
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '.pdf"')
res.setHeader('Cache-Control', 'private')
res.setHeader('Content-Type', 'application/pdf; charset=UTF-8')
res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling
stream.pipe(res)
fs.unlink(path)
})
}
function actionGist (req, res, note) {
var data = {
client_id: config.github.clientID,
redirect_uri: config.serverurl + '/auth/github/callback/' + LZString.compressToBase64(note.id) + '/gist',
scope: "gist",
scope: 'gist',
state: shortId.generate()
};
var query = querystring.stringify(data);
res.redirect("https://github.com/login/oauth/authorize?" + query);
}
var query = querystring.stringify(data)
res.redirect('https://github.com/login/oauth/authorize?' + query)
}
function actionRevision (req, res, note) {
var actionId = req.params.actionId;
var actionId = req.params.actionId
if (actionId) {
var time = moment(parseInt(actionId));
var time = moment(parseInt(actionId))
if (time.isValid()) {
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
if (err) {
logger.error(err);
return response.errorInternalError(res);
logger.error(err)
return response.errorInternalError(res)
}
if (!content) {
return response.errorNotFound(res);
return response.errorNotFound(res)
}
res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API
@ -339,115 +334,114 @@ function actionRevision(req, res, note) {
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
res.send(content);
});
})
res.send(content)
})
} else {
return response.errorNotFound(res);
return response.errorNotFound(res)
}
} else {
models.Revision.getNoteRevisions(note, function (err, data) {
if (err) {
logger.error(err);
return response.errorInternalError(res);
logger.error(err)
return response.errorInternalError(res)
}
var out = {
revision: data
};
}
res.set({
'Access-Control-Allow-Origin': '*', // allow CORS as API
'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
});
res.send(out);
});
})
res.send(out)
})
}
}
function noteActions (req, res, next) {
var noteId = req.params.noteId;
var noteId = req.params.noteId
findNote(req, res, function (note) {
var action = req.params.action;
var action = req.params.action
switch (action) {
case "publish":
case "pretty": //pretty deprecated
actionPublish(req, res, note);
break;
case "slide":
actionSlide(req, res, note);
break;
case "download":
actionDownload(req, res, note);
break;
case "info":
actionInfo(req, res, note);
break;
case "pdf":
actionPDF(req, res, note);
break;
case "gist":
actionGist(req, res, note);
break;
case "revision":
actionRevision(req, res, note);
break;
case 'publish':
case 'pretty': // pretty deprecated
actionPublish(req, res, note)
break
case 'slide':
actionSlide(req, res, note)
break
case 'download':
actionDownload(req, res, note)
break
case 'info':
actionInfo(req, res, note)
break
case 'pdf':
actionPDF(req, res, note)
break
case 'gist':
actionGist(req, res, note)
break
case 'revision':
actionRevision(req, res, note)
break
default:
return res.redirect(config.serverurl + '/' + noteId);
break;
return res.redirect(config.serverurl + '/' + noteId)
}
});
})
}
function publishNoteActions (req, res, next) {
findNote(req, res, function (note) {
var action = req.params.action;
var action = req.params.action
switch (action) {
case "edit":
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)));
break;
case 'edit':
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)))
break
default:
res.redirect(config.serverurl + '/s/' + note.shortid);
break;
res.redirect(config.serverurl + '/s/' + note.shortid)
break
}
});
})
}
function publishSlideActions (req, res, next) {
findNote(req, res, function (note) {
var action = req.params.action;
var action = req.params.action
switch (action) {
case "edit":
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)));
break;
case 'edit':
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)))
break
default:
res.redirect(config.serverurl + '/p/' + note.shortid);
break;
res.redirect(config.serverurl + '/p/' + note.shortid)
break
}
});
})
}
function githubActions (req, res, next) {
var noteId = req.params.noteId;
var noteId = req.params.noteId
findNote(req, res, function (note) {
var action = req.params.action;
var action = req.params.action
switch (action) {
case "gist":
githubActionGist(req, res, note);
break;
case 'gist':
githubActionGist(req, res, note)
break
default:
res.redirect(config.serverurl + '/' + noteId);
break;
res.redirect(config.serverurl + '/' + noteId)
break
}
});
})
}
function githubActionGist (req, res, note) {
var code = req.query.code;
var state = req.query.state;
var code = req.query.code
var state = req.query.state
if (!code || !state) {
return response.errorForbidden(res);
return response.errorForbidden(res)
} else {
var data = {
client_id: config.github.clientID,
@ -455,64 +449,64 @@ function githubActionGist(req, res, note) {
code: code,
state: state
}
var auth_url = 'https://github.com/login/oauth/access_token';
var authUrl = 'https://github.com/login/oauth/access_token'
request({
url: auth_url,
method: "POST",
url: authUrl,
method: 'POST',
json: data
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var access_token = body.access_token;
if (access_token) {
var content = note.content;
var title = models.Note.decodeTitle(note.title);
var filename = title.replace('/', ' ') + '.md';
if (!error && httpResponse.statusCode === 200) {
var accessToken = body.access_token
if (accessToken) {
var content = note.content
var title = models.Note.decodeTitle(note.title)
var filename = title.replace('/', ' ') + '.md'
var gist = {
"files": {}
};
'files': {}
}
gist.files[filename] = {
"content": content
};
var gist_url = "https://api.github.com/gists";
'content': content
}
var gistUrl = 'https://api.github.com/gists'
request({
url: gist_url,
url: gistUrl,
headers: {
'User-Agent': 'HackMD',
'Authorization': 'token ' + access_token
'Authorization': 'token ' + accessToken
},
method: "POST",
method: 'POST',
json: gist
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 201) {
res.setHeader('referer', '');
res.redirect(body.html_url);
if (!error && httpResponse.statusCode === 201) {
res.setHeader('referer', '')
res.redirect(body.html_url)
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
});
})
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
})
}
}
function gitlabActions (req, res, next) {
var noteId = req.params.noteId;
var noteId = req.params.noteId
findNote(req, res, function (note) {
var action = req.params.action;
var action = req.params.action
switch (action) {
case "projects":
gitlabActionProjects(req, res, note);
break;
case 'projects':
gitlabActionProjects(req, res, note)
break
default:
res.redirect(config.serverurl + '/' + noteId);
break;
res.redirect(config.serverurl + '/' + noteId)
break
}
});
})
}
function gitlabActionProjects (req, res, note) {
@ -522,57 +516,55 @@ function gitlabActionProjects(req, res, note) {
id: req.user.id
}
}).then(function (user) {
if (!user)
return response.errorNotFound(res);
var ret = { baseURL: config.gitlab.baseURL };
ret.accesstoken = user.accessToken;
ret.profileid = user.profileid;
if (!user) { return response.errorNotFound(res) }
var ret = { baseURL: config.gitlab.baseURL }
ret.accesstoken = user.accessToken
ret.profileid = user.profileid
request(
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
ret.projects = JSON.parse(body);
return res.send(ret);
if (!error && httpResponse.statusCode === 200) {
ret.projects = JSON.parse(body)
return res.send(ret)
} else {
return res.send(ret);
return res.send(ret)
}
}
);
)
}).catch(function (err) {
logger.error('gitlab action projects failed: ' + err);
return response.errorInternalError(res);
});
logger.error('gitlab action projects failed: ' + err)
return response.errorInternalError(res)
})
} else {
return response.errorForbidden(res);
return response.errorForbidden(res)
}
}
function showPublishSlide (req, res, next) {
var include = [{
model: models.User,
as: "owner"
as: 'owner'
}, {
model: models.User,
as: "lastchangeuser"
}];
as: 'lastchangeuser'
}]
findNote(req, res, function (note) {
// force to use short id
var shortid = req.params.shortid;
if ((note.alias && shortid != note.alias) || (!note.alias && shortid != note.shortid))
return res.redirect(config.serverurl + "/p/" + (note.alias || note.shortid));
var shortid = req.params.shortid
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverurl + '/p/' + (note.alias || note.shortid)) }
note.increment('viewcount').then(function (note) {
if (!note) {
return response.errorNotFound(res);
return response.errorNotFound(res)
}
var body = note.content;
var extracted = models.Note.extractMeta(body);
markdown = extracted.markdown;
meta = models.Note.parseMeta(extracted.meta);
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(meta.title || title);
var origin = config.serverurl;
var body = note.content
var extracted = models.Note.extractMeta(body)
var markdown = extracted.markdown
var meta = models.Note.parseMeta(extracted.meta)
var createtime = note.createdAt
var updatetime = note.lastchangeAt
var title = models.Note.decodeTitle(note.title)
title = models.Note.generateWebTitle(meta.title || title)
var origin = config.serverurl
var data = {
title: title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
@ -590,20 +582,20 @@ function showPublishSlide(req, res, next) {
robots: meta.robots || false, // default allow robots
GA: meta.GA,
disqus: meta.disqus
};
return renderPublishSlide(data, res);
}
return renderPublishSlide(data, res)
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
}, include);
logger.error(err)
return response.errorInternalError(res)
})
}, include)
}
function renderPublishSlide (data, res) {
res.set({
'Cache-Control': 'private' // only cache by client
});
res.render(config.slidepath, data);
})
res.render(config.slidepath, data)
}
module.exports = response;
module.exports = response

View file

@ -1,140 +1,137 @@
// external modules
var DiffMatchPatch = require('diff-match-patch');
var dmp = new DiffMatchPatch();
var DiffMatchPatch = require('diff-match-patch')
var dmp = new DiffMatchPatch()
// core
var config = require("../config.js");
var logger = require("../logger.js");
var config = require('../config.js')
var logger = require('../logger.js')
process.on('message', function (data) {
if (!data || !data.msg || !data.cacheKey) {
return logger.error('dmp worker error: not enough data');
return logger.error('dmp worker error: not enough data')
}
switch (data.msg) {
case 'create patch':
if (!data.hasOwnProperty('lastDoc') || !data.hasOwnProperty('currDoc')) {
return logger.error('dmp worker error: not enough data on create patch');
return logger.error('dmp worker error: not enough data on create patch')
}
try {
var patch = createPatch(data.lastDoc, data.currDoc);
var patch = createPatch(data.lastDoc, data.currDoc)
process.send({
msg: 'check',
result: patch,
cacheKey: data.cacheKey
});
})
} catch (err) {
logger.error('dmp worker error', err);
logger.error('dmp worker error', err)
process.send({
msg: 'error',
error: err,
cacheKey: data.cacheKey
});
})
}
break;
break
case 'get revision':
if (!data.hasOwnProperty('revisions') || !data.hasOwnProperty('count')) {
return logger.error('dmp worker error: not enough data on get revision');
return logger.error('dmp worker error: not enough data on get revision')
}
try {
var result = getRevision(data.revisions, data.count);
var result = getRevision(data.revisions, data.count)
process.send({
msg: 'check',
result: result,
cacheKey: data.cacheKey
});
})
} catch (err) {
logger.error('dmp worker error', err);
logger.error('dmp worker error', err)
process.send({
msg: 'error',
error: err,
cacheKey: data.cacheKey
});
})
}
break;
break
}
});
})
function createPatch (lastDoc, currDoc) {
var ms_start = (new Date()).getTime();
var diff = dmp.diff_main(lastDoc, currDoc);
var patch = dmp.patch_make(lastDoc, diff);
patch = dmp.patch_toText(patch);
var ms_end = (new Date()).getTime();
var msStart = (new Date()).getTime()
var diff = dmp.diff_main(lastDoc, currDoc)
var patch = dmp.patch_make(lastDoc, diff)
patch = dmp.patch_toText(patch)
var msEnd = (new Date()).getTime()
if (config.debug) {
logger.info(patch);
logger.info((ms_end - ms_start) + 'ms');
logger.info(patch)
logger.info((msEnd - msStart) + 'ms')
}
return patch;
return patch
}
function getRevision (revisions, count) {
var ms_start = (new Date()).getTime();
var startContent = null;
var lastPatch = [];
var applyPatches = [];
var authorship = [];
var msStart = (new Date()).getTime()
var startContent = null
var lastPatch = []
var applyPatches = []
var authorship = []
if (count <= Math.round(revisions.length / 2)) {
// start from top to target
for (var i = 0; i < count; i++) {
var revision = revisions[i];
if (i == 0) {
startContent = revision.content || revision.lastContent;
for (let i = 0; i < count; i++) {
let revision = revisions[i]
if (i === 0) {
startContent = revision.content || revision.lastContent
}
if (i != count - 1) {
var patch = dmp.patch_fromText(revision.patch);
applyPatches = applyPatches.concat(patch);
if (i !== count - 1) {
let patch = dmp.patch_fromText(revision.patch)
applyPatches = applyPatches.concat(patch)
}
lastPatch = revision.patch;
authorship = revision.authorship;
lastPatch = revision.patch
authorship = revision.authorship
}
// swap DIFF_INSERT and DIFF_DELETE to achieve unpatching
for (var i = 0, l = applyPatches.length; i < l; i++) {
for (var j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
var diff = applyPatches[i].diffs[j];
if (diff[0] == DiffMatchPatch.DIFF_INSERT)
diff[0] = DiffMatchPatch.DIFF_DELETE;
else if (diff[0] == DiffMatchPatch.DIFF_DELETE)
diff[0] = DiffMatchPatch.DIFF_INSERT;
for (let i = 0, l = applyPatches.length; i < l; i++) {
for (let j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
var diff = applyPatches[i].diffs[j]
if (diff[0] === DiffMatchPatch.DIFF_INSERT) { diff[0] = DiffMatchPatch.DIFF_DELETE } else if (diff[0] === DiffMatchPatch.DIFF_DELETE) { diff[0] = DiffMatchPatch.DIFF_INSERT }
}
}
} else {
// start from bottom to target
var l = revisions.length - 1;
var l = revisions.length - 1
for (var i = l; i >= count - 1; i--) {
var revision = revisions[i];
if (i == l) {
startContent = revision.lastContent;
authorship = revision.authorship;
let revision = revisions[i]
if (i === l) {
startContent = revision.lastContent
authorship = revision.authorship
}
if (revision.patch) {
var patch = dmp.patch_fromText(revision.patch);
applyPatches = applyPatches.concat(patch);
let patch = dmp.patch_fromText(revision.patch)
applyPatches = applyPatches.concat(patch)
}
lastPatch = revision.patch;
authorship = revision.authorship;
lastPatch = revision.patch
authorship = revision.authorship
}
}
try {
var finalContent = dmp.patch_apply(applyPatches, startContent)[0];
var finalContent = dmp.patch_apply(applyPatches, startContent)[0]
} catch (err) {
throw new Error(err);
throw new Error(err)
}
var data = {
content: finalContent,
patch: dmp.patch_fromText(lastPatch),
authorship: authorship
};
var ms_end = (new Date()).getTime();
if (config.debug) {
logger.info((ms_end - ms_start) + 'ms');
}
return data;
var msEnd = (new Date()).getTime()
if (config.debug) {
logger.info((msEnd - msStart) + 'ms')
}
return data
}
// log uncaught exception
process.on('uncaughtException', function (err) {
logger.error('An uncaught exception has occured.');
logger.error(err);
logger.error('Process will exit now.');
process.exit(1);
});
logger.error('An uncaught exception has occured.')
logger.error(err)
logger.error('Process will exit now.')
process.exit(1)
})

View file

@ -5,7 +5,7 @@
"main": "app.js",
"license": "MIT",
"scripts": {
"test": "npm run-script lint",
"test": "node ./node_modules/standard/bin/cmd.js && npm run-script lint",
"lint": "eslint .",
"dev": "webpack --config webpack.config.js --progress --colors --watch",
"build": "webpack --config webpack.production.js --progress --colors",
@ -165,8 +165,15 @@
"optimize-css-assets-webpack-plugin": "^1.3.0",
"script-loader": "^0.7.0",
"style-loader": "^0.13.1",
"standard": "^9.0.1",
"url-loader": "^0.5.7",
"webpack": "^1.14.0",
"webpack-parallel-uglify-plugin": "^0.2.0"
},
"standard": {
"ignore": [
"lib/ot",
"public/vendor"
]
}
}

View file

@ -1,7 +1,10 @@
require('./locale');
/* eslint-env browser, jquery */
/* global moment, serverurl */
require('../css/cover.css');
require('../css/site.css');
require('./locale')
require('../css/cover.css')
require('../css/site.css')
import {
checkIfAuth,
@ -9,7 +12,7 @@ import {
getLoginState,
resetCheckAuth,
setloginStateChangeEvent
} from './lib/common/login';
} from './lib/common/login'
import {
clearDuplicatedHistory,
@ -23,411 +26,403 @@ import {
removeHistory,
saveHistory,
saveStorageHistoryToServer
} from './history';
} from './history'
import { saveAs } from 'file-saver';
import List from 'list.js';
import S from 'string';
import { saveAs } from 'file-saver'
import List from 'list.js'
import S from 'string'
const options = {
valueNames: ['id', 'text', 'timestamp', 'fromNow', 'time', 'tags', 'pinned'],
item: '<li class="col-xs-12 col-sm-6 col-md-6 col-lg-4">\
<span class="id" style="display:none;"></span>\
<a href="#">\
<div class="item">\
<div class="ui-history-pin fa fa-thumb-tack fa-fw"></div>\
<div class="ui-history-close fa fa-close fa-fw" data-toggle="modal" data-target=".delete-modal"></div>\
<div class="content">\
<h4 class="text"></h4>\
<p>\
<i><i class="fa fa-clock-o"></i> visited </i><i class="fromNow"></i>\
<br>\
<i class="timestamp" style="display:none;"></i>\
<i class="time"></i>\
</p>\
<p class="tags"></p>\
</div>\
</div>\
</a>\
</li>',
item: '<li class="col-xs-12 col-sm-6 col-md-6 col-lg-4">' +
'<span class="id" style="display:none;"></span>' +
'<a href="#">' +
'<div class="item">' +
'<div class="ui-history-pin fa fa-thumb-tack fa-fw"></div>' +
'<div class="ui-history-close fa fa-close fa-fw" data-toggle="modal" data-target=".delete-modal"></div>' +
'<div class="content">' +
'<h4 class="text"></h4>' +
'<p>' +
'<i><i class="fa fa-clock-o"></i> visited </i><i class="fromNow"></i>' +
'<br>' +
'<i class="timestamp" style="display:none;"></i>' +
'<i class="time"></i>' +
'</p>' +
'<p class="tags"></p>' +
'</div>' +
'</div>' +
'</a>' +
'</li>',
page: 18,
plugins: [
ListPagination({
window.ListPagination({
outerWindow: 1
})
]
};
const historyList = new List('history', options);
}
const historyList = new List('history', options)
migrateHistoryFromTempCallback = pageInit;
setloginStateChangeEvent(pageInit);
window.migrateHistoryFromTempCallback = pageInit
setloginStateChangeEvent(pageInit)
pageInit();
pageInit()
function pageInit () {
checkIfAuth(
data => {
$('.ui-signin').hide();
$('.ui-or').hide();
$('.ui-welcome').show();
if (data.photo) $('.ui-avatar').prop('src', data.photo).show();
else $('.ui-avatar').prop('src', '').hide();
$('.ui-name').html(data.name);
$('.ui-signout').show();
$(".ui-history").click();
parseServerToHistory(historyList, parseHistoryCallback);
$('.ui-signin').hide()
$('.ui-or').hide()
$('.ui-welcome').show()
if (data.photo) $('.ui-avatar').prop('src', data.photo).show()
else $('.ui-avatar').prop('src', '').hide()
$('.ui-name').html(data.name)
$('.ui-signout').show()
$('.ui-history').click()
parseServerToHistory(historyList, parseHistoryCallback)
},
() => {
$('.ui-signin').show();
$('.ui-or').show();
$('.ui-welcome').hide();
$('.ui-avatar').prop('src', '').hide();
$('.ui-name').html('');
$('.ui-signout').hide();
parseStorageToHistory(historyList, parseHistoryCallback);
$('.ui-signin').show()
$('.ui-or').show()
$('.ui-welcome').hide()
$('.ui-avatar').prop('src', '').hide()
$('.ui-name').html('')
$('.ui-signout').hide()
parseStorageToHistory(historyList, parseHistoryCallback)
}
);
)
}
$(".masthead-nav li").click(function () {
$(this).siblings().removeClass("active");
$(this).addClass("active");
});
$('.masthead-nav li').click(function () {
$(this).siblings().removeClass('active')
$(this).addClass('active')
})
// prevent empty link change hash
$('a[href="#"]').click(function (e) {
e.preventDefault();
});
e.preventDefault()
})
$(".ui-home").click(function (e) {
if (!$("#home").is(':visible')) {
$(".section:visible").hide();
$("#home").fadeIn();
$('.ui-home').click(function (e) {
if (!$('#home').is(':visible')) {
$('.section:visible').hide()
$('#home').fadeIn()
}
});
})
$(".ui-history").click(() => {
if (!$("#history").is(':visible')) {
$(".section:visible").hide();
$("#history").fadeIn();
$('.ui-history').click(() => {
if (!$('#history').is(':visible')) {
$('.section:visible').hide()
$('#history').fadeIn()
}
});
})
function checkHistoryList () {
if ($("#history-list").children().length > 0) {
$('.pagination').show();
$(".ui-nohistory").hide();
$(".ui-import-from-browser").hide();
} else if ($("#history-list").children().length == 0) {
$('.pagination').hide();
$(".ui-nohistory").slideDown();
if ($('#history-list').children().length > 0) {
$('.pagination').show()
$('.ui-nohistory').hide()
$('.ui-import-from-browser').hide()
} else if ($('#history-list').children().length === 0) {
$('.pagination').hide()
$('.ui-nohistory').slideDown()
getStorageHistory(data => {
if (data && data.length > 0 && getLoginState() && historyList.items.length == 0) {
$(".ui-import-from-browser").slideDown();
if (data && data.length > 0 && getLoginState() && historyList.items.length === 0) {
$('.ui-import-from-browser').slideDown()
}
});
})
}
}
function parseHistoryCallback (list, notehistory) {
checkHistoryList();
checkHistoryList()
// sort by pinned then timestamp
list.sort('', {
sortFunction (a, b) {
const notea = a.values();
const noteb = b.values();
const notea = a.values()
const noteb = b.values()
if (notea.pinned && !noteb.pinned) {
return -1;
return -1
} else if (!notea.pinned && noteb.pinned) {
return 1;
return 1
} else {
if (notea.timestamp > noteb.timestamp) {
return -1;
return -1
} else if (notea.timestamp < noteb.timestamp) {
return 1;
return 1
} else {
return 0;
return 0
}
}
}
});
})
// parse filter tags
const filtertags = [];
const filtertags = []
for (let i = 0, l = list.items.length; i < l; i++) {
const tags = list.items[i]._values.tags;
const tags = list.items[i]._values.tags
if (tags && tags.length > 0) {
for (let j = 0; j < tags.length; j++) {
// push info filtertags if not found
let found = false;
if (filtertags.includes(tags[j]))
found = true;
if (!found)
filtertags.push(tags[j]);
let found = false
if (filtertags.includes(tags[j])) { found = true }
if (!found) { filtertags.push(tags[j]) }
}
}
}
buildTagsFilter(filtertags);
buildTagsFilter(filtertags)
}
// update items whenever list updated
historyList.on('updated', e => {
for (let i = 0, l = e.items.length; i < l; i++) {
const item = e.items[i];
const item = e.items[i]
if (item.visible()) {
const itemEl = $(item.elm);
const values = item._values;
const a = itemEl.find("a");
const pin = itemEl.find(".ui-history-pin");
const tagsEl = itemEl.find(".tags");
const itemEl = $(item.elm)
const values = item._values
const a = itemEl.find('a')
const pin = itemEl.find('.ui-history-pin')
const tagsEl = itemEl.find('.tags')
// parse link to element a
a.attr('href', `${serverurl}/${values.id}`);
a.attr('href', `${serverurl}/${values.id}`)
// parse pinned
if (values.pinned) {
pin.addClass('active');
pin.addClass('active')
} else {
pin.removeClass('active');
pin.removeClass('active')
}
// parse tags
const tags = values.tags;
const tags = values.tags
if (tags && tags.length > 0 && tagsEl.children().length <= 0) {
const labels = [];
const labels = []
for (let j = 0; j < tags.length; j++) {
// push into the item label
labels.push(`<span class='label label-default'>${tags[j]}</span>`);
labels.push(`<span class='label label-default'>${tags[j]}</span>`)
}
tagsEl.html(labels.join(' '));
tagsEl.html(labels.join(' '))
}
}
}
$(".ui-history-close").off('click');
$(".ui-history-close").on('click', historyCloseClick);
$(".ui-history-pin").off('click');
$(".ui-history-pin").on('click', historyPinClick);
});
$('.ui-history-close').off('click')
$('.ui-history-close').on('click', historyCloseClick)
$('.ui-history-pin').off('click')
$('.ui-history-pin').on('click', historyPinClick)
})
function historyCloseClick (e) {
e.preventDefault();
const id = $(this).closest("a").siblings("span").html();
const value = historyList.get('id', id)[0]._values;
$('.ui-delete-modal-msg').text('Do you really want to delete below history?');
$('.ui-delete-modal-item').html(`<i class="fa fa-file-text"></i> ${value.text}<br><i class="fa fa-clock-o"></i> ${value.time}`);
clearHistory = false;
deleteId = id;
e.preventDefault()
const id = $(this).closest('a').siblings('span').html()
const value = historyList.get('id', id)[0]._values
$('.ui-delete-modal-msg').text('Do you really want to delete below history?')
$('.ui-delete-modal-item').html(`<i class="fa fa-file-text"></i> ${value.text}<br><i class="fa fa-clock-o"></i> ${value.time}`)
clearHistory = false
deleteId = id
}
function historyPinClick (e) {
e.preventDefault();
const $this = $(this);
const id = $this.closest("a").siblings("span").html();
const item = historyList.get('id', id)[0];
const values = item._values;
let pinned = values.pinned;
e.preventDefault()
const $this = $(this)
const id = $this.closest('a').siblings('span').html()
const item = historyList.get('id', id)[0]
const values = item._values
let pinned = values.pinned
if (!values.pinned) {
pinned = true;
item._values.pinned = true;
pinned = true
item._values.pinned = true
} else {
pinned = false;
item._values.pinned = false;
pinned = false
item._values.pinned = false
}
checkIfAuth(() => {
postHistoryToServer(id, {
pinned
}, (err, result) => {
if (!err) {
if (pinned)
$this.addClass('active');
else
$this.removeClass('active');
if (pinned) { $this.addClass('active') } else { $this.removeClass('active') }
}
});
})
}, () => {
getHistory(notehistory => {
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == id) {
notehistory[i].pinned = pinned;
break;
if (notehistory[i].id === id) {
notehistory[i].pinned = pinned
break
}
}
saveHistory(notehistory);
if (pinned)
$this.addClass('active');
else
$this.removeClass('active');
});
});
saveHistory(notehistory)
if (pinned) { $this.addClass('active') } else { $this.removeClass('active') }
})
})
}
// auto update item fromNow every minutes
setInterval(updateItemFromNow, 60000);
setInterval(updateItemFromNow, 60000)
function updateItemFromNow () {
const items = $('.item').toArray();
const items = $('.item').toArray()
for (let i = 0; i < items.length; i++) {
const item = $(items[i]);
const timestamp = parseInt(item.find('.timestamp').text());
item.find('.fromNow').text(moment(timestamp).fromNow());
const item = $(items[i])
const timestamp = parseInt(item.find('.timestamp').text())
item.find('.fromNow').text(moment(timestamp).fromNow())
}
}
var clearHistory = false;
var deleteId = null;
var clearHistory = false
var deleteId = null
function deleteHistory () {
checkIfAuth(() => {
deleteServerHistory(deleteId, (err, result) => {
if (!err) {
if (clearHistory) {
historyList.clear();
checkHistoryList();
historyList.clear()
checkHistoryList()
} else {
historyList.remove('id', deleteId);
checkHistoryList();
historyList.remove('id', deleteId)
checkHistoryList()
}
}
$('.delete-modal').modal('hide');
deleteId = null;
clearHistory = false;
});
$('.delete-modal').modal('hide')
deleteId = null
clearHistory = false
})
}, () => {
if (clearHistory) {
saveHistory([]);
historyList.clear();
checkHistoryList();
deleteId = null;
saveHistory([])
historyList.clear()
checkHistoryList()
deleteId = null
} else {
if (!deleteId) return;
if (!deleteId) return
getHistory(notehistory => {
const newnotehistory = removeHistory(deleteId, notehistory);
saveHistory(newnotehistory);
historyList.remove('id', deleteId);
checkHistoryList();
deleteId = null;
});
const newnotehistory = removeHistory(deleteId, notehistory)
saveHistory(newnotehistory)
historyList.remove('id', deleteId)
checkHistoryList()
deleteId = null
})
}
$('.delete-modal').modal('hide');
clearHistory = false;
});
$('.delete-modal').modal('hide')
clearHistory = false
})
}
$(".ui-delete-modal-confirm").click(() => {
deleteHistory();
});
$('.ui-delete-modal-confirm').click(() => {
deleteHistory()
})
$(".ui-import-from-browser").click(() => {
$('.ui-import-from-browser').click(() => {
saveStorageHistoryToServer(() => {
parseStorageToHistory(historyList, parseHistoryCallback);
});
});
parseStorageToHistory(historyList, parseHistoryCallback)
})
})
$(".ui-save-history").click(() => {
$('.ui-save-history').click(() => {
getHistory(data => {
const history = JSON.stringify(data);
const history = JSON.stringify(data)
const blob = new Blob([history], {
type: "application/json;charset=utf-8"
});
saveAs(blob, `hackmd_history_${moment().format('YYYYMMDDHHmmss')}`, true);
});
});
type: 'application/json;charset=utf-8'
})
saveAs(blob, `hackmd_history_${moment().format('YYYYMMDDHHmmss')}`, true)
})
})
$(".ui-open-history").bind("change", e => {
const files = e.target.files || e.dataTransfer.files;
const file = files[0];
const reader = new FileReader();
$('.ui-open-history').bind('change', e => {
const files = e.target.files || e.dataTransfer.files
const file = files[0]
const reader = new FileReader()
reader.onload = () => {
const notehistory = JSON.parse(reader.result);
const notehistory = JSON.parse(reader.result)
// console.log(notehistory);
if (!reader.result) return;
if (!reader.result) return
getHistory(data => {
let mergedata = data.concat(notehistory);
mergedata = clearDuplicatedHistory(mergedata);
saveHistory(mergedata);
parseHistory(historyList, parseHistoryCallback);
});
$(".ui-open-history").replaceWith($(".ui-open-history").val('').clone(true));
};
reader.readAsText(file);
});
let mergedata = data.concat(notehistory)
mergedata = clearDuplicatedHistory(mergedata)
saveHistory(mergedata)
parseHistory(historyList, parseHistoryCallback)
})
$('.ui-open-history').replaceWith($('.ui-open-history').val('').clone(true))
}
reader.readAsText(file)
})
$(".ui-clear-history").click(() => {
$('.ui-delete-modal-msg').text('Do you really want to clear all history?');
$('.ui-delete-modal-item').html('There is no turning back.');
clearHistory = true;
deleteId = null;
});
$('.ui-clear-history').click(() => {
$('.ui-delete-modal-msg').text('Do you really want to clear all history?')
$('.ui-delete-modal-item').html('There is no turning back.')
clearHistory = true
deleteId = null
})
$(".ui-refresh-history").click(() => {
const lastTags = $(".ui-use-tags").select2('val');
$(".ui-use-tags").select2('val', '');
historyList.filter();
const lastKeyword = $('.search').val();
$('.search').val('');
historyList.search();
$('#history-list').slideUp('fast');
$('.pagination').hide();
$('.ui-refresh-history').click(() => {
const lastTags = $('.ui-use-tags').select2('val')
$('.ui-use-tags').select2('val', '')
historyList.filter()
const lastKeyword = $('.search').val()
$('.search').val('')
historyList.search()
$('#history-list').slideUp('fast')
$('.pagination').hide()
resetCheckAuth();
historyList.clear();
resetCheckAuth()
historyList.clear()
parseHistory(historyList, (list, notehistory) => {
parseHistoryCallback(list, notehistory);
$(".ui-use-tags").select2('val', lastTags);
$(".ui-use-tags").trigger('change');
historyList.search(lastKeyword);
$('.search').val(lastKeyword);
checkHistoryList();
$('#history-list').slideDown('fast');
});
});
parseHistoryCallback(list, notehistory)
$('.ui-use-tags').select2('val', lastTags)
$('.ui-use-tags').trigger('change')
historyList.search(lastKeyword)
$('.search').val(lastKeyword)
checkHistoryList()
$('#history-list').slideDown('fast')
})
})
$(".ui-logout").click(() => {
clearLoginState();
location.href = `${serverurl}/logout`;
});
$('.ui-logout').click(() => {
clearLoginState()
location.href = `${serverurl}/logout`
})
let filtertags = [];
$(".ui-use-tags").select2({
placeholder: $(".ui-use-tags").attr('placeholder'),
let filtertags = []
$('.ui-use-tags').select2({
placeholder: $('.ui-use-tags').attr('placeholder'),
multiple: true,
data () {
return {
results: filtertags
};
}
});
$('.select2-input').css('width', 'inherit');
buildTagsFilter([]);
}
})
$('.select2-input').css('width', 'inherit')
buildTagsFilter([])
function buildTagsFilter (tags) {
for (let i = 0; i < tags.length; i++)
for (let i = 0; i < tags.length; i++) {
tags[i] = {
id: i,
text: S(tags[i]).unescapeHTML().s
};
filtertags = tags;
}
$(".ui-use-tags").on('change', function () {
const tags = [];
const data = $(this).select2('data');
for (let i = 0; i < data.length; i++)
tags.push(data[i].text);
}
filtertags = tags
}
$('.ui-use-tags').on('change', function () {
const tags = []
const data = $(this).select2('data')
for (let i = 0; i < data.length; i++) { tags.push(data[i].text) }
if (tags.length > 0) {
historyList.filter(item => {
const values = item.values();
if (!values.tags) return false;
let found = false;
const values = item.values()
if (!values.tags) return false
let found = false
for (let i = 0; i < tags.length; i++) {
if (values.tags.includes(tags[i])) {
found = true;
break;
found = true
break
}
}
return found;
});
return found
})
} else {
historyList.filter();
historyList.filter()
}
checkHistoryList();
});
checkHistoryList()
})
$('.search').keyup(() => {
checkHistoryList();
});
checkHistoryList()
})

File diff suppressed because it is too large Load diff

View file

@ -8,23 +8,23 @@
*/
var FilePicker = window.FilePicker = function (options) {
// Config
this.apiKey = options.apiKey;
this.clientId = options.clientId;
this.apiKey = options.apiKey
this.clientId = options.clientId
// Elements
this.buttonEl = options.buttonEl;
this.buttonEl = options.buttonEl
// Events
this.onSelect = options.onSelect;
this.buttonEl.on('click', this.open.bind(this));
this.onSelect = options.onSelect
this.buttonEl.on('click', this.open.bind(this))
// Disable the button until the API loads, as it won't work properly until then.
this.buttonEl.prop('disabled', true);
this.buttonEl.prop('disabled', true)
// Load the drive API
gapi.client.setApiKey(this.apiKey);
gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this));
google.load('picker', '1', { callback: this._pickerApiLoaded.bind(this) });
window.gapi.client.setApiKey(this.apiKey)
window.gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this))
window.google.load('picker', '1', { callback: this._pickerApiLoaded.bind(this) })
}
FilePicker.prototype = {
@ -33,13 +33,13 @@
*/
open: function () {
// Check if the user has already authenticated
var token = gapi.auth.getToken();
var token = window.gapi.auth.getToken()
if (token) {
this._showPicker();
this._showPicker()
} else {
// The user has not yet authenticated with Google
// We need to do the authentication before displaying the Drive picker.
this._doAuth(false, function() { this._showPicker(); }.bind(this));
this._doAuth(false, function () { this._showPicker() }.bind(this))
}
},
@ -48,19 +48,19 @@
* @private
*/
_showPicker: function () {
var accessToken = gapi.auth.getToken().access_token;
var view = new google.picker.DocsView();
view.setMimeTypes("text/markdown,text/html");
view.setIncludeFolders(true);
view.setOwnedByMe(true);
this.picker = new google.picker.PickerBuilder().
enableFeature(google.picker.Feature.NAV_HIDDEN).
addView(view).
setAppId(this.clientId).
setOAuthToken(accessToken).
setCallback(this._pickerCallback.bind(this)).
build().
setVisible(true);
var accessToken = window.gapi.auth.getToken().access_token
var view = new window.google.picker.DocsView()
view.setMimeTypes('text/markdown,text/html')
view.setIncludeFolders(true)
view.setOwnedByMe(true)
this.picker = new window.google.picker.PickerBuilder()
.enableFeature(window.google.picker.Feature.NAV_HIDDEN)
.addView(view)
.setAppId(this.clientId)
.setOAuthToken(accessToken)
.setCallback(this._pickerCallback.bind(this))
.build()
.setVisible(true)
},
/**
@ -68,14 +68,13 @@
* @private
*/
_pickerCallback: function (data) {
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var file = data[google.picker.Response.DOCUMENTS][0],
id = file[google.picker.Document.ID],
request = gapi.client.drive.files.get({
if (data[window.google.picker.Response.ACTION] === window.google.picker.Action.PICKED) {
var file = data[window.google.picker.Response.DOCUMENTS][0]
var id = file[window.google.picker.Document.ID]
var request = window.gapi.client.drive.files.get({
fileId: id
});
request.execute(this._fileGetCallback.bind(this));
})
request.execute(this._fileGetCallback.bind(this))
}
},
/**
@ -84,7 +83,7 @@
*/
_fileGetCallback: function (file) {
if (this.onSelect) {
this.onSelect(file);
this.onSelect(file)
}
},
@ -93,7 +92,7 @@
* @private
*/
_pickerApiLoaded: function () {
this.buttonEl.prop('disabled', false);
this.buttonEl.prop('disabled', false)
},
/**
@ -101,7 +100,7 @@
* @private
*/
_driveApiLoaded: function () {
this._doAuth(true);
this._doAuth(true)
},
/**
@ -109,11 +108,11 @@
* @private
*/
_doAuth: function (immediate, callback) {
gapi.auth.authorize({
window.gapi.auth.authorize({
client_id: this.clientId,
scope: 'https://www.googleapis.com/auth/drive.readonly',
immediate: immediate
}, callback ? callback : function() {});
}, callback || function () {})
}
};
}());
}
}())

View file

@ -1,3 +1,4 @@
/* eslint-env browser, jquery */
/**
* Helper for implementing retries with backoff. Initial retry
* delay is 1 second, increasing by 2x (+jitter) for subsequent retries
@ -5,9 +6,9 @@
* @constructor
*/
var RetryHandler = function () {
this.interval = 1000; // Start at one second
this.maxInterval = 60 * 1000; // Don't wait longer than a minute
};
this.interval = 1000 // Start at one second
this.maxInterval = 60 * 1000 // Don't wait longer than a minute
}
/**
* Invoke the function after waiting
@ -15,16 +16,16 @@ var RetryHandler = function() {
* @param {function} fn Function to invoke
*/
RetryHandler.prototype.retry = function (fn) {
setTimeout(fn, this.interval);
this.interval = this.nextInterval_();
};
setTimeout(fn, this.interval)
this.interval = this.nextInterval_()
}
/**
* Reset the counter (e.g. after successful request.)
*/
RetryHandler.prototype.reset = function () {
this.interval = 1000;
};
this.interval = 1000
}
/**
* Calculate the next wait time.
@ -33,9 +34,9 @@ RetryHandler.prototype.reset = function() {
* @private
*/
RetryHandler.prototype.nextInterval_ = function () {
var interval = this.interval * 2 + this.getRandomInt_(0, 1000);
return Math.min(interval, this.maxInterval);
};
var interval = this.interval * 2 + this.getRandomInt_(0, 1000)
return Math.min(interval, this.maxInterval)
}
/**
* Get a random int in the range of min to max. Used to add jitter to wait times.
@ -45,9 +46,8 @@ RetryHandler.prototype.nextInterval_ = function() {
* @private
*/
RetryHandler.prototype.getRandomInt_ = function (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
};
return Math.floor(Math.random() * (max - min + 1) + min)
}
/**
* Helper class for resumable uploads using XHR/CORS. Can upload any Blob-like item, whether
@ -76,55 +76,54 @@ RetryHandler.prototype.getRandomInt_ = function(min, max) {
* @param {function} [options.onError] Callback if upload fails
*/
var MediaUploader = function (options) {
var noop = function() {};
this.file = options.file;
this.contentType = options.contentType || this.file.type || 'application/octet-stream';
var noop = function () {}
this.file = options.file
this.contentType = options.contentType || this.file.type || 'application/octet-stream'
this.metadata = options.metadata || {
'title': this.file.name,
'mimeType': this.contentType
};
this.token = options.token;
this.onComplete = options.onComplete || noop;
this.onProgress = options.onProgress || noop;
this.onError = options.onError || noop;
this.offset = options.offset || 0;
this.chunkSize = options.chunkSize || 0;
this.retryHandler = new RetryHandler();
this.url = options.url;
if (!this.url) {
var params = options.params || {};
params.uploadType = 'resumable';
this.url = this.buildUrl_(options.fileId, params, options.baseUrl);
}
this.httpMethod = options.fileId ? 'PUT' : 'POST';
};
this.token = options.token
this.onComplete = options.onComplete || noop
this.onProgress = options.onProgress || noop
this.onError = options.onError || noop
this.offset = options.offset || 0
this.chunkSize = options.chunkSize || 0
this.retryHandler = new RetryHandler()
this.url = options.url
if (!this.url) {
var params = options.params || {}
params.uploadType = 'resumable'
this.url = this.buildUrl_(options.fileId, params, options.baseUrl)
}
this.httpMethod = options.fileId ? 'PUT' : 'POST'
}
/**
* Initiate the upload.
*/
MediaUploader.prototype.upload = function () {
var self = this;
var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest()
xhr.open(this.httpMethod, this.url, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('X-Upload-Content-Length', this.file.size);
xhr.setRequestHeader('X-Upload-Content-Type', this.contentType);
xhr.open(this.httpMethod, this.url, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.setRequestHeader('X-Upload-Content-Length', this.file.size)
xhr.setRequestHeader('X-Upload-Content-Type', this.contentType)
xhr.onload = function (e) {
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location');
this.url = location;
this.sendFile_();
var location = e.target.getResponseHeader('Location')
this.url = location
this.sendFile_()
} else {
this.onUploadError_(e);
this.onUploadError_(e)
}
}.bind(this)
xhr.onerror = this.onUploadError_.bind(this)
xhr.send(JSON.stringify(this.metadata))
}
}.bind(this);
xhr.onerror = this.onUploadError_.bind(this);
xhr.send(JSON.stringify(this.metadata));
};
/**
* Send the actual file content.
@ -132,29 +131,29 @@ MediaUploader.prototype.upload = function() {
* @private
*/
MediaUploader.prototype.sendFile_ = function () {
var content = this.file;
var end = this.file.size;
var content = this.file
var end = this.file.size
if (this.offset || this.chunkSize) {
// Only bother to slice the file if we're either resuming or uploading in chunks
if (this.chunkSize) {
end = Math.min(this.offset + this.chunkSize, this.file.size);
end = Math.min(this.offset + this.chunkSize, this.file.size)
}
content = content.slice(this.offset, end);
content = content.slice(this.offset, end)
}
var xhr = new XMLHttpRequest();
xhr.open('PUT', this.url, true);
xhr.setRequestHeader('Content-Type', this.contentType);
xhr.setRequestHeader('Content-Range', "bytes " + this.offset + "-" + (end - 1) + "/" + this.file.size);
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type);
var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Type', this.contentType)
xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.file.size)
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type)
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress);
xhr.upload.addEventListener('progress', this.onProgress)
}
xhr.onload = this.onContentUploadSuccess_.bind(this)
xhr.onerror = this.onContentUploadError_.bind(this)
xhr.send(content)
}
xhr.onload = this.onContentUploadSuccess_.bind(this);
xhr.onerror = this.onContentUploadError_.bind(this);
xhr.send(content);
};
/**
* Query for the state of the file for resumption.
@ -162,17 +161,17 @@ MediaUploader.prototype.sendFile_ = function() {
* @private
*/
MediaUploader.prototype.resume_ = function () {
var xhr = new XMLHttpRequest();
xhr.open('PUT', this.url, true);
xhr.setRequestHeader('Content-Range', "bytes */" + this.file.size);
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type);
var xhr = new XMLHttpRequest()
xhr.open('PUT', this.url, true)
xhr.setRequestHeader('Content-Range', 'bytes */' + this.file.size)
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type)
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress);
xhr.upload.addEventListener('progress', this.onProgress)
}
xhr.onload = this.onContentUploadSuccess_.bind(this)
xhr.onerror = this.onContentUploadError_.bind(this)
xhr.send()
}
xhr.onload = this.onContentUploadSuccess_.bind(this);
xhr.onerror = this.onContentUploadError_.bind(this);
xhr.send();
};
/**
* Extract the last saved range if available in the request.
@ -180,11 +179,11 @@ MediaUploader.prototype.resume_ = function() {
* @param {XMLHttpRequest} xhr Request object
*/
MediaUploader.prototype.extractRange_ = function (xhr) {
var range = xhr.getResponseHeader('Range');
var range = xhr.getResponseHeader('Range')
if (range) {
this.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1;
this.offset = parseInt(range.match(/\d+/g).pop(), 10) + 1
}
}
};
/**
* Handle successful responses for uploads. Depending on the context,
@ -195,16 +194,16 @@ MediaUploader.prototype.extractRange_ = function(xhr) {
* @param {object} e XHR event
*/
MediaUploader.prototype.onContentUploadSuccess_ = function (e) {
if (e.target.status == 200 || e.target.status == 201) {
this.onComplete(e.target.response);
} else if (e.target.status == 308) {
this.extractRange_(e.target);
this.retryHandler.reset();
this.sendFile_();
if (e.target.status === 200 || e.target.status === 201) {
this.onComplete(e.target.response)
} else if (e.target.status === 308) {
this.extractRange_(e.target)
this.retryHandler.reset()
this.sendFile_()
} else {
this.onContentUploadError_(e);
this.onContentUploadError_(e)
}
}
};
/**
* Handles errors for uploads. Either retries or aborts depending
@ -215,11 +214,11 @@ MediaUploader.prototype.onContentUploadSuccess_ = function(e) {
*/
MediaUploader.prototype.onContentUploadError_ = function (e) {
if (e.target.status && e.target.status < 500) {
this.onError(e.target.response);
this.onError(e.target.response)
} else {
this.retryHandler.retry(this.resume_.bind(this));
this.retryHandler.retry(this.resume_.bind(this))
}
}
};
/**
* Handles errors for the initial request.
@ -228,8 +227,8 @@ MediaUploader.prototype.onContentUploadError_ = function(e) {
* @param {object} e XHR event
*/
MediaUploader.prototype.onUploadError_ = function (e) {
this.onError(e.target.response); // TODO - Retries for initial upload
};
this.onError(e.target.response) // TODO - Retries for initial upload
}
/**
* Construct a query string from a hash/object
@ -239,11 +238,11 @@ MediaUploader.prototype.onUploadError_ = function(e) {
* @return {string} query string
*/
MediaUploader.prototype.buildQuery_ = function (params) {
params = params || {};
params = params || {}
return Object.keys(params).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');
};
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
}).join('&')
}
/**
* Build the drive upload URL
@ -254,15 +253,15 @@ MediaUploader.prototype.buildQuery_ = function(params) {
* @return {string} URL
*/
MediaUploader.prototype.buildUrl_ = function (id, params, baseUrl) {
var url = baseUrl || 'https://www.googleapis.com/upload/drive/v2/files/';
var url = baseUrl || 'https://www.googleapis.com/upload/drive/v2/files/'
if (id) {
url += id;
url += id
}
var query = this.buildQuery_(params);
var query = this.buildQuery_(params)
if (query) {
url += '?' + query;
url += '?' + query
}
return url
}
return url;
};
window.MediaUploader = MediaUploader;
window.MediaUploader = MediaUploader

View file

@ -1,125 +1,110 @@
import store from 'store';
import S from 'string';
/* eslint-env browser, jquery */
/* global serverurl, Cookies, moment */
import store from 'store'
import S from 'string'
import {
checkIfAuth
} from './lib/common/login';
} from './lib/common/login'
import {
urlpath
} from './lib/config';
} from './lib/config'
window.migrateHistoryFromTempCallback = null;
window.migrateHistoryFromTempCallback = null
migrateHistoryFromTemp();
migrateHistoryFromTemp()
function migrateHistoryFromTemp () {
if (url('#tempid')) {
if (window.url('#tempid')) {
$.get(`${serverurl}/temp`, {
tempid: url('#tempid')
tempid: window.url('#tempid')
})
.done(data => {
if (data && data.temp) {
getStorageHistory(olddata => {
if (!olddata || olddata.length == 0) {
saveHistoryToStorage(JSON.parse(data.temp));
if (!olddata || olddata.length === 0) {
saveHistoryToStorage(JSON.parse(data.temp))
}
});
})
}
})
.always(() => {
let hash = location.hash.split('#')[1];
hash = hash.split('&');
for (let i = 0; i < hash.length; i++)
if (hash[i].indexOf('tempid') == 0) {
hash.splice(i, 1);
i--;
let hash = location.hash.split('#')[1]
hash = hash.split('&')
for (let i = 0; i < hash.length; i++) {
if (hash[i].indexOf('tempid') === 0) {
hash.splice(i, 1)
i--
}
hash = hash.join('&');
location.hash = hash;
if (migrateHistoryFromTempCallback)
migrateHistoryFromTempCallback();
});
}
hash = hash.join('&')
location.hash = hash
if (window.migrateHistoryFromTempCallback) { window.migrateHistoryFromTempCallback() }
})
}
}
export function saveHistory (notehistory) {
checkIfAuth(
() => {
saveHistoryToServer(notehistory);
saveHistoryToServer(notehistory)
},
() => {
saveHistoryToStorage(notehistory);
saveHistoryToStorage(notehistory)
}
);
)
}
function saveHistoryToStorage (notehistory) {
if (store.enabled)
store.set('notehistory', JSON.stringify(notehistory));
else
saveHistoryToCookie(notehistory);
if (store.enabled) { store.set('notehistory', JSON.stringify(notehistory)) } else { saveHistoryToCookie(notehistory) }
}
function saveHistoryToCookie (notehistory) {
Cookies.set('notehistory', notehistory, {
expires: 365
});
})
}
function saveHistoryToServer (notehistory) {
$.post(`${serverurl}/history`, {
history: JSON.stringify(notehistory)
});
}
function saveCookieHistoryToStorage(callback) {
store.set('notehistory', Cookies.get('notehistory'));
callback();
})
}
export function saveStorageHistoryToServer (callback) {
const data = store.get('notehistory');
const data = store.get('notehistory')
if (data) {
$.post(`${serverurl}/history`, {
history: data
})
.done(data => {
callback(data);
});
}
}
function saveCookieHistoryToServer(callback) {
$.post(`${serverurl}/history`, {
history: Cookies.get('notehistory')
callback(data)
})
.done(data => {
callback(data);
});
}
}
export function clearDuplicatedHistory (notehistory) {
const newnotehistory = [];
const newnotehistory = []
for (let i = 0; i < notehistory.length; i++) {
let found = false;
let found = false
for (let j = 0; j < newnotehistory.length; j++) {
const id = notehistory[i].id.replace(/\=+$/, '');
const newId = newnotehistory[j].id.replace(/\=+$/, '');
if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
const time = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
const newTime = (typeof newnotehistory[i].time === 'number' ? moment(newnotehistory[i].time) : moment(newnotehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
const id = notehistory[i].id.replace(/=+$/, '')
const newId = newnotehistory[j].id.replace(/=+$/, '')
if (id === newId || notehistory[i].id === newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
const time = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'))
const newTime = (typeof newnotehistory[i].time === 'number' ? moment(newnotehistory[i].time) : moment(newnotehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'))
if (time >= newTime) {
newnotehistory[j] = notehistory[i];
newnotehistory[j] = notehistory[i]
}
found = true;
break;
found = true
break
}
}
if (!found)
newnotehistory.push(notehistory[i]);
if (!found) { newnotehistory.push(notehistory[i]) }
}
return newnotehistory;
return newnotehistory
}
function addHistory (id, text, time, tags, pinned, notehistory) {
@ -131,19 +116,19 @@ function addHistory(id, text, time, tags, pinned, notehistory) {
time,
tags,
pinned
});
})
}
return notehistory;
return notehistory
}
export function removeHistory (id, notehistory) {
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == id) {
notehistory.splice(i, 1);
i -= 1;
if (notehistory[i].id === id) {
notehistory.splice(i, 1)
i -= 1
}
}
return notehistory;
return notehistory
}
// used for inner
@ -154,219 +139,190 @@ export function writeHistory(title, tags) {
// writeHistoryToServer(title, tags);
},
() => {
writeHistoryToStorage(title, tags);
writeHistoryToStorage(title, tags)
}
);
}
function writeHistoryToServer(title, tags) {
$.get(`${serverurl}/history`)
.done(data => {
try {
if (data.history) {
var notehistory = data.history;
} else {
var notehistory = [];
}
} catch (err) {
var notehistory = [];
}
if (!notehistory)
notehistory = [];
const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToServer(newnotehistory);
})
.fail((xhr, status, error) => {
console.error(xhr.responseText);
});
)
}
function writeHistoryToCookie (title, tags) {
var notehistory
try {
var notehistory = Cookies.getJSON('notehistory');
notehistory = Cookies.getJSON('notehistory')
} catch (err) {
var notehistory = [];
notehistory = []
}
if (!notehistory)
notehistory = [];
const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToCookie(newnotehistory);
if (!notehistory) { notehistory = [] }
const newnotehistory = generateHistory(title, tags, notehistory)
saveHistoryToCookie(newnotehistory)
}
function writeHistoryToStorage (title, tags) {
if (store.enabled) {
let data = store.get('notehistory');
let data = store.get('notehistory')
var notehistory
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
var notehistory = data;
} else
var notehistory = [];
if (!notehistory)
notehistory = [];
const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToStorage(newnotehistory);
if (typeof data === 'string') { data = JSON.parse(data) }
notehistory = data
} else {
writeHistoryToCookie(title, tags);
notehistory = []
}
if (!notehistory) { notehistory = [] }
const newnotehistory = generateHistory(title, tags, notehistory)
saveHistoryToStorage(newnotehistory)
} else {
writeHistoryToCookie(title, tags)
}
}
if (!Array.isArray) {
Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]';
Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]'
}
function renderHistory (title, tags) {
// console.debug(tags);
const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1];
const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1]
return {
id,
text: title,
time: moment().valueOf(),
tags
};
}
}
function generateHistory (title, tags, notehistory) {
const info = renderHistory(title, tags);
const info = renderHistory(title, tags)
// keep any pinned data
let pinned = false;
let pinned = false
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == info.id && notehistory[i].pinned) {
pinned = true;
break;
if (notehistory[i].id === info.id && notehistory[i].pinned) {
pinned = true
break
}
}
notehistory = removeHistory(info.id, notehistory);
notehistory = addHistory(info.id, info.text, info.time, info.tags, pinned, notehistory);
notehistory = clearDuplicatedHistory(notehistory);
return notehistory;
notehistory = removeHistory(info.id, notehistory)
notehistory = addHistory(info.id, info.text, info.time, info.tags, pinned, notehistory)
notehistory = clearDuplicatedHistory(notehistory)
return notehistory
}
// used for outer
export function getHistory (callback) {
checkIfAuth(
() => {
getServerHistory(callback);
getServerHistory(callback)
},
() => {
getStorageHistory(callback);
getStorageHistory(callback)
}
);
)
}
function getServerHistory (callback) {
$.get(`${serverurl}/history`)
.done(data => {
if (data.history) {
callback(data.history);
callback(data.history)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText);
});
console.error(xhr.responseText)
})
}
function getCookieHistory (callback) {
callback(Cookies.getJSON('notehistory'));
callback(Cookies.getJSON('notehistory'))
}
export function getStorageHistory (callback) {
if (store.enabled) {
let data = store.get('notehistory');
let data = store.get('notehistory')
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
callback(data);
} else
getCookieHistory(callback);
if (typeof data === 'string') { data = JSON.parse(data) }
callback(data)
} else { getCookieHistory(callback) }
} else {
getCookieHistory(callback);
getCookieHistory(callback)
}
}
export function parseHistory (list, callback) {
checkIfAuth(
() => {
parseServerToHistory(list, callback);
parseServerToHistory(list, callback)
},
() => {
parseStorageToHistory(list, callback);
parseStorageToHistory(list, callback)
}
);
)
}
export function parseServerToHistory (list, callback) {
$.get(`${serverurl}/history`)
.done(data => {
if (data.history) {
parseToHistory(list, data.history, callback);
parseToHistory(list, data.history, callback)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText);
});
console.error(xhr.responseText)
})
}
function parseCookieToHistory (list, callback) {
const notehistory = Cookies.getJSON('notehistory');
parseToHistory(list, notehistory, callback);
const notehistory = Cookies.getJSON('notehistory')
parseToHistory(list, notehistory, callback)
}
export function parseStorageToHistory (list, callback) {
if (store.enabled) {
let data = store.get('notehistory');
let data = store.get('notehistory')
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
parseToHistory(list, data, callback);
} else
parseCookieToHistory(list, callback);
if (typeof data === 'string') { data = JSON.parse(data) }
parseToHistory(list, data, callback)
} else { parseCookieToHistory(list, callback) }
} else {
parseCookieToHistory(list, callback);
parseCookieToHistory(list, callback)
}
}
function parseToHistory (list, notehistory, callback) {
if (!callback) return;
else if (!list || !notehistory) callback(list, notehistory);
if (!callback) return
else if (!list || !notehistory) callback(list, notehistory)
else if (notehistory && notehistory.length > 0) {
for (let i = 0; i < notehistory.length; i++) {
// parse time to timestamp and fromNow
const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
notehistory[i].timestamp = timestamp.valueOf();
notehistory[i].fromNow = timestamp.fromNow();
notehistory[i].time = timestamp.format('llll');
const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'))
notehistory[i].timestamp = timestamp.valueOf()
notehistory[i].fromNow = timestamp.fromNow()
notehistory[i].time = timestamp.format('llll')
// prevent XSS
notehistory[i].text = S(notehistory[i].text).escapeHTML().s;
notehistory[i].tags = (notehistory[i].tags && notehistory[i].tags.length > 0) ? S(notehistory[i].tags).escapeHTML().s.split(',') : [];
notehistory[i].text = S(notehistory[i].text).escapeHTML().s
notehistory[i].tags = (notehistory[i].tags && notehistory[i].tags.length > 0) ? S(notehistory[i].tags).escapeHTML().s.split(',') : []
// add to list
if (notehistory[i].id && list.get('id', notehistory[i].id).length == 0)
list.add(notehistory[i]);
if (notehistory[i].id && list.get('id', notehistory[i].id).length === 0) { list.add(notehistory[i]) }
}
}
callback(list, notehistory);
callback(list, notehistory)
}
export function postHistoryToServer (noteId, data, callback) {
$.post(`${serverurl}/history/${noteId}`, data)
.done(result => callback(null, result))
.fail((xhr, status, error) => {
console.error(xhr.responseText);
return callback(error, null);
});
console.error(xhr.responseText)
return callback(error, null)
})
}
export function deleteServerHistory (noteId, callback) {
$.ajax({
url: `${serverurl}/history${noteId ? '/' + noteId : ""}`,
url: `${serverurl}/history${noteId ? '/' + noteId : ''}`,
type: 'DELETE'
})
.done(result => callback(null, result))
.fail((xhr, status, error) => {
console.error(xhr.responseText);
return callback(error, null);
});
console.error(xhr.responseText)
return callback(error, null)
})
}

View file

@ -1,6 +1,6 @@
require('../css/github-extract.css');
require('../css/markdown.css');
require('../css/extra.css');
require('../css/slide-preview.css');
require('../css/google-font.css');
require('../css/site.css');
require('../css/github-extract.css')
require('../css/markdown.css')
require('../css/extra.css')
require('../css/slide-preview.css')
require('../css/google-font.css')
require('../css/site.css')

File diff suppressed because it is too large Load diff

View file

@ -1,82 +1,85 @@
import { serverurl } from '../config';
/* eslint-env browser, jquery */
/* global Cookies */
let checkAuth = false;
let profile = null;
let lastLoginState = getLoginState();
let lastUserId = getUserId();
var loginStateChangeEvent = null;
import { serverurl } from '../config'
let checkAuth = false
let profile = null
let lastLoginState = getLoginState()
let lastUserId = getUserId()
var loginStateChangeEvent = null
export function setloginStateChangeEvent (func) {
loginStateChangeEvent = func;
loginStateChangeEvent = func
}
export function resetCheckAuth () {
checkAuth = false;
checkAuth = false
}
export function setLoginState (bool, id) {
Cookies.set('loginstate', bool, {
expires: 365
});
})
if (id) {
Cookies.set('userid', id, {
expires: 365
});
})
} else {
Cookies.remove('userid');
Cookies.remove('userid')
}
lastLoginState = bool;
lastUserId = id;
checkLoginStateChanged();
lastLoginState = bool
lastUserId = id
checkLoginStateChanged()
}
export function checkLoginStateChanged () {
if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
if (loginStateChangeEvent) setTimeout(loginStateChangeEvent, 100);
return true;
if (getLoginState() !== lastLoginState || getUserId() !== lastUserId) {
if (loginStateChangeEvent) setTimeout(loginStateChangeEvent, 100)
return true
} else {
return false;
return false
}
}
export function getLoginState () {
const state = Cookies.get('loginstate');
return state === "true" || state === true;
const state = Cookies.get('loginstate')
return state === 'true' || state === true
}
export function getUserId () {
return Cookies.get('userid');
return Cookies.get('userid')
}
export function clearLoginState () {
Cookies.remove('loginstate');
Cookies.remove('loginstate')
}
export function checkIfAuth (yesCallback, noCallback) {
const cookieLoginState = getLoginState();
if (checkLoginStateChanged()) checkAuth = false;
if (!checkAuth || typeof cookieLoginState == 'undefined') {
const cookieLoginState = getLoginState()
if (checkLoginStateChanged()) checkAuth = false
if (!checkAuth || typeof cookieLoginState === 'undefined') {
$.get(`${serverurl}/me`)
.done(data => {
if (data && data.status == 'ok') {
profile = data;
yesCallback(profile);
setLoginState(true, data.id);
if (data && data.status === 'ok') {
profile = data
yesCallback(profile)
setLoginState(true, data.id)
} else {
noCallback();
setLoginState(false);
noCallback()
setLoginState(false)
}
})
.fail(() => {
noCallback();
noCallback()
})
.always(() => {
checkAuth = true;
});
checkAuth = true
})
} else if (cookieLoginState) {
yesCallback(profile);
yesCallback(profile)
} else {
noCallback();
noCallback()
}
}
@ -86,4 +89,4 @@ export default {
lastLoginState,
lastUserId,
loginStateChangeEvent
};
}

View file

@ -1,19 +1,19 @@
import configJson from '../../../../config.json'; // root path json config
import configJson from '../../../../config.json' // root path json config
const config = 'production' === process.env.NODE_ENV ? configJson.production : configJson.development;
const config = process.env.NODE_ENV === 'production' ? configJson.production : configJson.development
export const GOOGLE_API_KEY = (config.google && config.google.apiKey) || '';
export const GOOGLE_CLIENT_ID = (config.google && config.google.clientID) || '';
export const DROPBOX_APP_KEY = (config.dropbox && config.dropbox.appKey) || '';
export const GOOGLE_API_KEY = (config.google && config.google.apiKey) || ''
export const GOOGLE_CLIENT_ID = (config.google && config.google.clientID) || ''
export const DROPBOX_APP_KEY = (config.dropbox && config.dropbox.appKey) || ''
export const domain = config.domain || ''; // domain name
export const urlpath = config.urlpath || ''; // sub url path, like: www.example.com/<urlpath>
export const debug = config.debug || false;
export const domain = config.domain || '' // domain name
export const urlpath = config.urlpath || '' // sub url path, like: www.example.com/<urlpath>
export const debug = config.debug || false
export const port = window.location.port;
export const serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`;
window.serverurl = serverurl;
export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];
export const noteurl = `${serverurl}/${noteid}`;
export const port = window.location.port
export const serverurl = `${window.location.protocol}//${domain || window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`
window.serverurl = serverurl
export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]
export const noteurl = `${serverurl}/${noteid}`
export const version = '0.5.0';
export const version = '0.5.0'

View file

@ -1,26 +1,28 @@
var lang = "en";
var userLang = navigator.language || navigator.userLanguage;
var userLangCode = userLang.split('-')[0];
var userCountryCode = userLang.split('-')[1];
var locale = $('.ui-locale');
var supportLangs = [];
$(".ui-locale option").each(function() {
supportLangs.push($(this).val());
});
/* eslint-env browser, jquery */
/* global Cookies */
var lang = 'en'
var userLang = navigator.language || navigator.userLanguage
var userLangCode = userLang.split('-')[0]
var locale = $('.ui-locale')
var supportLangs = []
$('.ui-locale option').each(function () {
supportLangs.push($(this).val())
})
if (Cookies.get('locale')) {
lang = Cookies.get('locale');
lang = Cookies.get('locale')
} else if (supportLangs.indexOf(userLang) !== -1) {
lang = supportLangs[supportLangs.indexOf(userLang)];
lang = supportLangs[supportLangs.indexOf(userLang)]
} else if (supportLangs.indexOf(userLangCode) !== -1) {
lang = supportLangs[supportLangs.indexOf(userLangCode)];
lang = supportLangs[supportLangs.indexOf(userLangCode)]
}
locale.val(lang);
$('select.ui-locale option[value="' + lang + '"]').attr('selected','selected');
locale.val(lang)
$('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected')
locale.change(function () {
Cookies.set('locale', $(this).val(), {
expires: 365
});
window.location.reload();
});
})
window.location.reload()
})

View file

@ -1,8 +1,11 @@
require('../css/extra.css');
require('../css/slide-preview.css');
require('../css/site.css');
/* eslint-env browser, jquery */
/* global refreshView */
require('highlight.js/styles/github-gist.css');
require('../css/extra.css')
require('../css/slide-preview.css')
require('../css/site.css')
require('highlight.js/styles/github-gist.css')
import {
autoLinkify,
@ -16,126 +19,126 @@ import {
scrollToHash,
smoothHashScroll,
updateLastChange
} from './extra';
} from './extra'
import { preventXSS } from './render';
import { preventXSS } from './render'
const markdown = $("#doc.markdown-body");
const text = markdown.text();
const lastMeta = md.meta;
md.meta = {};
delete md.metaError;
let rendered = md.render(text);
const markdown = $('#doc.markdown-body')
const text = markdown.text()
const lastMeta = md.meta
md.meta = {}
delete md.metaError
let rendered = md.render(text)
if (md.meta.type && md.meta.type === 'slide') {
const slideOptions = {
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$'
};
const slides = RevealMarkdown.slidify(text, slideOptions);
markdown.html(slides);
RevealMarkdown.initialize();
}
const slides = window.RevealMarkdown.slidify(text, slideOptions)
markdown.html(slides)
window.RevealMarkdown.initialize()
// prevent XSS
markdown.html(preventXSS(markdown.html()));
markdown.addClass('slides');
markdown.html(preventXSS(markdown.html()))
markdown.addClass('slides')
} else {
if (lastMeta.type && lastMeta.type === 'slide') {
refreshView();
markdown.removeClass('slides');
refreshView()
markdown.removeClass('slides')
}
// only render again when meta changed
if (JSON.stringify(md.meta) != JSON.stringify(lastMeta)) {
parseMeta(md, null, markdown, $('#ui-toc'), $('#ui-toc-affix'));
rendered = md.render(text);
if (JSON.stringify(md.meta) !== JSON.stringify(lastMeta)) {
parseMeta(md, null, markdown, $('#ui-toc'), $('#ui-toc-affix'))
rendered = md.render(text)
}
// prevent XSS
rendered = preventXSS(rendered);
const result = postProcess(rendered);
markdown.html(result.html());
rendered = preventXSS(rendered)
const result = postProcess(rendered)
markdown.html(result.html())
}
$(document.body).show();
$(document.body).show()
finishView(markdown);
autoLinkify(markdown);
deduplicatedHeaderId(markdown);
renderTOC(markdown);
generateToc('ui-toc');
generateToc('ui-toc-affix');
smoothHashScroll();
createtime = lastchangeui.time.attr('data-createtime');
lastchangetime = lastchangeui.time.attr('data-updatetime');
updateLastChange();
finishView(markdown)
autoLinkify(markdown)
deduplicatedHeaderId(markdown)
renderTOC(markdown)
generateToc('ui-toc')
generateToc('ui-toc-affix')
smoothHashScroll()
window.createtime = window.lastchangeui.time.attr('data-createtime')
window.lastchangetime = window.lastchangeui.time.attr('data-updatetime')
updateLastChange()
const url = window.location.pathname;
$('.ui-edit').attr('href', `${url}/edit`);
const toc = $('.ui-toc');
const tocAffix = $('.ui-affix-toc');
const tocDropdown = $('.ui-toc-dropdown');
const url = window.location.pathname
$('.ui-edit').attr('href', `${url}/edit`)
const toc = $('.ui-toc')
const tocAffix = $('.ui-affix-toc')
const tocDropdown = $('.ui-toc-dropdown')
// toc
tocDropdown.click(e => {
e.stopPropagation();
});
e.stopPropagation()
})
let enoughForAffixToc = true;
let enoughForAffixToc = true
function generateScrollspy () {
$(document.body).scrollspy({
target: ''
});
$(document.body).scrollspy('refresh');
})
$(document.body).scrollspy('refresh')
if (enoughForAffixToc) {
toc.hide();
tocAffix.show();
toc.hide()
tocAffix.show()
} else {
tocAffix.hide();
toc.show();
tocAffix.hide()
toc.show()
}
$(document.body).scroll();
$(document.body).scroll()
}
function windowResize () {
// toc right
const paddingRight = parseFloat(markdown.css('padding-right'));
const right = ($(window).width() - (markdown.offset().left + markdown.outerWidth() - paddingRight));
toc.css('right', `${right}px`);
const paddingRight = parseFloat(markdown.css('padding-right'))
const right = ($(window).width() - (markdown.offset().left + markdown.outerWidth() - paddingRight))
toc.css('right', `${right}px`)
// affix toc left
let newbool;
const rightMargin = (markdown.parent().outerWidth() - markdown.outerWidth()) / 2;
let newbool
const rightMargin = (markdown.parent().outerWidth() - markdown.outerWidth()) / 2
// for ipad or wider device
if (rightMargin >= 133) {
newbool = true;
const affixLeftMargin = (tocAffix.outerWidth() - tocAffix.width()) / 2;
const left = markdown.offset().left + markdown.outerWidth() - affixLeftMargin;
tocAffix.css('left', `${left}px`);
newbool = true
const affixLeftMargin = (tocAffix.outerWidth() - tocAffix.width()) / 2
const left = markdown.offset().left + markdown.outerWidth() - affixLeftMargin
tocAffix.css('left', `${left}px`)
} else {
newbool = false;
newbool = false
}
if (newbool != enoughForAffixToc) {
enoughForAffixToc = newbool;
generateScrollspy();
if (newbool !== enoughForAffixToc) {
enoughForAffixToc = newbool
generateScrollspy()
}
}
$(window).resize(() => {
windowResize();
});
windowResize()
})
$(document).ready(() => {
windowResize();
generateScrollspy();
setTimeout(scrollToHash, 0);
windowResize()
generateScrollspy()
setTimeout(scrollToHash, 0)
// tooltip
$('[data-toggle="tooltip"]').tooltip();
});
$('[data-toggle="tooltip"]').tooltip()
})
export function scrollToTop () {
$('body, html').stop(true, true).animate({
scrollTop: 0
}, 100, "linear");
}, 100, 'linear')
}
export function scrollToBottom () {
$('body, html').stop(true, true).animate({
scrollTop: $(document.body)[0].scrollHeight
}, 100, "linear");
}, 100, 'linear')
}
window.scrollToTop = scrollToTop;
window.scrollToBottom = scrollToBottom;
window.scrollToTop = scrollToTop
window.scrollToBottom = scrollToBottom

View file

@ -1,62 +1,64 @@
/* eslint-env browser, jquery */
/* global filterXSS */
// allow some attributes
var whiteListAttr = ['id', 'class', 'style'];
window.whiteListAttr = whiteListAttr;
var whiteListAttr = ['id', 'class', 'style']
window.whiteListAttr = whiteListAttr
// allow link starts with '.', '/' and custom protocol with '://'
var linkRegex = /^([\w|-]+:\/\/)|^([\.|\/])+/;
var linkRegex = /^([\w|-]+:\/\/)|^([.|/])+/
// allow data uri, from https://gist.github.com/bgrins/6194623
var dataUriRegex = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*)\s*$/i;
var dataUriRegex = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*)\s*$/i
// custom white list
var whiteList = filterXSS.whiteList;
var whiteList = filterXSS.whiteList
// allow ol specify start number
whiteList['ol'] = ['start'];
whiteList['ol'] = ['start']
// allow li specify value number
whiteList['li'] = ['value'];
whiteList['li'] = ['value']
// allow style tag
whiteList['style'] = [];
whiteList['style'] = []
// allow kbd tag
whiteList['kbd'] = [];
whiteList['kbd'] = []
// allow ifram tag with some safe attributes
whiteList['iframe'] = ['allowfullscreen', 'name', 'referrerpolicy', 'sandbox', 'src', 'srcdoc', 'width', 'height'];
whiteList['iframe'] = ['allowfullscreen', 'name', 'referrerpolicy', 'sandbox', 'src', 'srcdoc', 'width', 'height']
// allow summary tag
whiteList['summary'] = [];
whiteList['summary'] = []
var filterXSSOptions = {
allowCommentTag: true,
whiteList: whiteList,
escapeHtml: function (html) {
// allow html comment in multiple lines
return html.replace(/<(.*?)>/g, '&lt;$1&gt;');
return html.replace(/<(.*?)>/g, '&lt;$1&gt;')
},
onIgnoreTag: function (tag, html, options) {
// allow comment tag
if (tag == "!--") {
if (tag === '!--') {
// do not filter its attributes
return html;
return html
}
},
onTagAttr: function (tag, name, value, isWhiteAttr) {
// allow href and src that match linkRegex
if (isWhiteAttr && (name === 'href' || name === 'src') && linkRegex.test(value)) {
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
}
// allow data uri in img src
if (isWhiteAttr && (tag == "img" && name === 'src') && dataUriRegex.test(value)) {
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
if (isWhiteAttr && (tag === 'img' && name === 'src') && dataUriRegex.test(value)) {
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
}
},
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
// allow attr start with 'data-' or in the whiteListAttr
if (name.substr(0, 5) === 'data-' || whiteListAttr.indexOf(name) !== -1) {
if (name.substr(0, 5) === 'data-' || window.whiteListAttr.indexOf(name) !== -1) {
// escape its value using built-in escapeAttrValue function
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
}
}
}
};
function preventXSS (html) {
return filterXSS(html, filterXSSOptions);
return filterXSS(html, filterXSSOptions)
}
window.preventXSS = preventXSS;
window.preventXSS = preventXSS
module.exports = {
preventXSS: preventXSS

View file

@ -1,3 +1,8 @@
/* eslint-env browser, jquery */
import { preventXSS } from './render'
import { md } from './extra'
/**
* The reveal.js markdown plugin. Handles parsing of
* markdown inside of presentations as well as loading
@ -5,49 +10,43 @@
*/
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
}
else {
module.exports = factory()
} else {
// Browser globals (root is window)
root.RevealMarkdown = factory();
root.RevealMarkdown.initialize();
root.RevealMarkdown = factory()
root.RevealMarkdown.initialize()
}
}(this, function () {
var DEFAULT_SLIDE_SEPARATOR = '^\r?\n---\r?\n$'
var DEFAULT_NOTES_SEPARATOR = 'note:'
var DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '\\.element\\s*?(.+?)$'
var DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR = '\\.slide:\\s*?(\\S.+?)$'
var DEFAULT_SLIDE_SEPARATOR = '^\r?\n---\r?\n$',
DEFAULT_NOTES_SEPARATOR = 'note:',
DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '\\\.element\\\s*?(.+?)$',
DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR = '\\\.slide:\\\s*?(\\\S.+?)$';
var SCRIPT_END_PLACEHOLDER = '__SCRIPT_END__';
var SCRIPT_END_PLACEHOLDER = '__SCRIPT_END__'
/**
* Retrieves the markdown contents of a slide section
* element. Normalizes leading tabs/whitespace.
*/
function getMarkdownFromSlide (section) {
var template = section.querySelector( 'script' );
var template = section.querySelector('script')
// strip leading whitespace so it isn't evaluated as code
var text = ( template || section ).textContent;
var text = (template || section).textContent
// restore script end tags
text = text.replace( new RegExp( SCRIPT_END_PLACEHOLDER, 'g' ), '</script>' );
text = text.replace(new RegExp(SCRIPT_END_PLACEHOLDER, 'g'), '</script>')
var leadingWs = text.match( /^\n?(\s*)/ )[1].length,
leadingTabs = text.match( /^\n?(\t*)/ )[1].length;
var leadingWs = text.match(/^\n?(\s*)/)[1].length
var leadingTabs = text.match(/^\n?(\t*)/)[1].length
if (leadingTabs > 0) {
text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' );
}
else if( leadingWs > 1 ) {
text = text.replace( new RegExp('\\n? {' + leadingWs + '}', 'g'), '\n' );
text = text.replace(new RegExp('\\n?\\t{' + leadingTabs + '}', 'g'), '\n')
} else if (leadingWs > 1) {
text = text.replace(new RegExp('\\n? {' + leadingWs + '}', 'g'), '\n')
}
return text;
return text
}
/**
@ -57,27 +56,24 @@
* to the output markdown slide.
*/
function getForwardedAttributes (section) {
var attributes = section.attributes;
var result = [];
var attributes = section.attributes
var result = []
for (var i = 0, len = attributes.length; i < len; i++) {
var name = attributes[i].name,
value = attributes[i].value;
var name = attributes[i].name
var value = attributes[i].value
// disregard attributes that are used for markdown loading/parsing
if( /data\-(markdown|separator|vertical|notes)/gi.test( name ) ) continue;
if (/data-(markdown|separator|vertical|notes)/gi.test(name)) continue
if (value) {
result.push( name + '="' + value + '"' );
}
else {
result.push( name );
result.push(name + '="' + value + '"')
} else {
result.push(name)
}
}
return result.join( ' ' );
return result.join(' ')
}
/**
@ -85,35 +81,31 @@
* values for what's not defined.
*/
function getSlidifyOptions (options) {
options = options || {}
options.separator = options.separator || DEFAULT_SLIDE_SEPARATOR
options.notesSeparator = options.notesSeparator || DEFAULT_NOTES_SEPARATOR
options.attributes = options.attributes || ''
options = options || {};
options.separator = options.separator || DEFAULT_SLIDE_SEPARATOR;
options.notesSeparator = options.notesSeparator || DEFAULT_NOTES_SEPARATOR;
options.attributes = options.attributes || '';
return options;
return options
}
/**
* Helper function for constructing a markdown slide.
*/
function createMarkdownSlide (content, options) {
options = getSlidifyOptions(options)
options = getSlidifyOptions( options );
var notesMatch = content.split( new RegExp( options.notesSeparator, 'mgi' ) );
var notesMatch = content.split(new RegExp(options.notesSeparator, 'mgi'))
if (notesMatch.length === 2) {
content = notesMatch[0] + '<aside class="notes" data-markdown>' + notesMatch[1].trim() + '</aside>';
content = notesMatch[0] + '<aside class="notes" data-markdown>' + notesMatch[1].trim() + '</aside>'
}
// prevent script end tags in the content from interfering
// with parsing
content = content.replace( /<\/script>/g, SCRIPT_END_PLACEHOLDER );
return '<script type="text/template">' + content + '</script>';
content = content.replace(/<\/script>/g, SCRIPT_END_PLACEHOLDER)
return '<script type="text/template">' + content + '</script>'
}
/**
@ -121,71 +113,65 @@
* on the passed in separator arguments.
*/
function slidify (markdown, options) {
options = getSlidifyOptions(options)
options = getSlidifyOptions( options );
var separatorRegex = new RegExp(options.separator + (options.verticalSeparator ? '|' + options.verticalSeparator : ''), 'mg')
var horizontalSeparatorRegex = new RegExp(options.separator)
var separatorRegex = new RegExp( options.separator + ( options.verticalSeparator ? '|' + options.verticalSeparator : '' ), 'mg' ),
horizontalSeparatorRegex = new RegExp( options.separator );
var matches,
lastIndex = 0,
isHorizontal,
wasHorizontal = true,
content,
sectionStack = [];
var matches
var lastIndex = 0
var isHorizontal
var wasHorizontal = true
var content
var sectionStack = []
// iterate until all blocks between separators are stacked up
while( matches = separatorRegex.exec( markdown ) ) {
notes = null;
while ((matches = separatorRegex.exec(markdown)) !== null) {
// determine direction (horizontal by default)
isHorizontal = horizontalSeparatorRegex.test( matches[0] );
isHorizontal = horizontalSeparatorRegex.test(matches[0])
if (!isHorizontal && wasHorizontal) {
// create vertical stack
sectionStack.push( [] );
sectionStack.push([])
}
// pluck slide content from markdown input
content = markdown.substring( lastIndex, matches.index );
content = markdown.substring(lastIndex, matches.index)
if (isHorizontal && wasHorizontal) {
// add to horizontal stack
sectionStack.push( content );
}
else {
sectionStack.push(content)
} else {
// add to vertical stack
sectionStack[sectionStack.length-1].push( content );
sectionStack[sectionStack.length - 1].push(content)
}
lastIndex = separatorRegex.lastIndex;
wasHorizontal = isHorizontal;
lastIndex = separatorRegex.lastIndex
wasHorizontal = isHorizontal
}
// add the remaining slide
( wasHorizontal ? sectionStack : sectionStack[sectionStack.length-1] ).push( markdown.substring( lastIndex ) );
(wasHorizontal ? sectionStack : sectionStack[sectionStack.length - 1]).push(markdown.substring(lastIndex))
var markdownSections = '';
var markdownSections = ''
// flatten the hierarchical stack, and insert <section data-markdown> tags
for (var i = 0, len = sectionStack.length; i < len; i++) {
// vertical
if (sectionStack[i] instanceof Array) {
markdownSections += '<section '+ options.attributes +'>';
markdownSections += '<section ' + options.attributes + '>'
sectionStack[i].forEach(function (child) {
markdownSections += '<section data-markdown>' + createMarkdownSlide( child, options ) + '</section>';
} );
markdownSections += '<section data-markdown>' + createMarkdownSlide(child, options) + '</section>'
})
markdownSections += '</section>';
}
else {
markdownSections += '<section '+ options.attributes +' data-markdown>' + createMarkdownSlide( sectionStack[i], options ) + '</section>';
markdownSections += '</section>'
} else {
markdownSections += '<section ' + options.attributes + ' data-markdown>' + createMarkdownSlide(sectionStack[i], options) + '</section>'
}
}
return markdownSections;
return markdownSections
}
/**
@ -194,76 +180,61 @@
* handles loading of external markdown.
*/
function processSlides () {
var sections = document.querySelectorAll( '[data-markdown]'),
section;
var sections = document.querySelectorAll('[data-markdown]')
var section
for (var i = 0, len = sections.length; i < len; i++) {
section = sections[i];
section = sections[i]
if (section.getAttribute('data-markdown').length) {
var xhr = new XMLHttpRequest()
var url = section.getAttribute('data-markdown')
var xhr = new XMLHttpRequest(),
url = section.getAttribute( 'data-markdown' );
datacharset = section.getAttribute( 'data-charset' );
var datacharset = section.getAttribute('data-charset')
// see https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute#Notes
if( datacharset != null && datacharset != '' ) {
xhr.overrideMimeType( 'text/html; charset=' + datacharset );
if (datacharset !== null && datacharset !== '') {
xhr.overrideMimeType('text/html; charset=' + datacharset)
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
// file protocol yields status code 0 (useful for local debug, mobile applications etc.)
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) {
section.outerHTML = slidify(xhr.responseText, {
separator: section.getAttribute('data-separator'),
verticalSeparator: section.getAttribute('data-separator-vertical'),
notesSeparator: section.getAttribute('data-separator-notes'),
attributes: getForwardedAttributes(section)
});
}
else {
})
} else {
section.outerHTML = '<section data-state="alert">' +
'ERROR: The attempt to fetch ' + url + ' failed with HTTP status ' + xhr.status + '.' +
'Check your browser\'s JavaScript console for more details.' +
'<p>Remember that you need to serve the presentation HTML from a HTTP server.</p>' +
'</section>';
'</section>'
}
}
}
};
xhr.open( 'GET', url, false );
xhr.open('GET', url, false)
try {
xhr.send();
xhr.send()
} catch (e) {
alert('Failed to get the Markdown file ' + url + '. Make sure that the presentation and the file are served by a HTTP server and the file can be found there. ' + e)
}
catch ( e ) {
alert( 'Failed to get the Markdown file ' + url + '. Make sure that the presentation and the file are served by a HTTP server and the file can be found there. ' + e );
}
}
else if( section.getAttribute( 'data-separator' ) || section.getAttribute( 'data-separator-vertical' ) || section.getAttribute( 'data-separator-notes' ) ) {
} else if (section.getAttribute('data-separator') || section.getAttribute('data-separator-vertical') || section.getAttribute('data-separator-notes')) {
section.outerHTML = slidify(getMarkdownFromSlide(section), {
separator: section.getAttribute('data-separator'),
verticalSeparator: section.getAttribute('data-separator-vertical'),
notesSeparator: section.getAttribute('data-separator-notes'),
attributes: getForwardedAttributes(section)
});
}
else {
section.innerHTML = createMarkdownSlide( getMarkdownFromSlide( section ) );
})
} else {
section.innerHTML = createMarkdownSlide(getMarkdownFromSlide(section))
}
}
}
/**
@ -276,24 +247,23 @@
* http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277
*/
function addAttributeInElement (node, elementTarget, separator) {
var mardownClassesInElementsRegex = new RegExp( separator, 'mg' );
var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' );
var nodeValue = node.nodeValue;
if( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) {
var classes = matches[1];
nodeValue = nodeValue.substring( 0, matches.index ) + nodeValue.substring( mardownClassesInElementsRegex.lastIndex );
node.nodeValue = nodeValue;
while( matchesClass = mardownClassRegex.exec( classes ) ) {
var name = matchesClass[1];
var value = matchesClass[2];
if (name.substr(0, 5) === 'data-' || whiteListAttr.indexOf(name) !== -1)
elementTarget.setAttribute( name, filterXSS.escapeAttrValue(value) );
var mardownClassesInElementsRegex = new RegExp(separator, 'mg')
var mardownClassRegex = new RegExp('([^"= ]+?)="([^"=]+?)"', 'mg')
var nodeValue = node.nodeValue
var matches
var matchesClass
if ((matches = mardownClassesInElementsRegex.exec(nodeValue))) {
var classes = matches[1]
nodeValue = nodeValue.substring(0, matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex)
node.nodeValue = nodeValue
while ((matchesClass = mardownClassRegex.exec(classes))) {
var name = matchesClass[1]
var value = matchesClass[2]
if (name.substr(0, 5) === 'data-' || window.whiteListAttr.indexOf(name) !== -1) { elementTarget.setAttribute(name, window.filterXSS.escapeAttrValue(value)) }
}
return true;
return true
}
return false;
return false
}
/**
@ -301,36 +271,35 @@
* or the element of an attribute node.
*/
function addAttributes (section, element, previousElement, separatorElementAttributes, separatorSectionAttributes) {
if ( element != null && element.childNodes != undefined && element.childNodes.length > 0 ) {
previousParentElement = element;
if (element != null && element.childNodes !== undefined && element.childNodes.length > 0) {
var previousParentElement = element
for (var i = 0; i < element.childNodes.length; i++) {
childElement = element.childNodes[i];
var childElement = element.childNodes[i]
if (i > 0) {
j = i - 1;
let j = i - 1
while (j >= 0) {
aPreviousChildElement = element.childNodes[j];
if ( typeof aPreviousChildElement.setAttribute == 'function' && aPreviousChildElement.tagName != "BR" ) {
previousParentElement = aPreviousChildElement;
break;
var aPreviousChildElement = element.childNodes[j]
if (typeof aPreviousChildElement.setAttribute === 'function' && aPreviousChildElement.tagName !== 'BR') {
previousParentElement = aPreviousChildElement
break
}
j = j - 1;
j = j - 1
}
}
parentSection = section;
if( childElement.nodeName == "section" ) {
parentSection = childElement ;
previousParentElement = childElement ;
var parentSection = section
if (childElement.nodeName === 'section') {
parentSection = childElement
previousParentElement = childElement
}
if ( typeof childElement.setAttribute == 'function' || childElement.nodeType == Node.COMMENT_NODE ) {
addAttributes( parentSection, childElement, previousParentElement, separatorElementAttributes, separatorSectionAttributes );
if (typeof childElement.setAttribute === 'function' || childElement.nodeType === Node.COMMENT_NODE) {
addAttributes(parentSection, childElement, previousParentElement, separatorElementAttributes, separatorSectionAttributes)
}
}
}
if ( element.nodeType == Node.COMMENT_NODE ) {
if ( addAttributeInElement( element, previousElement, separatorElementAttributes ) == false ) {
addAttributeInElement( element, section, separatorSectionAttributes );
if (element.nodeType === Node.COMMENT_NODE) {
if (addAttributeInElement(element, previousElement, separatorElementAttributes) === false) {
addAttributeInElement(element, section, separatorSectionAttributes)
}
}
}
@ -340,57 +309,47 @@
* DOM to HTML.
*/
function convertSlides () {
var sections = document.querySelectorAll( '[data-markdown]');
var sections = document.querySelectorAll('[data-markdown]')
for (var i = 0, len = sections.length; i < len; i++) {
var section = sections[i];
var section = sections[i]
// Only parse the same slide once
if (!section.getAttribute('data-markdown-parsed')) {
section.setAttribute('data-markdown-parsed', true)
var notes = section.querySelector( 'aside.notes' );
var markdown = getMarkdownFromSlide( section );
var notes = section.querySelector('aside.notes')
var markdown = getMarkdownFromSlide(section)
var rendered = md.render(markdown);
rendered = preventXSS(rendered);
var result = postProcess(rendered);
section.innerHTML = result[0].outerHTML;
var rendered = md.render(markdown)
rendered = preventXSS(rendered)
var result = window.postProcess(rendered)
section.innerHTML = result[0].outerHTML
addAttributes(section, section, null, section.getAttribute('data-element-attributes') ||
section.parentNode.getAttribute('data-element-attributes') ||
DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR,
section.getAttribute('data-attributes') ||
section.parentNode.getAttribute('data-attributes') ||
DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR);
DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR)
// If there were notes, we need to re-add them after
// having overwritten the section's HTML
if (notes) {
section.appendChild( notes );
section.appendChild(notes)
}
}
}
}
// API
return {
initialize: function () {
processSlides();
convertSlides();
processSlides()
convertSlides()
},
// TODO: Do these belong in the API?
processSlides: processSlides,
convertSlides: convertSlides,
slidify: slidify
};
}));
}
}))

View file

@ -1,42 +1,43 @@
require('../css/extra.css');
require('../css/site.css');
/* eslint-env browser, jquery */
/* global serverurl, Reveal */
import { md, updateLastChange, finishView } from './extra';
require('../css/extra.css')
require('../css/site.css')
import { preventXSS } from './render';
import { md, updateLastChange, finishView } from './extra'
const body = $(".slides").text();
const body = $('.slides').text()
createtime = lastchangeui.time.attr('data-createtime');
lastchangetime = lastchangeui.time.attr('data-updatetime');
updateLastChange();
const url = window.location.pathname;
$('.ui-edit').attr('href', `${url}/edit`);
window.createtime = window.lastchangeui.time.attr('data-createtime')
window.lastchangetime = window.lastchangeui.time.attr('data-updatetime')
updateLastChange()
const url = window.location.pathname
$('.ui-edit').attr('href', `${url}/edit`)
$(document).ready(() => {
// tooltip
$('[data-toggle="tooltip"]').tooltip();
});
$('[data-toggle="tooltip"]').tooltip()
})
function extend () {
const target = {};
const target = {}
for (const source of arguments) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
target[key] = source[key]
}
}
}
return target;
return target
}
// Optional libraries used to extend on reveal.js
const deps = [{
src: `${serverurl}/build/reveal.js/lib/js/classList.js`,
condition () {
return !document.body.classList;
return !document.body.classList
}
}, {
src: `${serverurl}/js/reveal-markdown.js`,
@ -44,19 +45,19 @@ const deps = [{
const slideOptions = {
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$'
};
const slides = RevealMarkdown.slidify(body, slideOptions);
$(".slides").html(slides);
RevealMarkdown.initialize();
$(".slides").show();
}
const slides = window.RevealMarkdown.slidify(body, slideOptions)
$('.slides').html(slides)
window.RevealMarkdown.initialize()
$('.slides').show()
}
}, {
src: `${serverurl}/build/reveal.js/plugin/notes/notes.js`,
async: true,
condition () {
return !!document.body.classList;
return !!document.body.classList
}
}];
}]
// default options to init reveal.js
const defaultOptions = {
@ -67,72 +68,72 @@ const defaultOptions = {
center: true,
transition: 'none',
dependencies: deps
};
}
// options from yaml meta
const meta = JSON.parse($("#meta").text());
var options = meta.slideOptions || {};
const meta = JSON.parse($('#meta').text())
var options = meta.slideOptions || {}
const view = $('.reveal');
const view = $('.reveal')
// text language
if (meta.lang && typeof meta.lang == "string") {
view.attr('lang', meta.lang);
if (meta.lang && typeof meta.lang === 'string') {
view.attr('lang', meta.lang)
} else {
view.removeAttr('lang');
view.removeAttr('lang')
}
// text direction
if (meta.dir && typeof meta.dir == "string" && meta.dir == "rtl") {
options.rtl = true;
if (meta.dir && typeof meta.dir === 'string' && meta.dir === 'rtl') {
options.rtl = true
} else {
options.rtl = false;
options.rtl = false
}
// breaks
if (typeof meta.breaks === 'boolean' && !meta.breaks) {
md.options.breaks = false;
md.options.breaks = false
} else {
md.options.breaks = true;
md.options.breaks = true
}
// options from URL query string
const queryOptions = Reveal.getQueryHash() || {};
const queryOptions = Reveal.getQueryHash() || {}
var options = extend(defaultOptions, options, queryOptions);
Reveal.initialize(options);
options = extend(defaultOptions, options, queryOptions)
Reveal.initialize(options)
window.viewAjaxCallback = () => {
Reveal.layout();
};
Reveal.layout()
}
function renderSlide (event) {
if (window.location.search.match(/print-pdf/gi)) {
const slides = $('.slides');
var title = document.title;
finishView(slides);
document.title = title;
Reveal.layout();
const slides = $('.slides')
let title = document.title
finishView(slides)
document.title = title
Reveal.layout()
} else {
const markdown = $(event.currentSlide);
const markdown = $(event.currentSlide)
if (!markdown.attr('data-rendered')) {
var title = document.title;
finishView(markdown);
markdown.attr('data-rendered', 'true');
document.title = title;
Reveal.layout();
let title = document.title
finishView(markdown)
markdown.attr('data-rendered', 'true')
document.title = title
Reveal.layout()
}
}
}
Reveal.addEventListener('ready', event => {
renderSlide(event);
const markdown = $(event.currentSlide);
renderSlide(event)
const markdown = $(event.currentSlide)
// force browser redraw
setTimeout(() => {
markdown.hide().show(0);
}, 0);
});
Reveal.addEventListener('slidechanged', renderSlide);
markdown.hide().show(0)
}, 0)
})
Reveal.addEventListener('slidechanged', renderSlide)
const isMacLike = navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) ? true : false;
const isMacLike = !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i)
if (!isMacLike) $('.container').addClass('hidescrollbar');
if (!isMacLike) $('.container').addClass('hidescrollbar')

View file

@ -1,365 +1,367 @@
/* eslint-env browser, jquery */
/* global _ */
// Inject line numbers for sync scroll.
import markdownitContainer from 'markdown-it-container';
import markdownitContainer from 'markdown-it-container'
import { md } from './extra';
import { md } from './extra'
function addPart (tokens, idx) {
if (tokens[idx].map && tokens[idx].level === 0) {
const startline = tokens[idx].map[0] + 1;
const endline = tokens[idx].map[1];
tokens[idx].attrJoin('class', 'part');
tokens[idx].attrJoin('data-startline', startline);
tokens[idx].attrJoin('data-endline', endline);
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
tokens[idx].attrJoin('class', 'part')
tokens[idx].attrJoin('data-startline', startline)
tokens[idx].attrJoin('data-endline', endline)
}
}
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
addPart(tokens, idx);
return self.renderToken(...arguments);
};
md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken(...arguments);
};
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken(...arguments);
};
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
if (tokens[idx].map) {
const startline = tokens[idx].map[0] + 1;
const endline = tokens[idx].map[1];
tokens[idx].attrJoin('data-startline', startline);
tokens[idx].attrJoin('data-endline', endline);
tokens[idx].attrJoin('class', 'raw')
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw')
if (tokens[idx].map) {
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
tokens[idx].attrJoin('data-startline', startline)
tokens[idx].attrJoin('data-endline', endline)
}
return self.renderToken(...arguments)
}
return self.renderToken(...arguments);
};
md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken(...arguments);
};
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken(...arguments);
};
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken(...arguments);
};
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
addPart(tokens, idx);
return self.renderToken(...arguments);
};
tokens[idx].attrJoin('class', 'raw')
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
const token = tokens[idx];
const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
let langName = '';
let highlighted;
const token = tokens[idx]
const info = token.info ? md.utils.unescapeAll(token.info).trim() : ''
let langName = ''
let highlighted
if (info) {
langName = info.split(/\s+/g)[0];
if (/\!$/.test(info)) token.attrJoin('class', 'wrap');
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$|\!$|\=\!/, ''));
token.attrJoin('class', 'hljs');
token.attrJoin('class', 'raw');
langName = info.split(/\s+/g)[0]
if (/!$/.test(info)) token.attrJoin('class', 'wrap')
token.attrJoin('class', options.langPrefix + langName.replace(/=$|=\d+$|=\+$|!$|=!/, ''))
token.attrJoin('class', 'hljs')
token.attrJoin('class', 'raw')
}
if (options.highlight) {
highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content);
highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content)
} else {
highlighted = md.utils.escapeHtml(token.content);
highlighted = md.utils.escapeHtml(token.content)
}
if (highlighted.indexOf('<pre') === 0) {
return `${highlighted}\n`;
return `${highlighted}\n`
}
if (tokens[idx].map && tokens[idx].level === 0) {
const startline = tokens[idx].map[0] + 1;
const endline = tokens[idx].map[1];
return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`;
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`
}
return `<pre><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`;
};
return `<pre><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`
}
md.renderer.rules.code_block = (tokens, idx, options, env, self) => {
if (tokens[idx].map && tokens[idx].level === 0) {
const startline = tokens[idx].map[0] + 1;
const endline = tokens[idx].map[1];
return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`;
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`
}
return `<pre><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`
}
return `<pre><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`;
};
function renderContainer (tokens, idx, options, env, self) {
tokens[idx].attrJoin('role', 'alert');
tokens[idx].attrJoin('class', 'alert');
tokens[idx].attrJoin('class', `alert-${tokens[idx].info.trim()}`);
addPart(tokens, idx);
return self.renderToken(...arguments);
tokens[idx].attrJoin('role', 'alert')
tokens[idx].attrJoin('class', 'alert')
tokens[idx].attrJoin('class', `alert-${tokens[idx].info.trim()}`)
addPart(tokens, idx)
return self.renderToken(...arguments)
}
md.use(markdownitContainer, 'success', { render: renderContainer });
md.use(markdownitContainer, 'info', { render: renderContainer });
md.use(markdownitContainer, 'warning', { render: renderContainer });
md.use(markdownitContainer, 'danger', { render: renderContainer });
md.use(markdownitContainer, 'success', { render: renderContainer })
md.use(markdownitContainer, 'info', { render: renderContainer })
md.use(markdownitContainer, 'warning', { render: renderContainer })
md.use(markdownitContainer, 'danger', { render: renderContainer })
// FIXME: expose syncscroll to window
window.syncscroll = true;
window.syncscroll = true
window.preventSyncScrollToEdit = false;
window.preventSyncScrollToView = false;
window.preventSyncScrollToEdit = false
window.preventSyncScrollToView = false
const editScrollThrottle = 5;
const viewScrollThrottle = 5;
const buildMapThrottle = 100;
const editScrollThrottle = 5
const viewScrollThrottle = 5
const buildMapThrottle = 100
let viewScrolling = false;
let editScrolling = false;
let viewScrolling = false
let editScrolling = false
let editArea = null;
let viewArea = null;
let markdownArea = null;
let editArea = null
let viewArea = null
let markdownArea = null
export function setupSyncAreas (edit, view, markdown) {
editArea = edit;
viewArea = view;
markdownArea = markdown;
editArea.on('scroll', _.throttle(syncScrollToView, editScrollThrottle));
viewArea.on('scroll', _.throttle(syncScrollToEdit, viewScrollThrottle));
editArea = edit
viewArea = view
markdownArea = markdown
editArea.on('scroll', _.throttle(syncScrollToView, editScrollThrottle))
viewArea.on('scroll', _.throttle(syncScrollToEdit, viewScrollThrottle))
}
let scrollMap, lineHeightMap, viewTop, viewBottom;
let scrollMap, lineHeightMap, viewTop, viewBottom
export function clearMap () {
scrollMap = null;
lineHeightMap = null;
viewTop = null;
viewBottom = null;
scrollMap = null
lineHeightMap = null
viewTop = null
viewBottom = null
}
window.viewAjaxCallback = clearMap;
window.viewAjaxCallback = clearMap
const buildMap = _.throttle(buildMapInner, buildMapThrottle);
const buildMap = _.throttle(buildMapInner, buildMapThrottle)
// Build offsets for each line (lines can be wrapped)
// That's a bit dirty to process each line everytime, but ok for demo.
// Optimizations are required only for big texts.
function buildMapInner (callback) {
if (!viewArea || !markdownArea) return;
let i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount, acc, _scrollMap;
if (!viewArea || !markdownArea) return
let i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount, acc, _scrollMap
offset = viewArea.scrollTop() - viewArea.offset().top;
_scrollMap = [];
nonEmptyList = [];
_lineHeightMap = [];
viewTop = 0;
viewBottom = viewArea[0].scrollHeight - viewArea.height();
offset = viewArea.scrollTop() - viewArea.offset().top
_scrollMap = []
nonEmptyList = []
_lineHeightMap = []
viewTop = 0
viewBottom = viewArea[0].scrollHeight - viewArea.height()
acc = 0;
const lines = editor.getValue().split('\n');
const lineHeight = editor.defaultTextHeight();
acc = 0
const lines = window.editor.getValue().split('\n')
const lineHeight = window.editor.defaultTextHeight()
for (i = 0; i < lines.length; i++) {
const str = lines[i];
const str = lines[i]
_lineHeightMap.push(acc);
_lineHeightMap.push(acc)
if (str.length === 0) {
acc++;
continue;
acc++
continue
}
const h = editor.heightAtLine(i + 1) - editor.heightAtLine(i);
acc += Math.round(h / lineHeight);
const h = window.editor.heightAtLine(i + 1) - window.editor.heightAtLine(i)
acc += Math.round(h / lineHeight)
}
_lineHeightMap.push(acc);
linesCount = acc;
_lineHeightMap.push(acc)
linesCount = acc
for (i = 0; i < linesCount; i++) {
_scrollMap.push(-1);
_scrollMap.push(-1)
}
nonEmptyList.push(0);
nonEmptyList.push(0)
// make the first line go top
_scrollMap[0] = viewTop;
_scrollMap[0] = viewTop
const parts = markdownArea.find('.part').toArray();
const parts = markdownArea.find('.part').toArray()
for (i = 0; i < parts.length; i++) {
const $el = $(parts[i]);
let t = $el.attr('data-startline') - 1;
const $el = $(parts[i])
let t = $el.attr('data-startline') - 1
if (t === '') {
return;
return
}
t = _lineHeightMap[t];
t = _lineHeightMap[t]
if (t !== 0 && t !== nonEmptyList[nonEmptyList.length - 1]) {
nonEmptyList.push(t);
nonEmptyList.push(t)
}
_scrollMap[t] = Math.round($el.offset().top + offset - 10);
_scrollMap[t] = Math.round($el.offset().top + offset - 10)
}
nonEmptyList.push(linesCount);
_scrollMap[linesCount] = viewArea[0].scrollHeight;
nonEmptyList.push(linesCount)
_scrollMap[linesCount] = viewArea[0].scrollHeight
pos = 0;
pos = 0
for (i = 1; i < linesCount; i++) {
if (_scrollMap[i] !== -1) {
pos++;
continue;
pos++
continue
}
a = nonEmptyList[pos];
b = nonEmptyList[pos + 1];
_scrollMap[i] = Math.round((_scrollMap[b] * (i - a) + _scrollMap[a] * (b - i)) / (b - a));
a = nonEmptyList[pos]
b = nonEmptyList[pos + 1]
_scrollMap[i] = Math.round((_scrollMap[b] * (i - a) + _scrollMap[a] * (b - i)) / (b - a))
}
_scrollMap[0] = 0;
_scrollMap[0] = 0
scrollMap = _scrollMap;
lineHeightMap = _lineHeightMap;
scrollMap = _scrollMap
lineHeightMap = _lineHeightMap
if (loaded && callback) callback();
if (window.loaded && callback) callback()
}
// sync view scroll progress to edit
let viewScrollingTimer = null;
let viewScrollingTimer = null
export function syncScrollToEdit (event, preventAnimate) {
if (currentMode != modeType.both || !syncscroll || !editArea) return;
if (preventSyncScrollToEdit) {
if (typeof preventSyncScrollToEdit === 'number') {
preventSyncScrollToEdit--;
if (window.currentMode !== window.modeType.both || !window.syncscroll || !editArea) return
if (window.preventSyncScrollToEdit) {
if (typeof window.preventSyncScrollToEdit === 'number') {
window.preventSyncScrollToEdit--
} else {
preventSyncScrollToEdit = false;
window.preventSyncScrollToEdit = false
}
return;
return
}
if (!scrollMap || !lineHeightMap) {
buildMap(() => {
syncScrollToEdit(event, preventAnimate);
});
return;
syncScrollToEdit(event, preventAnimate)
})
return
}
if (editScrolling) return;
if (editScrolling) return
const scrollTop = viewArea[0].scrollTop;
let lineIndex = 0;
for (var i = 0, l = scrollMap.length; i < l; i++) {
const scrollTop = viewArea[0].scrollTop
let lineIndex = 0
for (let i = 0, l = scrollMap.length; i < l; i++) {
if (scrollMap[i] > scrollTop) {
break;
break
} else {
lineIndex = i;
lineIndex = i
}
}
let lineNo = 0;
let lineDiff = 0;
for (var i = 0, l = lineHeightMap.length; i < l; i++) {
let lineNo = 0
let lineDiff = 0
for (let i = 0, l = lineHeightMap.length; i < l; i++) {
if (lineHeightMap[i] > lineIndex) {
break;
break
} else {
lineNo = lineHeightMap[i];
lineDiff = lineHeightMap[i + 1] - lineNo;
lineNo = lineHeightMap[i]
lineDiff = lineHeightMap[i + 1] - lineNo
}
}
let posTo = 0;
let topDiffPercent = 0;
let posToNextDiff = 0;
const scrollInfo = editor.getScrollInfo();
const textHeight = editor.defaultTextHeight();
const preLastLineHeight = scrollInfo.height - scrollInfo.clientHeight - textHeight;
const preLastLineNo = Math.round(preLastLineHeight / textHeight);
const preLastLinePos = scrollMap[preLastLineNo];
let posTo = 0
let topDiffPercent = 0
let posToNextDiff = 0
const scrollInfo = window.editor.getScrollInfo()
const textHeight = window.editor.defaultTextHeight()
const preLastLineHeight = scrollInfo.height - scrollInfo.clientHeight - textHeight
const preLastLineNo = Math.round(preLastLineHeight / textHeight)
const preLastLinePos = scrollMap[preLastLineNo]
if (scrollInfo.height > scrollInfo.clientHeight && scrollTop >= preLastLinePos) {
posTo = preLastLineHeight;
topDiffPercent = (scrollTop - preLastLinePos) / (viewBottom - preLastLinePos);
posToNextDiff = textHeight * topDiffPercent;
posTo += Math.ceil(posToNextDiff);
posTo = preLastLineHeight
topDiffPercent = (scrollTop - preLastLinePos) / (viewBottom - preLastLinePos)
posToNextDiff = textHeight * topDiffPercent
posTo += Math.ceil(posToNextDiff)
} else {
posTo = lineNo * textHeight;
topDiffPercent = (scrollTop - scrollMap[lineNo]) / (scrollMap[lineNo + lineDiff] - scrollMap[lineNo]);
posToNextDiff = textHeight * lineDiff * topDiffPercent;
posTo += Math.ceil(posToNextDiff);
posTo = lineNo * textHeight
topDiffPercent = (scrollTop - scrollMap[lineNo]) / (scrollMap[lineNo + lineDiff] - scrollMap[lineNo])
posToNextDiff = textHeight * lineDiff * topDiffPercent
posTo += Math.ceil(posToNextDiff)
}
if (preventAnimate) {
editArea.scrollTop(posTo);
editArea.scrollTop(posTo)
} else {
const posDiff = Math.abs(scrollInfo.top - posTo);
var duration = posDiff / 50;
duration = duration >= 100 ? duration : 100;
const posDiff = Math.abs(scrollInfo.top - posTo)
var duration = posDiff / 50
duration = duration >= 100 ? duration : 100
editArea.stop(true, true).animate({
scrollTop: posTo
}, duration, "linear");
}, duration, 'linear')
}
viewScrolling = true;
clearTimeout(viewScrollingTimer);
viewScrollingTimer = setTimeout(viewScrollingTimeoutInner, duration * 1.5);
viewScrolling = true
clearTimeout(viewScrollingTimer)
viewScrollingTimer = setTimeout(viewScrollingTimeoutInner, duration * 1.5)
}
function viewScrollingTimeoutInner () {
viewScrolling = false;
viewScrolling = false
}
// sync edit scroll progress to view
let editScrollingTimer = null;
let editScrollingTimer = null
export function syncScrollToView (event, preventAnimate) {
if (currentMode != modeType.both || !syncscroll || !viewArea) return;
if (preventSyncScrollToView) {
if (window.currentMode !== window.modeType.both || !window.syncscroll || !viewArea) return
if (window.preventSyncScrollToView) {
if (typeof preventSyncScrollToView === 'number') {
preventSyncScrollToView--;
window.preventSyncScrollToView--
} else {
preventSyncScrollToView = false;
window.preventSyncScrollToView = false
}
return;
return
}
if (!scrollMap || !lineHeightMap) {
buildMap(() => {
syncScrollToView(event, preventAnimate);
});
return;
syncScrollToView(event, preventAnimate)
})
return
}
if (viewScrolling) return;
if (viewScrolling) return
let lineNo, posTo;
let topDiffPercent, posToNextDiff;
const scrollInfo = editor.getScrollInfo();
const textHeight = editor.defaultTextHeight();
lineNo = Math.floor(scrollInfo.top / textHeight);
let lineNo, posTo
let topDiffPercent, posToNextDiff
const scrollInfo = window.editor.getScrollInfo()
const textHeight = window.editor.defaultTextHeight()
lineNo = Math.floor(scrollInfo.top / textHeight)
// if reach the last line, will start lerp to the bottom
const diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight);
const diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight)
if (scrollInfo.height > scrollInfo.clientHeight && diffToBottom > 0) {
topDiffPercent = diffToBottom / textHeight;
posTo = scrollMap[lineNo + 1];
posToNextDiff = (viewBottom - posTo) * topDiffPercent;
posTo += Math.floor(posToNextDiff);
topDiffPercent = diffToBottom / textHeight
posTo = scrollMap[lineNo + 1]
posToNextDiff = (viewBottom - posTo) * topDiffPercent
posTo += Math.floor(posToNextDiff)
} else {
topDiffPercent = (scrollInfo.top % textHeight) / textHeight;
posTo = scrollMap[lineNo];
posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent;
posTo += Math.floor(posToNextDiff);
topDiffPercent = (scrollInfo.top % textHeight) / textHeight
posTo = scrollMap[lineNo]
posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent
posTo += Math.floor(posToNextDiff)
}
if (preventAnimate) {
viewArea.scrollTop(posTo);
viewArea.scrollTop(posTo)
} else {
const posDiff = Math.abs(viewArea.scrollTop() - posTo);
var duration = posDiff / 50;
duration = duration >= 100 ? duration : 100;
const posDiff = Math.abs(viewArea.scrollTop() - posTo)
var duration = posDiff / 50
duration = duration >= 100 ? duration : 100
viewArea.stop(true, true).animate({
scrollTop: posTo
}, duration, "linear");
}, duration, 'linear')
}
editScrolling = true;
clearTimeout(editScrollingTimer);
editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.5);
editScrolling = true
clearTimeout(editScrollingTimer)
editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.5)
}
function editScrollingTimeoutInner () {
editScrolling = false;
editScrolling = false
}

View file

@ -1,3 +1,4 @@
/* eslint-env browser, jquery */
/**
* md-toc.js v1.0.2
* https://github.com/yijian166/md-toc.js
@ -5,125 +6,118 @@
(function (window) {
function Toc (id, options) {
this.el = document.getElementById(id);
if (!this.el) return;
this.options = options || {};
this.tocLevel = parseInt(options.level) || 0;
this.tocClass = options['class'] || 'toc';
this.ulClass = options['ulClass'];
this.tocTop = parseInt(options.top) || 0;
this.elChilds = this.el.children;
this.process = options['process'];
if (!this.elChilds.length) return;
this._init();
this.el = document.getElementById(id)
if (!this.el) return
this.options = options || {}
this.tocLevel = parseInt(options.level) || 0
this.tocClass = options['class'] || 'toc'
this.ulClass = options['ulClass']
this.tocTop = parseInt(options.top) || 0
this.elChilds = this.el.children
this.process = options['process']
if (!this.elChilds.length) return
this._init()
}
Toc.prototype._init = function () {
this._collectTitleElements();
this._createTocContent();
this._showToc();
};
this._collectTitleElements()
this._createTocContent()
this._showToc()
}
Toc.prototype._collectTitleElements = function () {
this._elTitlesNames = [],
this.elTitleElements = [];
this._elTitlesNames = []
this.elTitleElements = []
for (var i = 1; i < 7; i++) {
if (this.el.getElementsByTagName('h' + i).length) {
this._elTitlesNames.push('h' + i);
this._elTitlesNames.push('h' + i)
}
}
this._elTitlesNames.length = this._elTitlesNames.length > this.tocLevel ? this.tocLevel : this._elTitlesNames.length;
this._elTitlesNames.length = this._elTitlesNames.length > this.tocLevel ? this.tocLevel : this._elTitlesNames.length
for (var j = 0; j < this.elChilds.length; j++) {
this._elChildName = this.elChilds[j].tagName.toLowerCase();
this._elChildName = this.elChilds[j].tagName.toLowerCase()
if (this._elTitlesNames.toString().match(this._elChildName)) {
this.elTitleElements.push(this.elChilds[j]);
this.elTitleElements.push(this.elChilds[j])
}
}
}
};
Toc.prototype._createTocContent = function () {
this._elTitleElementsLen = this.elTitleElements.length;
if (!this._elTitleElementsLen) return;
this.tocContent = '';
this._tempLists = [];
this._elTitleElementsLen = this.elTitleElements.length
if (!this._elTitleElementsLen) return
this.tocContent = ''
this._tempLists = []
var url = location.origin + location.pathname;
for (var i = 0; i < this._elTitleElementsLen; i++) {
var j = i + 1;
this._elTitleElement = this.elTitleElements[i];
this._elTitleElementName = this._elTitleElement.tagName;
this._elTitleElementText = (typeof this.process === 'function' ? this.process(this._elTitleElement) : this._elTitleElement.innerHTML).replace(/<(?:.|\n)*?>/gm, '');
var id = this._elTitleElement.getAttribute('id');
var j = i + 1
this._elTitleElement = this.elTitleElements[i]
this._elTitleElementName = this._elTitleElement.tagName
this._elTitleElementText = (typeof this.process === 'function' ? this.process(this._elTitleElement) : this._elTitleElement.innerHTML).replace(/<(?:.|\n)*?>/gm, '')
var id = this._elTitleElement.getAttribute('id')
if (!id) {
this._elTitleElement.setAttribute('id', 'tip' + i);
id = '#tip' + i;
this._elTitleElement.setAttribute('id', 'tip' + i)
id = '#tip' + i
} else {
id = '#' + id;
id = '#' + id
}
this.tocContent += '<li><a href="' + id + '">' + this._elTitleElementText + '</a>';
this.tocContent += '<li><a href="' + id + '">' + this._elTitleElementText + '</a>'
if (j != this._elTitleElementsLen) {
this._elNextTitleElementName = this.elTitleElements[j].tagName;
if (this._elTitleElementName != this._elNextTitleElementName) {
var checkColse = false,
y = 1;
if (j !== this._elTitleElementsLen) {
this._elNextTitleElementName = this.elTitleElements[j].tagName
if (this._elTitleElementName !== this._elNextTitleElementName) {
var checkColse = false
var y = 1
for (var t = this._tempLists.length - 1; t >= 0; t--) {
if (this._tempLists[t].tagName == this._elNextTitleElementName) {
checkColse = true;
break;
if (this._tempLists[t].tagName === this._elNextTitleElementName) {
checkColse = true
break
}
y++;
y++
}
if (checkColse) {
this.tocContent += new Array(y + 1).join('</li></ul>');
this._tempLists.length = this._tempLists.length - y;
this.tocContent += new Array(y + 1).join('</li></ul>')
this._tempLists.length = this._tempLists.length - y
} else {
this._tempLists.push(this._elTitleElement);
if (this.ulClass)
this.tocContent += '<ul class="' + this.ulClass + '">';
else
this.tocContent += '<ul>';
this._tempLists.push(this._elTitleElement)
if (this.ulClass) { this.tocContent += '<ul class="' + this.ulClass + '">' } else { this.tocContent += '<ul>' }
}
} else {
this.tocContent += '</li>';
this.tocContent += '</li>'
}
} else {
if (this._tempLists.length) {
this.tocContent += new Array(this._tempLists.length + 1).join('</li></ul>');
this.tocContent += new Array(this._tempLists.length + 1).join('</li></ul>')
} else {
this.tocContent += '</li>';
this.tocContent += '</li>'
}
}
}
if (this.ulClass)
this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>';
else
this.tocContent = '<ul>' + this.tocContent + '</ul>';
};
if (this.ulClass) { this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>' } else { this.tocContent = '<ul>' + this.tocContent + '</ul>' }
}
Toc.prototype._showToc = function () {
this.toc = document.createElement('div');
this.toc.innerHTML = this.tocContent;
this.toc.setAttribute('class', this.tocClass);
this.toc = document.createElement('div')
this.toc.innerHTML = this.tocContent
this.toc.setAttribute('class', this.tocClass)
if (!this.options.targetId) {
this.el.appendChild(this.toc);
this.el.appendChild(this.toc)
} else {
document.getElementById(this.options.targetId).appendChild(this.toc);
document.getElementById(this.options.targetId).appendChild(this.toc)
}
var self = this;
var self = this
if (this.tocTop > -1) {
window.onscroll = function () {
var t = document.documentElement.scrollTop || document.body.scrollTop;
var t = document.documentElement.scrollTop || document.body.scrollTop
if (t < self.tocTop) {
self.toc.setAttribute('style', 'position:absolute;top:' + self.tocTop + 'px;');
self.toc.setAttribute('style', 'position:absolute;top:' + self.tocTop + 'px;')
} else {
self.toc.setAttribute('style', 'position:fixed;top:10px;');
self.toc.setAttribute('style', 'position:fixed;top:10px;')
}
}
}
};
window.Toc = Toc;
})(window);
}
window.Toc = Toc
})(window)

View file

@ -1,10 +1,10 @@
var baseConfig = require('./webpackBaseConfig');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require('path');
var baseConfig = require('./webpackBaseConfig')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var path = require('path')
module.exports = [Object.assign({}, baseConfig, {
plugins: baseConfig.plugins.concat([
new ExtractTextPlugin("[name].css")
new ExtractTextPlugin('[name].css')
])
}), {
entry: {
@ -28,6 +28,6 @@ module.exports = [Object.assign({}, baseConfig, {
filename: '[name].js'
},
plugins: [
new ExtractTextPlugin("html.min.css")
new ExtractTextPlugin('html.min.css')
]
}];
}]

View file

@ -1,9 +1,9 @@
var baseConfig = require('./webpackBaseConfig');
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
var ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
var baseConfig = require('./webpackBaseConfig')
var webpack = require('webpack')
var path = require('path')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
var ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')
module.exports = [Object.assign({}, baseConfig, {
plugins: baseConfig.plugins.concat([
@ -21,7 +21,7 @@ module.exports = [Object.assign({}, baseConfig, {
sourceMap: false
}
}),
new ExtractTextPlugin("[name].[hash].css")
new ExtractTextPlugin('[name].[hash].css')
]),
output: {
@ -57,7 +57,7 @@ module.exports = [Object.assign({}, baseConfig, {
'NODE_ENV': JSON.stringify('production')
}
}),
new ExtractTextPlugin("html.min.css"),
new ExtractTextPlugin('html.min.css'),
new OptimizeCssAssetsPlugin()
]
}];
}]

View file

@ -1,24 +1,24 @@
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var webpack = require('webpack')
var path = require('path')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
plugins: [
new webpack.ProvidePlugin({
Visibility: "visibilityjs",
Cookies: "js-cookie",
key: "keymaster",
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"moment": "moment",
"Handlebars": "handlebars"
Visibility: 'visibilityjs',
Cookies: 'js-cookie',
key: 'keymaster',
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'moment': 'moment',
'Handlebars': 'handlebars'
}),
new webpack.optimize.OccurrenceOrderPlugin(true),
new webpack.optimize.CommonsChunkPlugin({
names: ["cover", "index", "pretty", "slide", "vendor"],
names: ['cover', 'index', 'pretty', 'slide', 'vendor'],
children: true,
async: true,
filename: '[name].js',
@ -159,57 +159,56 @@ module.exports = {
}
])
],
entry: {
font: path.join(__dirname, 'public/css/google-font.css'),
"font-pack": path.join(__dirname, 'public/css/font.css'),
'font-pack': path.join(__dirname, 'public/css/font.css'),
common: [
"expose?jQuery!expose?$!jquery",
"velocity-animate",
"imports?$=jquery!jquery-mousewheel",
"bootstrap"
'expose?jQuery!expose?$!jquery',
'velocity-animate',
'imports?$=jquery!jquery-mousewheel',
'bootstrap'
],
cover: [
"babel-polyfill",
'babel-polyfill',
path.join(__dirname, 'public/js/cover.js')
],
"cover-styles-pack": [
'cover-styles-pack': [
path.join(__dirname, 'node_modules/bootstrap/dist/css/bootstrap.min.css'),
path.join(__dirname, 'node_modules/font-awesome/css/font-awesome.min.css'),
path.join(__dirname, 'public/css/bootstrap-social.css'),
path.join(__dirname, 'node_modules/select2/select2.css'),
path.join(__dirname, 'node_modules/select2/select2-bootstrap.css'),
path.join(__dirname, 'node_modules/select2/select2-bootstrap.css')
],
"cover-pack": [
"babel-polyfill",
"bootstrap-validator",
"script!listPagnation",
"expose?select2!select2",
"expose?moment!moment",
"script!js-url",
'cover-pack': [
'babel-polyfill',
'bootstrap-validator',
'script!listPagnation',
'expose?select2!select2',
'expose?moment!moment',
'script!js-url',
path.join(__dirname, 'public/js/cover.js')
],
index: [
"babel-polyfill",
"script!jquery-ui-resizable",
"script!js-url",
"expose?filterXSS!xss",
"script!Idle.Js",
"expose?LZString!lz-string",
"script!codemirror",
"script!inlineAttachment",
"script!jqueryTextcomplete",
"script!codemirrorSpellChecker",
"script!codemirrorInlineAttachment",
"script!ot",
"flowchart.js",
"js-sequence-diagrams",
"expose?RevealMarkdown!reveal-markdown",
'babel-polyfill',
'script!jquery-ui-resizable',
'script!js-url',
'expose?filterXSS!xss',
'script!Idle.Js',
'expose?LZString!lz-string',
'script!codemirror',
'script!inlineAttachment',
'script!jqueryTextcomplete',
'script!codemirrorSpellChecker',
'script!codemirrorInlineAttachment',
'script!ot',
'flowchart.js',
'js-sequence-diagrams',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/google-drive-upload.js'),
path.join(__dirname, 'public/js/google-drive-picker.js'),
path.join(__dirname, 'public/js/index.js')
],
"index-styles": [
'index-styles': [
path.join(__dirname, 'public/vendor/jquery-ui/jquery-ui.min.css'),
path.join(__dirname, 'public/vendor/codemirror-spell-checker/spell-checker.min.css'),
path.join(__dirname, 'node_modules/codemirror/lib/codemirror.css'),
@ -228,120 +227,120 @@ module.exports = {
path.join(__dirname, 'public/css/markdown.css'),
path.join(__dirname, 'public/css/slide-preview.css')
],
"index-styles-pack": [
'index-styles-pack': [
path.join(__dirname, 'node_modules/bootstrap/dist/css/bootstrap.min.css'),
path.join(__dirname, 'node_modules/font-awesome/css/font-awesome.min.css'),
path.join(__dirname, 'public/css/bootstrap-social.css'),
path.join(__dirname, 'node_modules/ionicons/css/ionicons.min.css'),
path.join(__dirname, 'node_modules/octicons/octicons/octicons.css')
],
"index-pack": [
"babel-polyfill",
"expose?Spinner!spin.js",
"script!jquery-ui-resizable",
"bootstrap-validator",
"expose?jsyaml!js-yaml",
"script!mermaid",
"expose?moment!moment",
"script!js-url",
"script!handlebars",
"expose?hljs!highlight.js",
"expose?emojify!emojify.js",
"expose?filterXSS!xss",
"script!Idle.Js",
"script!gist-embed",
"expose?LZString!lz-string",
"script!codemirror",
"script!inlineAttachment",
"script!jqueryTextcomplete",
"script!codemirrorSpellChecker",
"script!codemirrorInlineAttachment",
"script!ot",
"flowchart.js",
"js-sequence-diagrams",
"expose?Viz!viz.js",
"expose?io!socket.io-client",
"expose?RevealMarkdown!reveal-markdown",
'index-pack': [
'babel-polyfill',
'expose?Spinner!spin.js',
'script!jquery-ui-resizable',
'bootstrap-validator',
'expose?jsyaml!js-yaml',
'script!mermaid',
'expose?moment!moment',
'script!js-url',
'script!handlebars',
'expose?hljs!highlight.js',
'expose?emojify!emojify.js',
'expose?filterXSS!xss',
'script!Idle.Js',
'script!gist-embed',
'expose?LZString!lz-string',
'script!codemirror',
'script!inlineAttachment',
'script!jqueryTextcomplete',
'script!codemirrorSpellChecker',
'script!codemirrorInlineAttachment',
'script!ot',
'flowchart.js',
'js-sequence-diagrams',
'expose?Viz!viz.js',
'expose?io!socket.io-client',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/google-drive-upload.js'),
path.join(__dirname, 'public/js/google-drive-picker.js'),
path.join(__dirname, 'public/js/index.js')
],
pretty: [
"babel-polyfill",
"expose?filterXSS!xss",
"flowchart.js",
"js-sequence-diagrams",
"expose?RevealMarkdown!reveal-markdown",
'babel-polyfill',
'expose?filterXSS!xss',
'flowchart.js',
'js-sequence-diagrams',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/pretty.js')
],
"pretty-styles": [
'pretty-styles': [
path.join(__dirname, 'public/css/github-extract.css'),
path.join(__dirname, 'public/css/mermaid.css'),
path.join(__dirname, 'public/css/markdown.css'),
path.join(__dirname, 'public/css/slide-preview.css')
],
"pretty-styles-pack": [
'pretty-styles-pack': [
path.join(__dirname, 'node_modules/bootstrap/dist/css/bootstrap.min.css'),
path.join(__dirname, 'node_modules/font-awesome/css/font-awesome.min.css'),
path.join(__dirname, 'node_modules/ionicons/css/ionicons.min.css'),
path.join(__dirname, 'node_modules/octicons/octicons/octicons.css')
],
"pretty-pack": [
"babel-polyfill",
"expose?jsyaml!js-yaml",
"script!mermaid",
"expose?moment!moment",
"script!handlebars",
"expose?hljs!highlight.js",
"expose?emojify!emojify.js",
"expose?filterXSS!xss",
"script!gist-embed",
"flowchart.js",
"js-sequence-diagrams",
"expose?Viz!viz.js",
"expose?RevealMarkdown!reveal-markdown",
'pretty-pack': [
'babel-polyfill',
'expose?jsyaml!js-yaml',
'script!mermaid',
'expose?moment!moment',
'script!handlebars',
'expose?hljs!highlight.js',
'expose?emojify!emojify.js',
'expose?filterXSS!xss',
'script!gist-embed',
'flowchart.js',
'js-sequence-diagrams',
'expose?Viz!viz.js',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/pretty.js')
],
slide: [
"babel-polyfill",
"bootstrap-tooltip",
"expose?filterXSS!xss",
"flowchart.js",
"js-sequence-diagrams",
"expose?RevealMarkdown!reveal-markdown",
'babel-polyfill',
'bootstrap-tooltip',
'expose?filterXSS!xss',
'flowchart.js',
'js-sequence-diagrams',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/slide.js')
],
"slide-styles": [
'slide-styles': [
path.join(__dirname, 'public/vendor/bootstrap/tooltip.min.css'),
path.join(__dirname, 'public/css/github-extract.css'),
path.join(__dirname, 'public/css/mermaid.css'),
path.join(__dirname, 'public/css/markdown.css')
],
"slide-styles-pack": [
'slide-styles-pack': [
path.join(__dirname, 'node_modules/font-awesome/css/font-awesome.min.css'),
path.join(__dirname, 'node_modules/ionicons/css/ionicons.min.css'),
path.join(__dirname, 'node_modules/octicons/octicons/octicons.css')
],
"slide-pack": [
"babel-polyfill",
"expose?jQuery!expose?$!jquery",
"velocity-animate",
"imports?$=jquery!jquery-mousewheel",
"bootstrap-tooltip",
"expose?jsyaml!js-yaml",
"script!mermaid",
"expose?moment!moment",
"script!handlebars",
"expose?hljs!highlight.js",
"expose?emojify!emojify.js",
"expose?filterXSS!xss",
"script!gist-embed",
"flowchart.js",
"js-sequence-diagrams",
"expose?Viz!viz.js",
"headjs",
"expose?Reveal!reveal.js",
"expose?RevealMarkdown!reveal-markdown",
'slide-pack': [
'babel-polyfill',
'expose?jQuery!expose?$!jquery',
'velocity-animate',
'imports?$=jquery!jquery-mousewheel',
'bootstrap-tooltip',
'expose?jsyaml!js-yaml',
'script!mermaid',
'expose?moment!moment',
'script!handlebars',
'expose?hljs!highlight.js',
'expose?emojify!emojify.js',
'expose?filterXSS!xss',
'script!gist-embed',
'flowchart.js',
'js-sequence-diagrams',
'expose?Viz!viz.js',
'headjs',
'expose?Reveal!reveal.js',
'expose?RevealMarkdown!reveal-markdown',
path.join(__dirname, 'public/js/slide.js')
]
},
@ -358,7 +357,7 @@ module.exports = {
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'node_modules')
],
extensions: ["", ".js"],
extensions: ['', '.js'],
alias: {
codemirror: path.join(__dirname, 'node_modules/codemirror/codemirror.min.js'),
inlineAttachment: path.join(__dirname, 'public/vendor/inlineAttachment/inline-attachment.js'),
@ -369,23 +368,23 @@ module.exports = {
listPagnation: path.join(__dirname, 'node_modules/list.pagination.js/dist/list.pagination.min.js'),
mermaid: path.join(__dirname, 'node_modules/mermaid/dist/mermaid.min.js'),
handlebars: path.join(__dirname, 'node_modules/handlebars/dist/handlebars.min.js'),
"jquery-ui-resizable": path.join(__dirname, 'public/vendor/jquery-ui/jquery-ui.min.js'),
"gist-embed": path.join(__dirname, 'node_modules/gist-embed/gist-embed.min.js'),
"bootstrap-tooltip": path.join(__dirname, 'public/vendor/bootstrap/tooltip.min.js'),
"headjs": path.join(__dirname, 'node_modules/reveal.js/lib/js/head.min.js'),
"reveal-markdown": path.join(__dirname, 'public/js/reveal-markdown.js')
'jquery-ui-resizable': path.join(__dirname, 'public/vendor/jquery-ui/jquery-ui.min.js'),
'gist-embed': path.join(__dirname, 'node_modules/gist-embed/gist-embed.min.js'),
'bootstrap-tooltip': path.join(__dirname, 'public/vendor/bootstrap/tooltip.min.js'),
'headjs': path.join(__dirname, 'node_modules/reveal.js/lib/js/head.min.js'),
'reveal-markdown': path.join(__dirname, 'public/js/reveal-markdown.js')
}
},
externals: {
"viz.js": "Viz",
"socket.io-client": "io",
"lodash": "_",
"jquery": "$",
"moment": "moment",
"handlebars": "Handlebars",
"highlight.js": "hljs",
"select2": "select2"
'viz.js': 'Viz',
'socket.io-client': 'io',
'lodash': '_',
'jquery': '$',
'moment': 'moment',
'handlebars': 'Handlebars',
'highlight.js': 'hljs',
'select2': 'select2'
},
module: {
@ -406,31 +405,30 @@ module.exports = {
test: /\.less$/,
loader: ExtractTextPlugin.extract('style-loader', 'less-loader')
}, {
test: require.resolve("js-sequence-diagrams"),
loader: "imports?Raphael=raphael"
test: require.resolve('js-sequence-diagrams'),
loader: 'imports?Raphael=raphael'
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
loader: 'file'
}, {
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?prefix=font/&limit=5000"
loader: 'url?prefix=font/&limit=5000'
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
loader: 'url?limit=10000&mimetype=application/octet-stream'
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
loader: 'url?limit=10000&mimetype=image/svg+xml'
}, {
test: /\.png(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/png"
loader: 'url?limit=10000&mimetype=image/png'
}, {
test: /\.gif(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/gif"
loader: 'url?limit=10000&mimetype=image/gif'
}]
},
node: {
fs: "empty"
fs: 'empty'
},
quiet: false,
@ -438,4 +436,4 @@ module.exports = {
stats: {
assets: false
}
};
}