2018-11-26 23:47:02 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const noteService = require('../../services/notes');
|
|
|
|
const commonmark = require('commonmark');
|
|
|
|
const path = require('path');
|
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
async function importMarkdown(importContext, file, parentNote) {
|
2018-11-26 23:47:02 +01:00
|
|
|
const markdownContent = file.buffer.toString("UTF-8");
|
|
|
|
|
|
|
|
const reader = new commonmark.Parser();
|
|
|
|
const writer = new commonmark.HtmlRenderer();
|
|
|
|
|
|
|
|
const parsed = reader.parse(markdownContent);
|
|
|
|
const htmlContent = writer.render(parsed);
|
|
|
|
|
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, htmlContent, {
|
|
|
|
type: 'text',
|
|
|
|
mime: 'text/html'
|
|
|
|
});
|
|
|
|
|
2019-02-10 22:30:55 +01:00
|
|
|
importContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
async function importHtml(importContext, file, parentNote) {
|
2018-11-26 23:47:02 +01:00
|
|
|
const title = getFileNameWithoutExtension(file.originalname);
|
|
|
|
const content = file.buffer.toString("UTF-8");
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNote.noteId, title, content, {
|
|
|
|
type: 'text',
|
|
|
|
mime: 'text/html'
|
|
|
|
});
|
|
|
|
|
2019-02-10 22:30:55 +01:00
|
|
|
importContext.increaseProgressCount();
|
2019-02-10 19:36:03 +01:00
|
|
|
|
2018-11-26 23:47:02 +01:00
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileNameWithoutExtension(filePath) {
|
|
|
|
const extension = path.extname(filePath);
|
|
|
|
|
|
|
|
return filePath.substr(0, filePath.length - extension.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
importMarkdown,
|
|
|
|
importHtml
|
|
|
|
};
|