Skip to content

Commit

Permalink
FIX: support for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Canna71 committed Sep 21, 2023
1 parent 4e5e82d commit 59c3046
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ In the calendar you can select the single days or the week number (on the left).

The days tiles background gives an indication about how many notes were created or edited that day. The scale depends also on the average number of daily notes set in the settings.


# Obsidian Sync Issues
If you have Obsidian Sync, you might have noticed that synced file will have a creation timestamp corresponding to when they were synced to the other machines.
In version 1.1.9 a couple of settings were added in order to work around this issue by (optionally) reading from the file metadata the actual creation date of the note.
It is not mandatory that every note has a creation/modification date in the metadata, if it is missing the plugin will fallback to the file creation or modification date.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "chronology",
"name": "Chronology",
"version": "1.1.8",
"version": "1.1.9",
"minAppVersion": "0.15.0",
"description": "Enables to have a calendat and a timeline of the activities",
"author": "Gabriele Cannata",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "chronology",
"name": "Chronology",
"version": "1.1.8",
"version": "1.1.9",
"minAppVersion": "0.15.0",
"description": "Provides a calendar and a timeline of the notes creation and modification",
"author": "Gabriele Cannata",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-chronologyn",
"version": "1.1.8",
"version": "1.1.9",
"description": "Obsidian.md plugin that Provides a calendar and a timeline of the notes creation and modification",
"main": "main.js",
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions src/ChronologySettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ export class ChronologySettingTab extends PluginSettingTab {


})

// add setting for creation date attribute
new Setting(this.containerEl)
.setName("Creation Date Attribute")
.setDesc("The name of the metadata attribute to use for creation date")
.addText(cb=>{
cb
.setValue(this.plugin.settings.creationDateAttribute || "")
.onChange(async (value)=>{
this.plugin.settings.creationDateAttribute = value;
await this.plugin.saveSettings();
// this.display();
})
})

// add setting for modified date attribute
new Setting(this.containerEl)
.setName("Modified Date Attribute")
.setDesc("The name of the metadata attribute to use for modified date")
.addText(cb=>{
cb
.setValue(this.plugin.settings.modifiedDateAttribute || "")
.onChange(async (value)=>{
this.plugin.settings.modifiedDateAttribute = value;
await this.plugin.saveSettings();
// this.display();
})
})


}

Expand Down
40 changes: 33 additions & 7 deletions src/TimeIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,47 @@ export class TimeIndex implements ITimeIndex {
const { fromTime, toTime } = item.getTimeRange();

let notes = allNotes.reduce<NoteAttributes[]>((acc, note) => {
const createdTime = moment(note.stat.ctime);
const modifiedTime = moment(note.stat.mtime);
let createdTime = moment(note.stat.ctime);
let modifiedTime = moment(note.stat.mtime);
const creationStr = getChronologySettings().creationDateAttribute;
const modifiedStr = getChronologySettings().modifiedDateAttribute;
if(creationStr || modifiedStr ){
const md = app.metadataCache.getFileCache(note);
if(md?.frontmatter){
if(creationStr){
const ctime = md.frontmatter[creationStr];
if(ctime){
createdTime = moment(ctime);
}
}
if(modifiedStr){
const mtime = md.frontmatter[modifiedStr];
if(mtime){
modifiedTime = moment(mtime);
}
}
}
}

const matchCreated = createdTime.isBetween(fromTime, toTime);
const matchModified = modifiedTime.isBetween(fromTime, toTime);
const timeDiffMs = Math.abs(note.stat.mtime - note.stat.ctime);
// use momentjs to find the time difference between createdTime and modifiedTime
const timeDiffMs = moment.duration(modifiedTime.diff(createdTime)).asMilliseconds();

// gets the createdTime as a number
const ctime = createdTime.valueOf();
const mtime = modifiedTime.valueOf();


if (sortingStrategy === SortingStrategy.Mixed && matchCreated && matchModified && timeDiffMs > LIMIT_TIME_DIFF_MS) {
acc.push({
note,
time: note.stat.ctime,
time: ctime,
attribute: DateAttribute.Created
});
acc.push({
note,
time: note.stat.mtime,
time: mtime,
attribute: DateAttribute.Modified
});
return acc;
Expand All @@ -68,7 +94,7 @@ export class TimeIndex implements ITimeIndex {
) {
acc.push({
note,
time: note.stat.ctime,
time: ctime,
attribute: DateAttribute.Created
});
return acc;
Expand All @@ -78,7 +104,7 @@ export class TimeIndex implements ITimeIndex {
) {
acc.push({
note,
time: note.stat.mtime,
time: mtime,
attribute: DateAttribute.Modified
});
return acc
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface ChronologyPluginSettings {
useSimpleList: boolean;
groupItemsInSameSlot: boolean;
firstDayOfWeek: number;
creationDateAttribute?: string;
modifiedDateAttribute?: string;
}

const DEFAULT_SETTINGS: ChronologyPluginSettings = {
Expand All @@ -21,7 +23,9 @@ const DEFAULT_SETTINGS: ChronologyPluginSettings = {
avgDailyNotes: 3,
useSimpleList: false,
groupItemsInSameSlot: false,
firstDayOfWeek: -1 // locale default
firstDayOfWeek: -1, // locale default
creationDateAttribute: "",
modifiedDateAttribute: ""
}

let expSettings: ChronologyPluginSettings;
Expand Down

0 comments on commit 59c3046

Please sign in to comment.