Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/muness/obsidian-ics
Browse files Browse the repository at this point in the history
  • Loading branch information
muness committed Nov 10, 2023
2 parents 0e0d633 + ae207a0 commit 3ad9dc8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
48 changes: 28 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,33 @@ export default class ICSPlugin extends Plugin {
await this.saveSettings();
}

formatEvent(e: IEvent): string {
const callLinkOrLocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location;

// Conditionally format start and end time based on dataViewSyntax setting
const startTimeFormatted = this.data.format.dataViewSyntax ? `[startTime:: ${e.time}]` : `${e.time}`;
const endTimeFormatted = e.format.includeEventEndTime ? (this.data.format.dataViewSyntax ? `[endTime:: ${e.endTime}]` : `- ${e.endTime}`) : '';

// Combine all parts of the formatted event string
return [
`- ${e.format.checkbox ? '[ ]' : ''}`,
startTimeFormatted,
endTimeFormatted,
e.format.icsName ? e.icsName : '',
e.format.summary ? e.summary : '',
e.format.location ? callLinkOrLocation : '',
e.format.description && e.description ? `\n\t- ${e.description}` : '',
].filter(Boolean).join(' ').trim();
}

async getEvents(date: string) : Promise<IEvent[]> {
let events: IEvent[] = [];
let errorMessages: string[] = []; // To store error messages

for (const calendar in this.data.calendars) {
const calendarSetting = this.data.calendars[calendar];
let icsArray: any[] = [];

// Exception handling for downloading
try {
icsArray = parseIcs(await request({
Expand All @@ -59,11 +78,11 @@ export default class ICSPlugin extends Plugin {
}

var dateEvents;

// Exception handling for parsing and filtering
try {
dateEvents = filterMatchingEvents(icsArray, date);

} catch (filterError) {
console.error(`Error filtering events for calendar ${calendarSetting.icsName}: ${filterError}`);
errorMessages.push(`Error filtering events in calendar "${calendarSetting.icsName}"`);
Expand All @@ -72,7 +91,7 @@ export default class ICSPlugin extends Plugin {
try {
dateEvents.forEach((e) => {
const { callUrl, callType } = extractMeetingInfo(e);

let event: IEvent = {
utime: moment(e.start).format('X'),
time: moment(e.start).format(this.data.format.timeFormat),
Expand All @@ -92,15 +111,15 @@ export default class ICSPlugin extends Plugin {
errorMessages.push(`Error parsing events in calendar "${calendarSetting.icsName}"`);
}
}

// Notify the user if any errors were encountered
if (errorMessages.length > 0) {
const message = `Encountered ${errorMessages.length} error(s) while processing calendars:\n\n${errorMessages.join('\n')}\nSee console for details.`;
new Notice(message);
}

return events;
}
}

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

const mdArray = events.sort((a,b) => a.utime - b.utime).map(e => {
const callLinkOrlocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location;
return [
`- ${e.format?.checkbox ? '[ ]' : ''}`,
`${e.time}`,
e.format?.includeEventEndTime ? `- ${e.endTime}` : null,
e.format?.icsName ? e.icsName : null,
e.format?.summary ? e.summary : null,
e.format?.location ? callLinkOrlocation : null,
e.format?.description && e.description ? `\n\t- ${e.description}` : null,
].filter(Boolean).join(' ')
});
const mdArray = events.sort((a,b) => a.utime - b.utime).map(this.formatEvent, this);
editor.replaceRange(mdArray.join("\n"), editor.getCursor());
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/settings/ICSSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface ICSSettings {
format: {
timeFormat: string
dataViewSyntax: boolean,
},
calendars: Record < string, Calendar > ;
}
Expand Down Expand Up @@ -29,7 +30,8 @@ export const DEFAULT_CALENDAR_FORMAT = {

export const DEFAULT_SETTINGS: ICSSettings = {
format: {
timeFormat: "HH:mm"
timeFormat: "HH:mm",
dataViewSyntax: false,
},
calendars: {
}
Expand Down
64 changes: 37 additions & 27 deletions src/settings/ICSSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,40 @@ export default class ICSSettingsTab extends PluginSettingTab {
this.plugin = plugin;
}

// use this same format to create a description for the dataViewSyntax setting
private timeFormattingDescription(): DocumentFragment {
this.updateTimeFormatExample();

const descEl = document.createDocumentFragment();
descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.');
descEl.appendText(' For more syntax, refer to ');
descEl.appendChild(this.getMomentDocsLink());
descEl.appendText('.');

descEl.appendChild(document.createElement('p'));
descEl.appendText('Your current time format syntax looks like this: ');
descEl.appendChild(this.timeFormatExample);
descEl.appendText('.');
return descEl;
}

private timeFormattingDescription() {
this.updateTimeFormatExample();

const descEl = document.createDocumentFragment();
descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.');
descEl.appendText(' For more syntax, refer to ');
descEl.appendChild(this.getMomentDocsLink());
descEl.appendText('.');

descEl.appendChild(document.createElement('p'));
descEl.appendText('Your current time format syntax looks like this: ');
descEl.appendChild(this.timeFormatExample);
descEl.appendText('.');
return descEl;
}
private getMomentDocsLink(): HTMLAnchorElement {
const a = document.createElement('a');
a.href = 'https://momentjs.com/docs/#/displaying/format/';
a.text = 'format reference';
a.target = '_blank';
return a;
}

private updateTimeFormatExample() {
this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat);
}
private updateTimeFormatExample() {
this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat);
}

private getMomentDocsLink() {
const a = document.createElement('a');
a.href = 'https://momentjs.com/docs/#/displaying/format/';
a.text = 'format reference';
a.target = '_blank';
return a;
}
private dataViewSyntaxDescription(): DocumentFragment {
const descEl = document.createDocumentFragment();
descEl.appendText('Enable this option if you use the DataView plugin to query event start and end times.');
return descEl;
}

display(): void {
let {
Expand Down Expand Up @@ -166,8 +172,12 @@ private getMomentDocsLink() {
});
});



const dataViewSyntaxSetting = new Setting(containerEl)
.setName('DataView Metadata syntax for start and end times')
.setDesc(this.dataViewSyntaxDescription())
.addToggle(toggle => toggle
.setValue(this.plugin.data.format.dataViewSyntax || false)
.onChange(value => this.plugin.data.format.dataViewSyntax = value));

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

0 comments on commit 3ad9dc8

Please sign in to comment.