Created
October 27, 2021 18:48
-
-
Save conficient/8ed58874ba0dfdfab88161016a4a3a0c to your computer and use it in GitHub Desktop.
A simple ICalendar with no requirement for external libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@if(IsValid) | |
{ | |
<a href=@dataUrl download=@Filename >@ChildContent</a> | |
} | |
@code | |
{ | |
[Parameter] public DateTime? Start { get; set; } | |
[Parameter] public DateTime? End { get; set; } | |
[Parameter] public string Summary { get; set; } | |
[Parameter] public string Description { get; set; } | |
[Parameter] public RenderFragment ChildContent {get;set;} | |
[Parameter] public string Filename { get; set; } = "Calendar.ics"; | |
string dataUrl => GetDataUrl(); | |
string GetDataUrl() | |
{ | |
var text = GetCalendarText(); | |
var data = System.Text.Encoding.UTF8.GetBytes(text); | |
var base64 = Convert.ToBase64String(data); | |
return $"data:text/calendar;base64,{base64}"; | |
} | |
bool IsValid => (Start.HasValue && End.HasValue); | |
string GetCalendarText() | |
{ | |
var id = Guid.NewGuid().ToString(); | |
return | |
$@"BEGIN:VCALENDAR | |
PRODID:-//BlazorICalendar//NONSGML BlazorICalendar//EN | |
VERSION:2.0 | |
BEGIN:VEVENT | |
DESCRIPTION:{Description} | |
DTEND:{GetDate(End)} | |
DTSTAMP:{GetDate(DateTime.UtcNow)}Z | |
DTSTART:{GetDate(Start)} | |
SEQUENCE:0 | |
SUMMARY:{Summary} | |
UID:{id} | |
END:VEVENT | |
END:VCALENDAR"; | |
} | |
private string GetDate(DateTime value) | |
{ | |
return value.ToString("yyyyMMdd\THHmmss"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A really simple ICalendar generator with no library imports. Set the
Start
andEnd
dates and the optionalSummary
andDescription
fields.e.g.
Warning: this is rough-and-ready, I haven't really checked how the descriptions/summary should be encoded, or how dates and timezones are supported.