Notes/src/services/export/single.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-11-24 20:58:38 +01:00
"use strict";
const mimeTypes = require('mime-types');
const html = require('html');
const utils = require('../utils');
const mdService = require('./md');
2018-11-24 20:58:38 +01:00
2020-06-20 12:31:38 +02:00
function exportSingleNote(taskContext, branch, format, res) {
const note = branch.getNote();
2018-11-24 20:58:38 +01:00
if (note.type === 'image' || note.type === 'file') {
return [400, `Note type ${note.type} cannot be exported as single file.`];
}
if (format !== 'html' && format !== 'markdown') {
return [400, 'Unrecognized format ' + format];
}
let payload, extension, mime;
2020-06-20 12:31:38 +02:00
let content = note.getContent();
2019-02-06 21:29:23 +01:00
2018-11-24 20:58:38 +01:00
if (note.type === 'text') {
if (format === 'html') {
if (!content.toLowerCase().includes("<html")) {
content = '<html><head><meta charset="utf-8"></head><body>' + content + '</body></html>';
}
payload = html.prettyPrint(content, {indent_size: 2});
2018-11-24 20:58:38 +01:00
extension = 'html';
mime = 'text/html';
}
else if (format === 'markdown') {
payload = mdService.toMarkdown(content);
2018-11-24 20:58:38 +01:00
extension = 'md';
2019-02-25 21:57:11 +01:00
mime = 'text/x-markdown'
2018-11-24 20:58:38 +01:00
}
}
else if (note.type === 'code') {
payload = content;
2018-11-24 20:58:38 +01:00
extension = mimeTypes.extension(note.mime) || 'code';
mime = note.mime;
}
2022-05-09 16:18:06 +02:00
else if (note.type === 'relation-map' || note.type === 'canvas-note' || note.type === 'search') {
payload = content;
2018-11-24 20:58:38 +01:00
extension = 'json';
mime = 'application/json';
}
const filename = note.title + "." + extension;
2018-11-24 20:58:38 +01:00
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
2018-11-24 20:58:38 +01:00
res.setHeader('Content-Type', mime + '; charset=UTF-8');
res.send(payload);
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
taskContext.increaseProgressCount();
taskContext.taskSucceeded();
2018-11-24 20:58:38 +01:00
}
module.exports = {
exportSingleNote
2020-06-20 12:31:38 +02:00
};