Fixed note title might get wrong

This commit is contained in:
Cheng-Han, Wu 2016-03-04 23:12:03 +08:00
parent 04eabff3c3
commit c183002c14
2 changed files with 27 additions and 23 deletions

View file

@ -47,6 +47,7 @@ var note = {
checkNoteIdValid: checkNoteIdValid, checkNoteIdValid: checkNoteIdValid,
checkNoteExist: checkNoteExist, checkNoteExist: checkNoteExist,
getNoteTitle: getNoteTitle, getNoteTitle: getNoteTitle,
decodeTitle: decodeTitle,
generateWebTitle: generateWebTitle, generateWebTitle: generateWebTitle,
increaseViewCount: increaseViewCount, increaseViewCount: increaseViewCount,
updatePermission: updatePermission, updatePermission: updatePermission,
@ -96,6 +97,14 @@ function getNoteTitle(body) {
return title; return title;
} }
// decode title
function decodeTitle(title) {
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
else title = 'Untitled';
return title;
}
//generate note web page title //generate note web page title
function generateWebTitle(title) { function generateWebTitle(title) {
title = !title || title == "Untitled" ? "HackMD - Collaborative notes" : title + " - HackMD"; title = !title || title == "Untitled" ? "HackMD - Collaborative notes" : title + " - HackMD";

View file

@ -56,7 +56,7 @@ var response = {
showNote: showNote, showNote: showNote,
showPublishNote: showPublishNote, showPublishNote: showPublishNote,
showPublishSlide: showPublishSlide, showPublishSlide: showPublishSlide,
showIndex: showIndex, showIndex: showIndex,
noteActions: noteActions, noteActions: noteActions,
publishNoteActions: publishNoteActions, publishNoteActions: publishNoteActions,
githubActions: githubActions githubActions: githubActions
@ -75,7 +75,7 @@ function responseError(res, code, detail, msg) {
code: code, code: code,
detail: detail, detail: detail,
msg: msg, msg: msg,
useCDN: config.usecdn useCDN: config.usecdn
}); });
res.write(content); res.write(content);
res.end(); res.end();
@ -99,16 +99,15 @@ function responseHackMD(res, noteId) {
if (err) { if (err) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
var body = LZString.decompressFromBase64(data.rows[0].content); var notedata = data.rows[0];
var body = LZString.decompressFromBase64(notedata.content);
var meta = null; var meta = null;
try { try {
meta = metaMarked(body).meta; meta = metaMarked(body).meta;
} catch(err) { } catch(err) {
//na //na
} }
var title = data.rows[0].title; var title = Note.decodeTitle(notedata.title);
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
title = Note.generateWebTitle(title); title = Note.generateWebTitle(title);
var template = config.hackmdpath; var template = config.hackmdpath;
var options = { var options = {
@ -231,9 +230,7 @@ function showPublishNote(req, res, next) {
} }
var updatetime = notedata.update_time; var updatetime = notedata.update_time;
var text = S(body).escapeHTML().s; var text = S(body).escapeHTML().s;
var title = notedata.title; var title = Note.decodeTitle(notedata.title);
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
title = Note.generateWebTitle(title); title = Note.generateWebTitle(title);
var origin = config.getserverurl(); var origin = config.getserverurl();
var data = { var data = {
@ -325,13 +322,16 @@ function actionDownload(req, res, noteId) {
if (err) { if (err) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
var body = LZString.decompressFromBase64(data.rows[0].content); var notedata = data.rows[0];
var title = Note.getNoteTitle(body); var body = LZString.decompressFromBase64(notedata.content);
var title = Note.decodeTitle(notedata.title);
var filename = title;
filename = encodeURIComponent(filename);
res.writeHead(200, { res.writeHead(200, {
'Access-Control-Allow-Origin': '*', //allow CORS as API 'Access-Control-Allow-Origin': '*', //allow CORS as API
'Content-Type': 'text/markdown; charset=UTF-8', 'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private', 'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + title + '.md', 'Content-disposition': 'attachment; filename=' + filename + '.md',
'Content-Length': body.length 'Content-Length': body.length
}); });
res.end(body); res.end(body);
@ -343,13 +343,14 @@ function actionPDF(req, res, noteId) {
if (err) { if (err) {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
var body = LZString.decompressFromBase64(data.rows[0].content); var notedata = data.rows[0];
var body = LZString.decompressFromBase64(notedata.content);
try { try {
body = metaMarked(body).markdown; body = metaMarked(body).markdown;
} catch(err) { } catch(err) {
//na //na
} }
var title = Note.getNoteTitle(body); var title = Note.decodeTitle(notedata.title);
if (!fs.existsSync(config.tmppath)) { if (!fs.existsSync(config.tmppath)) {
fs.mkdirSync(config.tmppath); fs.mkdirSync(config.tmppath);
@ -545,10 +546,7 @@ function githubActionGist(req, res, noteId) {
var access_token = body.access_token; var access_token = body.access_token;
if (access_token) { if (access_token) {
var content = LZString.decompressFromBase64(notedata.content); var content = LZString.decompressFromBase64(notedata.content);
var title = notedata.title; var title = Note.decodeTitle(notedata.title);
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
else title = 'Untitled';
var filename = title.replace('/', ' ') + '.md'; var filename = title.replace('/', ' ') + '.md';
var gist = { var gist = {
"files": {} "files": {}
@ -612,9 +610,7 @@ function showPublishSlide(req, res, next) {
} catch(err) { } catch(err) {
//na //na
} }
var title = notedata.title; var title = Note.decodeTitle(notedata.title);
var decodedTitle = LZString.decompressFromBase64(title);
if (decodedTitle) title = decodedTitle;
title = Note.generateWebTitle(title); title = Note.generateWebTitle(title);
var text = S(body).escapeHTML().s; var text = S(body).escapeHTML().s;
render(res, title, text); render(res, title, text);
@ -640,5 +636,4 @@ var render = function (res, title, markdown) {
})); }));
}; };
module.exports = response;
module.exports = response;