mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
import plain text file as text/html
This commit is contained in:
parent
b7bd94b6b0
commit
d9429c4f4b
@ -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) {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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, "<").
|
||||
replace(/>/g, ">");
|
||||
|
||||
// 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
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user