Skip to content

Build your personal knowledge base with Trilium Notes

import plain text file as text/html

zadamd9429c4f4b14
diff --git a/src/routes/api/import.js b/src/routes/api/import.js
index b8e4f67df8..5d14d9fc0e 100644
--- a/src/routes/api/import.js
+++ b/src/routes/api/import.js
@@ -49,14 +49,10 @@ async function importToBranch(req) {
note = await tarImportService.importTar(importContext, file.buffer, parentNote);
} else if (extension === '.opml') {
note = await opmlImportService.importOpml(importContext, file.buffer, parentNote);
- } else if (extension === '.md') {
- note = await singleImportService.importMarkdown(importContext, file, parentNote);
- } else if (extension === '.html' || extension === '.htm') {
- note = await singleImportService.importHtml(importContext, file, parentNote);
} else if (extension === '.enex') {
note = await enexImportService.importEnex(importContext, file, parentNote);
} else {
- return [400, `Unrecognized extension ${extension}, must be .tar or .opml`];
+ note = await singleImportService.importSingleFile(importContext, file, parentNote);
}
}
catch (e) {
diff --git a/src/services/image.js b/src/services/image.js
index c08c6f525e..fd879a67be 100644
--- a/src/services/image.js
+++ b/src/services/image.js
@@ -12,8 +12,8 @@ const jimp = require('jimp');
const imageType = require('image-type');
const sanitizeFilename = require('sanitize-filename');
-async function saveImage(buffer, originalName, parentNoteId, shrinkImage) {
- const finalImageBuffer = shrinkImage ? await shrinkImage(buffer) : buffer;
+async function saveImage(buffer, originalName, parentNoteId, shrinkImageSwitch) {
+ const finalImageBuffer = shrinkImageSwitch ? await shrinkImage(buffer) : buffer;
const imageFormat = imageType(finalImageBuffer);
diff --git a/src/services/import/single.js b/src/services/import/single.js
index 487c8a2f30..c1864c8f15 100644
--- a/src/services/import/single.js
+++ b/src/services/import/single.js
@@ -4,6 +4,52 @@ const noteService = require('../../services/notes');
const commonmark = require('commonmark');
const path = require('path');
+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;
+}
+
async function importMarkdown(importContext, file, parentNote) {
const markdownContent = file.buffer.toString("UTF-8");
@@ -46,6 +92,5 @@ function getFileNameWithoutExtension(filePath) {
}
module.exports = {
- importMarkdown,
- importHtml
+ importSingleFile
};
\ No newline at end of file