You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-21 23:17:42 +02:00
All: Simplify Welcome notes and folders to avoid sync problems. Duplicate items will have to be manually deleted.
This commit is contained in:
@ -12,88 +12,51 @@ const { pregQuote } = require('lib/string-utils');
|
||||
class WelcomeUtils {
|
||||
|
||||
static async createWelcomeItems() {
|
||||
const overwriteExisting = Setting.value('env') === 'dev';
|
||||
|
||||
const output = {
|
||||
defaultFolderId: null,
|
||||
};
|
||||
|
||||
const noteAssets = welcomeAssets.notes;
|
||||
const appType = Setting.value('appType');
|
||||
|
||||
const folderAssets = welcomeAssets.folders;
|
||||
const tempDir = Setting.value('resourceDir');
|
||||
const timestamp = welcomeAssets.timestamp;
|
||||
|
||||
for (let i = 0; i < folderAssets.length; i++) {
|
||||
const folderAsset = folderAssets[i];
|
||||
const folderId = folderAsset.id;
|
||||
|
||||
if (!output.defaultFolderId) output.defaultFolderId = folderId;
|
||||
|
||||
let existingFolder = await Folder.load(folderId);
|
||||
|
||||
if (existingFolder && overwriteExisting) {
|
||||
await Folder.delete(existingFolder.id);
|
||||
existingFolder = null;
|
||||
}
|
||||
|
||||
if (existingFolder) continue;
|
||||
|
||||
await Folder.save({
|
||||
id: folderId,
|
||||
title: folderAsset.title,
|
||||
}, { isNew: true });
|
||||
const folder = await Folder.save({ title: folderAsset.title + ' (' + appType + ')'});
|
||||
if (!output.defaultFolderId) output.defaultFolderId = folder.id;
|
||||
}
|
||||
|
||||
const noteAssets = welcomeAssets.notes;
|
||||
|
||||
for (let i = noteAssets.length - 1; i >= 0; i--) {
|
||||
const noteAsset = noteAssets[i];
|
||||
|
||||
const noteId = noteAsset.id;
|
||||
|
||||
let existingNote = await Note.load(noteId);
|
||||
|
||||
if (existingNote && overwriteExisting) {
|
||||
await Note.delete(existingNote.id);
|
||||
existingNote = null;
|
||||
}
|
||||
|
||||
if (existingNote) continue;
|
||||
|
||||
let noteBody = noteAsset.body;
|
||||
|
||||
for (let resourceUrl in noteAsset.resources) {
|
||||
if (!noteAsset.resources.hasOwnProperty(resourceUrl)) continue;
|
||||
const resourceAsset = noteAsset.resources[resourceUrl];
|
||||
const resourceId = resourceAsset.id;
|
||||
|
||||
let existingResource = await Resource.load(resourceId);
|
||||
|
||||
if (existingResource && overwriteExisting) {
|
||||
await Resource.delete(resourceId);
|
||||
existingResource = null;
|
||||
}
|
||||
|
||||
if (!existingResource) {
|
||||
const ext = fileExtension(resourceUrl);
|
||||
const tempFilePath = tempDir + '/' + uuid.create() + '.tmp.' + ext;
|
||||
await shim.fsDriver().writeFile(tempFilePath, resourceAsset.body, 'base64');
|
||||
await shim.createResourceFromPath(tempFilePath, {
|
||||
id: resourceId,
|
||||
title: basename(resourceUrl),
|
||||
});
|
||||
await shim.fsDriver().remove(tempFilePath);
|
||||
}
|
||||
const ext = fileExtension(resourceUrl);
|
||||
const tempFilePath = tempDir + '/' + uuid.create() + '.tmp.' + ext;
|
||||
await shim.fsDriver().writeFile(tempFilePath, resourceAsset.body, 'base64');
|
||||
const resource = await shim.createResourceFromPath(tempFilePath, {
|
||||
title: basename(resourceUrl),
|
||||
});
|
||||
await shim.fsDriver().remove(tempFilePath);
|
||||
|
||||
const regex = new RegExp(pregQuote('(' + resourceUrl + ')'), 'g');
|
||||
noteBody = noteBody.replace(regex, '(:/' + resourceId + ')');
|
||||
noteBody = noteBody.replace(regex, '(:/' + resource.id + ')');
|
||||
}
|
||||
|
||||
await Note.save({
|
||||
id: noteId,
|
||||
parent_id: noteAsset.parent_id,
|
||||
const note = await Note.save({
|
||||
parent_id: output.defaultFolderId,
|
||||
title: noteAsset.title,
|
||||
body: noteBody,
|
||||
}, { isNew: true });
|
||||
});
|
||||
|
||||
if (noteAsset.tags) await Tag.setNoteTagsByTitles(noteId, noteAsset.tags);
|
||||
if (noteAsset.tags) await Tag.setNoteTagsByTitles(note.id, noteAsset.tags);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
Reference in New Issue
Block a user