mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	allow import of single HTML file too
This commit is contained in:
		@@ -4,7 +4,7 @@ const repository = require('../../services/repository');
 | 
			
		||||
const enexImportService = require('../../services/import/enex');
 | 
			
		||||
const opmlImportService = require('../../services/import/opml');
 | 
			
		||||
const tarImportService = require('../../services/import/tar');
 | 
			
		||||
const markdownImportService = require('../../services/import/markdown');
 | 
			
		||||
const singleImportService = require('../../services/import/single');
 | 
			
		||||
const cls = require('../../services/cls');
 | 
			
		||||
const path = require('path');
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +35,10 @@ async function importToBranch(req) {
 | 
			
		||||
        return await opmlImportService.importOpml(file.buffer, parentNote);
 | 
			
		||||
    }
 | 
			
		||||
    else if (extension === '.md') {
 | 
			
		||||
        return await markdownImportService.importMarkdown(file, parentNote);
 | 
			
		||||
        return await singleImportService.importMarkdown(file, parentNote);
 | 
			
		||||
    }
 | 
			
		||||
    else if (extension === '.html' || extension === '.htm') {
 | 
			
		||||
        return await singleImportService.importHtml(file, parentNote);
 | 
			
		||||
    }
 | 
			
		||||
    else if (extension === '.enex') {
 | 
			
		||||
        return await enexImportService.importEnex(file, parentNote);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,30 +0,0 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
// note that this is for import of single markdown file only - for archive/structure of markdown files
 | 
			
		||||
// see tar export/import
 | 
			
		||||
 | 
			
		||||
const noteService = require('../../services/notes');
 | 
			
		||||
const commonmark = require('commonmark');
 | 
			
		||||
 | 
			
		||||
async function importMarkdown(file, parentNote) {
 | 
			
		||||
    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 = file.originalname.substr(0, file.originalname.length - 3); // strip .md extension
 | 
			
		||||
 | 
			
		||||
    const {note} = await noteService.createNote(parentNote.noteId, title, htmlContent, {
 | 
			
		||||
        type: 'text',
 | 
			
		||||
        mime: 'text/html'
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return note;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    importMarkdown
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										47
									
								
								src/services/import/single.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/services/import/single.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
const noteService = require('../../services/notes');
 | 
			
		||||
const commonmark = require('commonmark');
 | 
			
		||||
const path = require('path');
 | 
			
		||||
 | 
			
		||||
async function importMarkdown(file, parentNote) {
 | 
			
		||||
    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'
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return note;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function importHtml(file, parentNote) {
 | 
			
		||||
    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'
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return note;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getFileNameWithoutExtension(filePath) {
 | 
			
		||||
    const extension = path.extname(filePath);
 | 
			
		||||
 | 
			
		||||
    return filePath.substr(0, filePath.length - extension.length);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    importMarkdown,
 | 
			
		||||
    importHtml
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user