Notes/src/services/import/single.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

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-24 13:10:47 +01:00
async function importSingleFile(importContext, file, parentNote) {
if (importContext.textImportedAsText) {
if (file.mimetype === 'text/html') {
return importHtml(importContext, file, parentNote);
} else if (file.mimetype === 'text/markdown') {
return importMarkdown(importContext, file, parentNote);
} else if (file.mimetype === 'text/plain') {
return importPlainText(importContext, file, parentNote);
}
}
}
async function importPlainText(importContext, file, parentNote) {
const title = getFileNameWithoutExtension(file.originalname);
const plainTextContent = file.buffer.toString("UTF-8");
const htmlContent = convertTextToHtml(plainTextContent);
const {note} = await noteService.createNote(parentNote.noteId, title, htmlContent, {
type: 'text',
mime: 'text/html'
});
importContext.increaseProgressCount();
return note;
}
function convertTextToHtml(text) {
// 1: Plain Text Search
text = text.replace(/&/g, "&").
replace(/</g, "&lt;").
replace(/>/g, "&gt;");
// 2: Line Breaks
text = text.replace(/\r\n?|\n/g, "<br>");
// 3: Paragraphs
text = text.replace(/<br>\s*<br>/g, "</p><p>");
// 4: Wrap in Paragraph Tags
text = "<p>" + text + "</p>";
return text;
}
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 = {
2019-02-24 13:10:47 +01:00
importSingleFile
2018-11-26 23:47:02 +01:00
};