mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
fix import and replace tree
This commit is contained in:
parent
b9b8b35342
commit
007e45ad8c
@ -15,14 +15,14 @@ const yauzl = require("yauzl");
|
|||||||
const htmlSanitizer = require('./html_sanitizer');
|
const htmlSanitizer = require('./html_sanitizer');
|
||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
|
|
||||||
const HELP_FILE_PATH = '/home/adam/Downloads/Help1.zip';
|
const HELP_FILE_PATH = '/home/adam/Downloads/Help4.zip';
|
||||||
|
|
||||||
beccaLoader.beccaLoaded.then(() => {
|
beccaLoader.beccaLoaded.then(() => {
|
||||||
cls.init(async () => {
|
cls.init(async () => {
|
||||||
const helpRoot = becca.getNote("_help");
|
const hiddenRoot = becca.getNote("_hidden");
|
||||||
const data = await fs.readFile(HELP_FILE_PATH, "binary");
|
const data = await fs.readFile(HELP_FILE_PATH, "binary");
|
||||||
|
|
||||||
await importZip(Buffer.from(data, 'binary'), helpRoot);
|
await importZip(Buffer.from(data, 'binary'), hiddenRoot);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
|
|
||||||
for (const segment of pathSegments) {
|
for (const segment of pathSegments) {
|
||||||
if (!cursor || !cursor.children || cursor.children.length === 0) {
|
if (!cursor || !cursor.children || cursor.children.length === 0) {
|
||||||
return {};
|
throw new Error(`Note meta for '${filePath}' not found.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
parent = cursor;
|
parent = cursor;
|
||||||
@ -75,7 +75,13 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getNoteId(noteMeta) {
|
function getNoteId(noteMeta) {
|
||||||
const helpNoteId = noteMeta.attributes?.find(attr => attr.type === 'label' && attr.name === 'helpNoteId')?.value;
|
let helpNoteId = noteMeta.attributes?.find(attr => attr.type === 'label' && attr.name === 'helpNoteId')?.value;
|
||||||
|
|
||||||
|
helpNoteId = '_help' + noteMeta.title.replace(/[^a-z0-9]/ig, '');
|
||||||
|
|
||||||
|
if (helpNoteId === '_helpHelp') {
|
||||||
|
helpNoteId = '_help';
|
||||||
|
}
|
||||||
|
|
||||||
const noteId = helpNoteId || noteMeta.noteId;
|
const noteId = helpNoteId || noteMeta.noteId;
|
||||||
noteIdMap[noteMeta.noteId] = noteId;
|
noteIdMap[noteMeta.noteId] = noteId;
|
||||||
@ -141,6 +147,7 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
isExpanded: noteMeta.isExpanded,
|
isExpanded: noteMeta.isExpanded,
|
||||||
notePosition: noteMeta.notePosition,
|
notePosition: noteMeta.notePosition,
|
||||||
isProtected: false,
|
isProtected: false,
|
||||||
|
ignoreForbiddenParents: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
saveAttributes(note, noteMeta);
|
saveAttributes(note, noteMeta);
|
||||||
@ -343,6 +350,7 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
isExpanded: noteMeta.isExpanded,
|
isExpanded: noteMeta.isExpanded,
|
||||||
notePosition: noteMeta.notePosition,
|
notePosition: noteMeta.notePosition,
|
||||||
isProtected: false,
|
isProtected: false,
|
||||||
|
ignoreForbiddenParents: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
saveAttributes(note, noteMeta);
|
saveAttributes(note, noteMeta);
|
||||||
@ -374,11 +382,9 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
metaFile = JSON.parse(entries.find(entry => entry.type === 'file' && entry.filePath === '!!!meta.json').content);
|
metaFile = JSON.parse(entries.find(entry => entry.type === 'file' && entry.filePath === '!!!meta.json').content);
|
||||||
|
|
||||||
sql.transactional(() => {
|
sql.transactional(() => {
|
||||||
|
deleteHelpSubtree();
|
||||||
|
|
||||||
for (const {type, filePath, content} of entries) {
|
for (const {type, filePath, content} of entries) {
|
||||||
|
|
||||||
console.log(filePath);
|
|
||||||
|
|
||||||
|
|
||||||
if (type === 'directory') {
|
if (type === 'directory') {
|
||||||
saveDirectory(filePath);
|
saveDirectory(filePath);
|
||||||
} else if (type === 'file') {
|
} else if (type === 'file') {
|
||||||
@ -405,6 +411,30 @@ async function importZip(fileBuffer, importRootNote) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a special implementation of deleting the subtree, because we want to preserve the links to the help pages
|
||||||
|
* and clones.
|
||||||
|
*/
|
||||||
|
function deleteHelpSubtree() {
|
||||||
|
const DELETE_ID = 'help';
|
||||||
|
|
||||||
|
function remove(branch) {
|
||||||
|
branch.markAsDeleted(DELETE_ID);
|
||||||
|
|
||||||
|
const note = becca.getNote(branch.noteId);
|
||||||
|
|
||||||
|
for (const branch of note.getChildBranches()) {
|
||||||
|
remove(branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
note.getOwnedAttributes().forEach(attr => attr.markAsDeleted(DELETE_ID));
|
||||||
|
|
||||||
|
note.markAsDeleted(DELETE_ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
remove(becca.getBranchFromChildAndParent('_help', '_hidden'));
|
||||||
|
}
|
||||||
|
|
||||||
/** @returns {string} path without leading or trailing slash and backslashes converted to forward ones */
|
/** @returns {string} path without leading or trailing slash and backslashes converted to forward ones */
|
||||||
function normalizeFilePath(filePath) {
|
function normalizeFilePath(filePath) {
|
||||||
filePath = filePath.replace(/\\/g, "/");
|
filePath = filePath.replace(/\\/g, "/");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user