From 66010c31bf0f89ec6a2533206a7c6d7141739a08 Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 22 May 2025 15:56:08 -0700 Subject: [PATCH 1/2] move Google Calendar URL generation to a helper function --- .../s2b_hugo_theme/assets/js/cal/helpers.js | 29 +++++++++++++++++++ .../s2b_hugo_theme/assets/js/cal/main.js | 28 +----------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js b/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js index 1e99c485..5219a9a4 100644 --- a/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js +++ b/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js @@ -71,6 +71,35 @@ } }; + $.fn.getAddToGoogleLink = function(event) { + const googleCalUrl = new URL('https://www.google.com/calendar/render'); + + const startDate = dayjs(`${event.date} ${event.time}`).toISOString(); + const duration = event.duration ?? 60; // Google requires a duration and defaults to 60 minutes anyway + const endDate = dayjs(startDate).add(dayjs.duration({ 'minute': duration })).toISOString(); + /** + * Matches anything that is not a word or whitespace + * @example + * "2025-05-21T16:30:00.000Z".replace(regex, '') // 20250521T163000000Z + */ + const regex = /[^\w\s]/gi; + + // Remove colons and periods for Google Calendar URL (2025-05-21T16:30:00.000Z => 20250521T163000000Z) + const calendarDates = `${startDate.replace(regex, '')}/${endDate.replace(regex, '')}`; + + googleCalUrl.search = new URLSearchParams({ + action: "TEMPLATE", + text: `shift2Bikes: ${event.title}`, + location: event.address, + details: `${event.details}\n\n${event.shareable}`, + dates: calendarDates, + sf: true, // ?? + output: 'xml' + }); + + return googleCalUrl; + }; + $.fn.compareTimes = function ( event1, event2 ) { if ( event1.time < event2.time ) { return -1; diff --git a/site/themes/s2b_hugo_theme/assets/js/cal/main.js b/site/themes/s2b_hugo_theme/assets/js/cal/main.js index a472d408..3a30b522 100755 --- a/site/themes/s2b_hugo_theme/assets/js/cal/main.js +++ b/site/themes/s2b_hugo_theme/assets/js/cal/main.js @@ -56,33 +56,7 @@ $(document).ready(function() { exportURL.searchParams.set('id', value.id); value.exportlink = exportURL.toString(); - /** - * Add To Google Calendar - */ - const googleCalUrl = new URL('https://www.google.com/calendar/render') - const startDate = dayjs(`${value.date} ${value.time}`).toISOString() - const duration = value.duration ?? 60 // Google requires a duration and defaults to 60 minutes anyway - const endDate = dayjs(startDate).add(dayjs.duration({ 'minute': duration })).toISOString() - /** - * Matches anything that is not a word or whitespace - * @example - * "2025-05-21T16:30:00.000Z".replace(regex, '') // 20250521T163000000Z - */ - const regex = /[^\w\s]/gi - - // Remove colons and periods for Google Calendar URL (2025-05-21T16:30:00.000Z => 20250521T163000000Z) - const calendarDates = `${startDate.replace(regex, '')}/${endDate.replace(regex, '')}` - - googleCalUrl.search = new URLSearchParams({ - action: "TEMPLATE", - text: `shift2Bikes: ${value.title}`, - location: value.address, - details: `${value.details}\n\n${value.shareable}`, - dates: calendarDates, - sf: true, // ?? - output: 'xml' - }) - value.addToGoogleLink = googleCalUrl.toString() + value.addToGoogleLink = container.getAddToGoogleLink(value); groupedByDate[date].events.push(value); }); From 1ca6344ea9fb815706858438ef57c58d33a31beb Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 22 May 2025 16:03:19 -0700 Subject: [PATCH 2/2] restore accidentally-removed toString() --- site/themes/s2b_hugo_theme/assets/js/cal/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js b/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js index 5219a9a4..604e3753 100644 --- a/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js +++ b/site/themes/s2b_hugo_theme/assets/js/cal/helpers.js @@ -97,7 +97,7 @@ output: 'xml' }); - return googleCalUrl; + return googleCalUrl.toString(); }; $.fn.compareTimes = function ( event1, event2 ) {