mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	fixes & refactoring to markdown import
This commit is contained in:
		@@ -99,7 +99,7 @@ const contextMenuOptions = {
 | 
				
			|||||||
            {title: "OPML", cmd: "exportSubtreeToOpml"},
 | 
					            {title: "OPML", cmd: "exportSubtreeToOpml"},
 | 
				
			||||||
            {title: "Markdown", cmd: "exportSubtreeToMarkdown"}
 | 
					            {title: "Markdown", cmd: "exportSubtreeToMarkdown"}
 | 
				
			||||||
        ]},
 | 
					        ]},
 | 
				
			||||||
        {title: "Import into note (tar, opml)", cmd: "importIntoNote", uiIcon: "ui-icon-arrowthick-1-sw"},
 | 
					        {title: "Import into note (tar, opml, md)", cmd: "importIntoNote", uiIcon: "ui-icon-arrowthick-1-sw"},
 | 
				
			||||||
        {title: "----"},
 | 
					        {title: "----"},
 | 
				
			||||||
        {title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "ui-icon-minus"},
 | 
					        {title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "ui-icon-minus"},
 | 
				
			||||||
        {title: "Force note sync", cmd: "forceNoteSync", uiIcon: "ui-icon-refresh"},
 | 
					        {title: "Force note sync", cmd: "forceNoteSync", uiIcon: "ui-icon-refresh"},
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -77,23 +77,22 @@ async function importOpml(file, parentNoteId) {
 | 
				
			|||||||
async function importTar(file, parentNoteId) {
 | 
					async function importTar(file, parentNoteId) {
 | 
				
			||||||
    const files = await parseImportFile(file);
 | 
					    const files = await parseImportFile(file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const ctx = {
 | 
				
			||||||
        // maps from original noteId (in tar file) to newly generated noteId
 | 
					        // maps from original noteId (in tar file) to newly generated noteId
 | 
				
			||||||
    const noteIdMap = {};
 | 
					        noteIdMap: {},
 | 
				
			||||||
    const attributes = [];
 | 
					        attributes: [],
 | 
				
			||||||
 | 
					 | 
				
			||||||
    const markdown = {
 | 
					 | 
				
			||||||
        reader: new commonmark.Parser(),
 | 
					        reader: new commonmark.Parser(),
 | 
				
			||||||
        writer: new commonmark.HtmlRenderer()
 | 
					        writer: new commonmark.HtmlRenderer()
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await importNotes(markdown, files, parentNoteId, noteIdMap, attributes);
 | 
					    await importNotes(ctx, files, parentNoteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // we save attributes after importing notes because we need to have all the relation
 | 
					    // we save attributes after importing notes because we need to have all the relation
 | 
				
			||||||
    // targets already existing
 | 
					    // targets already existing
 | 
				
			||||||
    for (const attr of attributes) {
 | 
					    for (const attr of ctx.attributes) {
 | 
				
			||||||
        if (attr.type === 'relation') {
 | 
					        if (attr.type === 'relation') {
 | 
				
			||||||
            // map to local noteId
 | 
					            // map to local noteId
 | 
				
			||||||
            attr.value = noteIdMap[attr.value];
 | 
					            attr.value = ctx.noteIdMap[attr.value];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!attr.value) {
 | 
					            if (!attr.value) {
 | 
				
			||||||
                // relation is targeting note not present in the import
 | 
					                // relation is targeting note not present in the import
 | 
				
			||||||
@@ -121,8 +120,7 @@ function getFileName(name) {
 | 
				
			|||||||
        name = name.substr(0, name.length - 5);
 | 
					        name = name.substr(0, name.length - 5);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        // this is supposed to be directory
 | 
					        log.error("Unknown file type in import: " + name);
 | 
				
			||||||
        key = "data";
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {name, key};
 | 
					    return {name, key};
 | 
				
			||||||
@@ -135,7 +133,15 @@ async function parseImportFile(file) {
 | 
				
			|||||||
    const extract = tar.extract();
 | 
					    const extract = tar.extract();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    extract.on('entry', function(header, stream, next) {
 | 
					    extract.on('entry', function(header, stream, next) {
 | 
				
			||||||
        const {name, key} = getFileName(header.name);
 | 
					        let name, key;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (header.type === 'file') {
 | 
				
			||||||
 | 
					            ({name, key} = getFileName(header.name));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            name = header.name;
 | 
				
			||||||
 | 
					            key = 'directory';
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let file = fileMap[name];
 | 
					        let file = fileMap[name];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -190,13 +196,20 @@ async function parseImportFile(file) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function importNotes(markdown, files, parentNoteId, noteIdMap, attributes) {
 | 
					async function importNotes(ctx, files, parentNoteId) {
 | 
				
			||||||
    for (const file of files) {
 | 
					    for (const file of files) {
 | 
				
			||||||
        let note;
 | 
					        let note;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (file.markdown) {
 | 
					        if (!file.meta) {
 | 
				
			||||||
            const parsed = markdown.reader.parse(file.markdown.toString("UTF-8"));
 | 
					            let content = '';
 | 
				
			||||||
            const content = markdown.writer.render(parsed);
 | 
					
 | 
				
			||||||
 | 
					            if (file.data) {
 | 
				
			||||||
 | 
					                content = file.data.toString("UTF-8");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else if (file.markdown) {
 | 
				
			||||||
 | 
					                const parsed = ctx.reader.parse(file.markdown.toString("UTF-8"));
 | 
				
			||||||
 | 
					                content = ctx.writer.render(parsed);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            note = (await noteService.createNote(parentNoteId, file.name, content, {
 | 
					            note = (await noteService.createNote(parentNoteId, file.name, content, {
 | 
				
			||||||
                type: 'text',
 | 
					                type: 'text',
 | 
				
			||||||
@@ -211,7 +224,7 @@ async function importNotes(markdown, files, parentNoteId, noteIdMap, attributes)
 | 
				
			|||||||
            if (file.meta.clone) {
 | 
					            if (file.meta.clone) {
 | 
				
			||||||
                await new Branch({
 | 
					                await new Branch({
 | 
				
			||||||
                    parentNoteId: parentNoteId,
 | 
					                    parentNoteId: parentNoteId,
 | 
				
			||||||
                    noteId: noteIdMap[file.meta.noteId],
 | 
					                    noteId: ctx.noteIdMap[file.meta.noteId],
 | 
				
			||||||
                    prefix: file.meta.prefix
 | 
					                    prefix: file.meta.prefix
 | 
				
			||||||
                }).save();
 | 
					                }).save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -228,10 +241,10 @@ async function importNotes(markdown, files, parentNoteId, noteIdMap, attributes)
 | 
				
			|||||||
                prefix: file.meta.prefix
 | 
					                prefix: file.meta.prefix
 | 
				
			||||||
            })).note;
 | 
					            })).note;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            noteIdMap[file.meta.noteId] = note.noteId;
 | 
					            ctx.noteIdMap[file.meta.noteId] = note.noteId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for (const attribute of file.meta.attributes) {
 | 
					            for (const attribute of file.meta.attributes) {
 | 
				
			||||||
                attributes.push({
 | 
					                ctx.attributes.push({
 | 
				
			||||||
                    noteId: note.noteId,
 | 
					                    noteId: note.noteId,
 | 
				
			||||||
                    type: attribute.type,
 | 
					                    type: attribute.type,
 | 
				
			||||||
                    name: attribute.name,
 | 
					                    name: attribute.name,
 | 
				
			||||||
@@ -243,7 +256,7 @@ async function importNotes(markdown, files, parentNoteId, noteIdMap, attributes)
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (file.children.length > 0) {
 | 
					        if (file.children.length > 0) {
 | 
				
			||||||
            await importNotes(markdown, file.children, note.noteId, noteIdMap, attributes);
 | 
					            await importNotes(ctx, file.children, note.noteId);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user