summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/note.js9
-rw-r--r--lib/response.js41
2 files changed, 27 insertions, 23 deletions
diff --git a/lib/note.js b/lib/note.js
index a73d2d1d..0965b23e 100644
--- a/lib/note.js
+++ b/lib/note.js
@@ -47,6 +47,7 @@ var note = {
checkNoteIdValid: checkNoteIdValid,
checkNoteExist: checkNoteExist,
getNoteTitle: getNoteTitle,
+ decodeTitle: decodeTitle,
generateWebTitle: generateWebTitle,
increaseViewCount: increaseViewCount,
updatePermission: updatePermission,
@@ -96,6 +97,14 @@ function getNoteTitle(body) {
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
function generateWebTitle(title) {
title = !title || title == "Untitled" ? "HackMD - Collaborative notes" : title + " - HackMD";
diff --git a/lib/response.js b/lib/response.js
index cc1020b3..d8f82a9f 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -56,7 +56,7 @@ var response = {
showNote: showNote,
showPublishNote: showPublishNote,
showPublishSlide: showPublishSlide,
- showIndex: showIndex,
+ showIndex: showIndex,
noteActions: noteActions,
publishNoteActions: publishNoteActions,
githubActions: githubActions
@@ -75,7 +75,7 @@ function responseError(res, code, detail, msg) {
code: code,
detail: detail,
msg: msg,
- useCDN: config.usecdn
+ useCDN: config.usecdn
});
res.write(content);
res.end();
@@ -99,16 +99,15 @@ function responseHackMD(res, noteId) {
if (err) {
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;
try {
meta = metaMarked(body).meta;
} catch(err) {
//na
}
- var title = data.rows[0].title;
- var decodedTitle = LZString.decompressFromBase64(title);
- if (decodedTitle) title = decodedTitle;
+ var title = Note.decodeTitle(notedata.title);
title = Note.generateWebTitle(title);
var template = config.hackmdpath;
var options = {
@@ -231,9 +230,7 @@ function showPublishNote(req, res, next) {
}
var updatetime = notedata.update_time;
var text = S(body).escapeHTML().s;
- var title = notedata.title;
- var decodedTitle = LZString.decompressFromBase64(title);
- if (decodedTitle) title = decodedTitle;
+ var title = Note.decodeTitle(notedata.title);
title = Note.generateWebTitle(title);
var origin = config.getserverurl();
var data = {
@@ -325,13 +322,16 @@ function actionDownload(req, res, noteId) {
if (err) {
return response.errorNotFound(res);
}
- var body = LZString.decompressFromBase64(data.rows[0].content);
- var title = Note.getNoteTitle(body);
+ var notedata = data.rows[0];
+ var body = LZString.decompressFromBase64(notedata.content);
+ var title = Note.decodeTitle(notedata.title);
+ var filename = title;
+ filename = encodeURIComponent(filename);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*', //allow CORS as API
'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private',
- 'Content-disposition': 'attachment; filename=' + title + '.md',
+ 'Content-disposition': 'attachment; filename=' + filename + '.md',
'Content-Length': body.length
});
res.end(body);
@@ -343,13 +343,14 @@ function actionPDF(req, res, noteId) {
if (err) {
return response.errorNotFound(res);
}
- var body = LZString.decompressFromBase64(data.rows[0].content);
+ var notedata = data.rows[0];
+ var body = LZString.decompressFromBase64(notedata.content);
try {
body = metaMarked(body).markdown;
} catch(err) {
//na
}
- var title = Note.getNoteTitle(body);
+ var title = Note.decodeTitle(notedata.title);
if (!fs.existsSync(config.tmppath)) {
fs.mkdirSync(config.tmppath);
@@ -545,10 +546,7 @@ function githubActionGist(req, res, noteId) {
var access_token = body.access_token;
if (access_token) {
var content = LZString.decompressFromBase64(notedata.content);
- var title = notedata.title;
- var decodedTitle = LZString.decompressFromBase64(title);
- if (decodedTitle) title = decodedTitle;
- else title = 'Untitled';
+ var title = Note.decodeTitle(notedata.title);
var filename = title.replace('/', ' ') + '.md';
var gist = {
"files": {}
@@ -612,9 +610,7 @@ function showPublishSlide(req, res, next) {
} catch(err) {
//na
}
- var title = notedata.title;
- var decodedTitle = LZString.decompressFromBase64(title);
- if (decodedTitle) title = decodedTitle;
+ var title = Note.decodeTitle(notedata.title);
title = Note.generateWebTitle(title);
var text = S(body).escapeHTML().s;
render(res, title, text);
@@ -640,5 +636,4 @@ var render = function (res, title, markdown) {
}));
};
-
-module.exports = response;
+module.exports = response; \ No newline at end of file