Skip to content

Commit 3ad9dc8

Browse files
committed
Merge branch 'master' of https://github.com/muness/obsidian-ics
2 parents 0e0d633 + ae207a0 commit 3ad9dc8

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

src/main.ts

+28-20
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,33 @@ export default class ICSPlugin extends Plugin {
4040
await this.saveSettings();
4141
}
4242

43+
formatEvent(e: IEvent): string {
44+
const callLinkOrLocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location;
45+
46+
// Conditionally format start and end time based on dataViewSyntax setting
47+
const startTimeFormatted = this.data.format.dataViewSyntax ? `[startTime:: ${e.time}]` : `${e.time}`;
48+
const endTimeFormatted = e.format.includeEventEndTime ? (this.data.format.dataViewSyntax ? `[endTime:: ${e.endTime}]` : `- ${e.endTime}`) : '';
49+
50+
// Combine all parts of the formatted event string
51+
return [
52+
`- ${e.format.checkbox ? '[ ]' : ''}`,
53+
startTimeFormatted,
54+
endTimeFormatted,
55+
e.format.icsName ? e.icsName : '',
56+
e.format.summary ? e.summary : '',
57+
e.format.location ? callLinkOrLocation : '',
58+
e.format.description && e.description ? `\n\t- ${e.description}` : '',
59+
].filter(Boolean).join(' ').trim();
60+
}
61+
4362
async getEvents(date: string) : Promise<IEvent[]> {
4463
let events: IEvent[] = [];
4564
let errorMessages: string[] = []; // To store error messages
46-
65+
4766
for (const calendar in this.data.calendars) {
4867
const calendarSetting = this.data.calendars[calendar];
4968
let icsArray: any[] = [];
50-
69+
5170
// Exception handling for downloading
5271
try {
5372
icsArray = parseIcs(await request({
@@ -59,11 +78,11 @@ export default class ICSPlugin extends Plugin {
5978
}
6079

6180
var dateEvents;
62-
81+
6382
// Exception handling for parsing and filtering
6483
try {
6584
dateEvents = filterMatchingEvents(icsArray, date);
66-
85+
6786
} catch (filterError) {
6887
console.error(`Error filtering events for calendar ${calendarSetting.icsName}: ${filterError}`);
6988
errorMessages.push(`Error filtering events in calendar "${calendarSetting.icsName}"`);
@@ -72,7 +91,7 @@ export default class ICSPlugin extends Plugin {
7291
try {
7392
dateEvents.forEach((e) => {
7493
const { callUrl, callType } = extractMeetingInfo(e);
75-
94+
7695
let event: IEvent = {
7796
utime: moment(e.start).format('X'),
7897
time: moment(e.start).format(this.data.format.timeFormat),
@@ -92,15 +111,15 @@ export default class ICSPlugin extends Plugin {
92111
errorMessages.push(`Error parsing events in calendar "${calendarSetting.icsName}"`);
93112
}
94113
}
95-
114+
96115
// Notify the user if any errors were encountered
97116
if (errorMessages.length > 0) {
98117
const message = `Encountered ${errorMessages.length} error(s) while processing calendars:\n\n${errorMessages.join('\n')}\nSee console for details.`;
99118
new Notice(message);
100119
}
101-
120+
102121
return events;
103-
}
122+
}
104123

105124
async onload() {
106125
await this.loadSettings();
@@ -112,18 +131,7 @@ export default class ICSPlugin extends Plugin {
112131
const fileDate = getDateFromFile(view.file, "day").format("YYYY-MM-DD");
113132
var events: any[] = await this.getEvents(fileDate);
114133

115-
const mdArray = events.sort((a,b) => a.utime - b.utime).map(e => {
116-
const callLinkOrlocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location;
117-
return [
118-
`- ${e.format?.checkbox ? '[ ]' : ''}`,
119-
`${e.time}`,
120-
e.format?.includeEventEndTime ? `- ${e.endTime}` : null,
121-
e.format?.icsName ? e.icsName : null,
122-
e.format?.summary ? e.summary : null,
123-
e.format?.location ? callLinkOrlocation : null,
124-
e.format?.description && e.description ? `\n\t- ${e.description}` : null,
125-
].filter(Boolean).join(' ')
126-
});
134+
const mdArray = events.sort((a,b) => a.utime - b.utime).map(this.formatEvent, this);
127135
editor.replaceRange(mdArray.join("\n"), editor.getCursor());
128136
}
129137
});

src/settings/ICSSettings.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface ICSSettings {
22
format: {
33
timeFormat: string
4+
dataViewSyntax: boolean,
45
},
56
calendars: Record < string, Calendar > ;
67
}
@@ -29,7 +30,8 @@ export const DEFAULT_CALENDAR_FORMAT = {
2930

3031
export const DEFAULT_SETTINGS: ICSSettings = {
3132
format: {
32-
timeFormat: "HH:mm"
33+
timeFormat: "HH:mm",
34+
dataViewSyntax: false,
3335
},
3436
calendars: {
3537
}

src/settings/ICSSettingsTab.ts

+37-27
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,40 @@ export default class ICSSettingsTab extends PluginSettingTab {
3838
this.plugin = plugin;
3939
}
4040

41+
// use this same format to create a description for the dataViewSyntax setting
42+
private timeFormattingDescription(): DocumentFragment {
43+
this.updateTimeFormatExample();
44+
45+
const descEl = document.createDocumentFragment();
46+
descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.');
47+
descEl.appendText(' For more syntax, refer to ');
48+
descEl.appendChild(this.getMomentDocsLink());
49+
descEl.appendText('.');
50+
51+
descEl.appendChild(document.createElement('p'));
52+
descEl.appendText('Your current time format syntax looks like this: ');
53+
descEl.appendChild(this.timeFormatExample);
54+
descEl.appendText('.');
55+
return descEl;
56+
}
4157

42-
private timeFormattingDescription() {
43-
this.updateTimeFormatExample();
44-
45-
const descEl = document.createDocumentFragment();
46-
descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.');
47-
descEl.appendText(' For more syntax, refer to ');
48-
descEl.appendChild(this.getMomentDocsLink());
49-
descEl.appendText('.');
50-
51-
descEl.appendChild(document.createElement('p'));
52-
descEl.appendText('Your current time format syntax looks like this: ');
53-
descEl.appendChild(this.timeFormatExample);
54-
descEl.appendText('.');
55-
return descEl;
56-
}
58+
private getMomentDocsLink(): HTMLAnchorElement {
59+
const a = document.createElement('a');
60+
a.href = 'https://momentjs.com/docs/#/displaying/format/';
61+
a.text = 'format reference';
62+
a.target = '_blank';
63+
return a;
64+
}
5765

58-
private updateTimeFormatExample() {
59-
this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat);
60-
}
66+
private updateTimeFormatExample() {
67+
this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat);
68+
}
6169

62-
private getMomentDocsLink() {
63-
const a = document.createElement('a');
64-
a.href = 'https://momentjs.com/docs/#/displaying/format/';
65-
a.text = 'format reference';
66-
a.target = '_blank';
67-
return a;
68-
}
70+
private dataViewSyntaxDescription(): DocumentFragment {
71+
const descEl = document.createDocumentFragment();
72+
descEl.appendText('Enable this option if you use the DataView plugin to query event start and end times.');
73+
return descEl;
74+
}
6975

7076
display(): void {
7177
let {
@@ -166,8 +172,12 @@ private getMomentDocsLink() {
166172
});
167173
});
168174

169-
170-
175+
const dataViewSyntaxSetting = new Setting(containerEl)
176+
.setName('DataView Metadata syntax for start and end times')
177+
.setDesc(this.dataViewSyntaxDescription())
178+
.addToggle(toggle => toggle
179+
.setValue(this.plugin.data.format.dataViewSyntax || false)
180+
.onChange(value => this.plugin.data.format.dataViewSyntax = value));
171181

172182
// Sponsor link - Thank you!
173183
const divSponsor = containerEl.createDiv();

0 commit comments

Comments
 (0)