mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
improving import WIP
This commit is contained in:
parent
f6944b8219
commit
a06ddc439d
@ -23,16 +23,19 @@ const BAttachment = require("../../becca/entities/battachment");
|
|||||||
* @returns {Promise<*>}
|
* @returns {Promise<*>}
|
||||||
*/
|
*/
|
||||||
async function importZip(taskContext, fileBuffer, importRootNote) {
|
async function importZip(taskContext, fileBuffer, importRootNote) {
|
||||||
// maps from original noteId (in ZIP file) to newly generated noteId
|
/** @type {Object.<string, string>} maps from original noteId (in ZIP file) to newly generated noteId */
|
||||||
const noteIdMap = {};
|
const noteIdMap = {};
|
||||||
const attributes = [];
|
const attributes = [];
|
||||||
// path => noteId, used only when meta file is not available
|
// path => noteId, used only when meta file is not available
|
||||||
|
/** @type {Object.<string, string>} path => noteId */
|
||||||
const createdPaths = { '/': importRootNote.noteId, '\\': importRootNote.noteId };
|
const createdPaths = { '/': importRootNote.noteId, '\\': importRootNote.noteId };
|
||||||
const mdReader = new commonmark.Parser();
|
const mdReader = new commonmark.Parser();
|
||||||
const mdWriter = new commonmark.HtmlRenderer();
|
const mdWriter = new commonmark.HtmlRenderer();
|
||||||
let metaFile = null;
|
let metaFile = null;
|
||||||
|
/** @type {BNote} */
|
||||||
let firstNote = null;
|
let firstNote = null;
|
||||||
const createdNoteIds = {};
|
/** @type {Set.<string>} */
|
||||||
|
const createdNoteIds = new Set();
|
||||||
|
|
||||||
function getNewNoteId(origNoteId) {
|
function getNewNoteId(origNoteId) {
|
||||||
// in case the original noteId is empty. This probably shouldn't happen, but still good to have this precaution
|
// in case the original noteId is empty. This probably shouldn't happen, but still good to have this precaution
|
||||||
@ -52,6 +55,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
return noteIdMap[origNoteId];
|
return noteIdMap[origNoteId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {{noteMeta: NoteMeta, parentNoteMeta: NoteMeta, attachmentMeta: AttachmentMeta}} */
|
||||||
function getMeta(filePath) {
|
function getMeta(filePath) {
|
||||||
if (!metaFile) {
|
if (!metaFile) {
|
||||||
return {};
|
return {};
|
||||||
@ -99,6 +103,11 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} filePath
|
||||||
|
* @param {NoteMeta} parentNoteMeta
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
function getParentNoteId(filePath, parentNoteMeta) {
|
function getParentNoteId(filePath, parentNoteMeta) {
|
||||||
let parentNoteId;
|
let parentNoteId;
|
||||||
|
|
||||||
@ -123,6 +132,11 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
return parentNoteId;
|
return parentNoteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {NoteMeta} noteMeta
|
||||||
|
* @param {string} filePath
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
function getNoteId(noteMeta, filePath) {
|
function getNoteId(noteMeta, filePath) {
|
||||||
if (noteMeta) {
|
if (noteMeta) {
|
||||||
return getNewNoteId(noteMeta.noteId);
|
return getNewNoteId(noteMeta.noteId);
|
||||||
@ -148,6 +162,10 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
return { mime, type };
|
return { mime, type };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {BNote} note
|
||||||
|
* @param {NoteMeta} noteMeta
|
||||||
|
*/
|
||||||
function saveAttributes(note, noteMeta) {
|
function saveAttributes(note, noteMeta) {
|
||||||
if (!noteMeta) {
|
if (!noteMeta) {
|
||||||
return;
|
return;
|
||||||
@ -218,7 +236,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
isProtected: importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
createdNoteIds[note.noteId] = true;
|
createdNoteIds.add(note.noteId);
|
||||||
|
|
||||||
saveAttributes(note, noteMeta);
|
saveAttributes(note, noteMeta);
|
||||||
|
|
||||||
@ -254,6 +272,13 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
return targetNoteId;
|
return targetNoteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} content
|
||||||
|
* @param {string} noteTitle
|
||||||
|
* @param {string} filePath
|
||||||
|
* @param {NoteMeta} noteMeta
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
function processTextNoteContent(content, noteTitle, filePath, noteMeta) {
|
function processTextNoteContent(content, noteTitle, filePath, noteMeta) {
|
||||||
function isUrlAbsolute(url) {
|
function isUrlAbsolute(url) {
|
||||||
return /^(?:[a-z]+:)?\/\//i.test(url);
|
return /^(?:[a-z]+:)?\/\//i.test(url);
|
||||||
@ -344,6 +369,15 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {NoteMeta} noteMeta
|
||||||
|
* @param {string} type
|
||||||
|
* @param {string} mime
|
||||||
|
* @param {string|Buffer} content
|
||||||
|
* @param {string} noteTitle
|
||||||
|
* @param {string} filePath
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
function processNoteContent(noteMeta, type, mime, content, noteTitle, filePath) {
|
function processNoteContent(noteMeta, type, mime, content, noteTitle, filePath) {
|
||||||
if (noteMeta?.format === 'markdown'
|
if (noteMeta?.format === 'markdown'
|
||||||
|| (!noteMeta && taskContext.data.textImportedAsText && ['text/markdown', 'text/x-markdown'].includes(mime))) {
|
|| (!noteMeta && taskContext.data.textImportedAsText && ['text/markdown', 'text/x-markdown'].includes(mime))) {
|
||||||
@ -397,7 +431,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
const parentNoteId = getParentNoteId(filePath, parentNoteMeta);
|
const parentNoteId = getParentNoteId(filePath, parentNoteMeta);
|
||||||
|
|
||||||
if (!parentNoteId) {
|
if (!parentNoteId) {
|
||||||
throw new Error(`Cannot find parentNoteId for ${filePath}`);
|
throw new Error(`Cannot find parentNoteId for '${filePath}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noteMeta?.isClone) {
|
if (noteMeta?.isClone) {
|
||||||
@ -468,7 +502,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
isProtected: isProtected,
|
isProtected: isProtected,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
createdNoteIds[note.noteId] = true;
|
createdNoteIds.add(note.noteId);
|
||||||
|
|
||||||
saveAttributes(note, noteMeta);
|
saveAttributes(note, noteMeta);
|
||||||
|
|
||||||
@ -521,12 +555,12 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
zipfile.readEntry();
|
zipfile.readEntry();
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const noteId in createdNoteIds) { // now the noteIds are unique
|
for (const noteId of createdNoteIds) {
|
||||||
const note = becca.getNote(noteId);
|
const note = becca.getNote(noteId);
|
||||||
await noteService.asyncPostProcessContent(note, note.getContent());
|
await noteService.asyncPostProcessContent(note, note.getContent());
|
||||||
|
|
||||||
if (!metaFile) {
|
if (!metaFile) {
|
||||||
// if there's no meta file then the notes are created based on the order in that zip file but that
|
// if there's no meta file, then the notes are created based on the order in that zip file but that
|
||||||
// is usually quite random, so we sort the notes in the way they would appear in the file manager
|
// is usually quite random, so we sort the notes in the way they would appear in the file manager
|
||||||
treeService.sortNotes(noteId, 'title', false, true);
|
treeService.sortNotes(noteId, 'title', false, true);
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ class NoteMeta {
|
|||||||
dataFileName;
|
dataFileName;
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
dirFileName;
|
dirFileName;
|
||||||
|
/** @type {boolean} - this file should not be imported (e.g., HTML navigation) */
|
||||||
|
noImport = false;
|
||||||
/** @type {AttributeMeta[]} */
|
/** @type {AttributeMeta[]} */
|
||||||
attributes;
|
attributes;
|
||||||
/** @type {AttachmentMeta[]} */
|
/** @type {AttachmentMeta[]} */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user