From 78c40f1739f3d17f2c487684e63e8aa1ff5b8d42 Mon Sep 17 00:00:00 2001 From: Dale de Silva Date: Fri, 6 Jan 2023 22:07:52 +1100 Subject: [PATCH] Added timestamps to created notes --- main.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.ts b/main.ts index d9b1d44..158c951 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { fileSyntax } from 'esbuild-sass-plugin/lib/utils'; -import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder, Vault } from 'obsidian'; +import { App, DataWriteOptions, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder, Vault } from 'obsidian'; @@ -495,12 +495,13 @@ async function importJson(file: File, folder: TFolder) { const content: KeepJson = JSON.parse(readerEvent.target.result as string); let path: string = `${folder.path}/${filenameSanitize(content.title || file.name)}`; // TODO: Strip file extension from filename let fileRef: TFile; + // Create new file try { // path = await getUnusedFilename(path); // fileRef = await plugin.app.vault.create(`${path}.md`, ''); - fileRef = await createNewEmptyMdFile(path); + fileRef = await createNewEmptyMdFile(path, {}); } catch (error) { failCount++; return Promise.reject(new Error(`Error creating new file ${path} (from ${file.name}. Error: ${error})`)); @@ -562,6 +563,15 @@ async function importJson(file: File, folder: TFolder) { } } + // Update created and modified date to match Keep data + const options: DataWriteOptions = { + ctime: content.createdTimestampUsec/1000, + mtime: content.userEditedTimestampUsec/1000 + } + await plugin.app.vault.append(fileRef, '', options); + // await plugin.app.vault.process(fileRef, (str) => str, options); // In docs, but not in class + + successCount++; resolve(fileRef); @@ -761,18 +771,18 @@ const singleOrPlural = (count: number, singleVersion: string, pluralVersion?: st -async function createNewEmptyMdFile(path: string, version: number = 1) : Promise { +async function createNewEmptyMdFile(path: string, options: DataWriteOptions, version: number = 1) : Promise { let fileRef: TFile; try { if(version == 1) { - fileRef = await plugin.app.vault.create(`${path}.md`, ''); + fileRef = await plugin.app.vault.create(`${path}.md`, '', options); } else { - fileRef = await plugin.app.vault.create(`${path} (${version}).md`, ''); + fileRef = await plugin.app.vault.create(`${path} (${version}).md`, '', options); } - } catch { - fileRef = await createNewEmptyMdFile(path, version+1); + } catch(error) { +; fileRef = await createNewEmptyMdFile(path, options, version+1); }