From 2ebed9a05574021f173dcb359a536f7d66de4fcb Mon Sep 17 00:00:00 2001 From: "Michael J. Radwin" Date: Wed, 15 Dec 2021 12:34:15 -0800 Subject: [PATCH] Allow rss to override link URL options --- package.json | 2 +- rest-api.d.ts | 3 ++- src/rss.js | 44 +++++++++++++++++++++++++++++++++----------- src/rss.spec.js | 8 +++++--- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 43ea5e3..64e271f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hebcal/rest-api", - "version": "3.10.1", + "version": "3.10.2", "author": "Michael J. Radwin (https://github.com/mjradwin)", "keywords": [ "hebcal" diff --git a/rest-api.d.ts b/rest-api.d.ts index 78bb0e1..cb0458b 100644 --- a/rest-api.d.ts +++ b/rest-api.d.ts @@ -40,7 +40,8 @@ declare module '@hebcal/rest-api' { */ export function eventsToRss(events: Event[], location: Location, mainUrl: string, selfUrl: string, lang?: string, evPubDate?: boolean): string; export function eventsToRss2(events: Event[], options: HebrewCalendar.Options): string; - export function eventToRssItem(ev: Event, evPubDate: boolean, lastBuildDate: string, dayFormat: Intl.DateTimeFormat, location: Location, baseUrl: string): string; + export function eventToRssItem(ev: Event, evPubDate: boolean, lastBuildDate: string, dayFormat: Intl.DateTimeFormat, location: Location, baseUrl: string, options?: HebrewCalendar.Options): string; + export function eventToRssItem2(ev: Event, options: HebrewCalendar.Options): string; export function getDownloadFilename(options: HebrewCalendar.Options): string; export function pad2(number: number): string; export function pad4(number: number): string; diff --git a/src/rss.js b/src/rss.js index c513744..39c67ae 100644 --- a/src/rss.js +++ b/src/rss.js @@ -9,9 +9,11 @@ import holidayDescription from './holidays.json'; * @param {boolean} il * @param {string} tzid * @param {string} mainUrl + * @param {string} utmSource + * @param {string} utmMedium * @return {string[]} */ -function getLinkAndGuid(ev, il, tzid, mainUrl) { +function getLinkAndGuid(ev, il, tzid, mainUrl, utmSource, utmMedium) { let link; let guid; const dt = ev.eventTime || ev.getDate().greg(); @@ -22,11 +24,11 @@ function getLinkAndGuid(ev, il, tzid, mainUrl) { const anchor = `${dtAnchor}-${descAnchor}`; const url0 = ev.url(); if (url0) { - link = appendIsraelAndTracking(url0, il, 'shabbat1c', 'rss').replace(/&/g, '&'); + link = appendIsraelAndTracking(url0, il, utmSource, utmMedium).replace(/&/g, '&'); guid = `${url0}#${anchor}`; } else { const url1 = `${mainUrl}&dt=${dtStr}`; - const url = appendIsraelAndTracking(url1, il, 'shabbat1c', 'rss').replace(/&/g, '&'); + const url = appendIsraelAndTracking(url1, il, utmSource, utmMedium).replace(/&/g, '&'); guid = url1.replace(/&/g, '&') + `#${anchor}`; link = `${url}#${anchor}`; } @@ -73,7 +75,7 @@ const localeToLg = { * @return {string} */ export function eventsToRss2(events, options) { - const dayFormat = new Intl.DateTimeFormat('en-US', { + options.dayFormat = new Intl.DateTimeFormat('en-US', { weekday: 'long', day: '2-digit', month: 'long', @@ -81,15 +83,16 @@ export function eventsToRss2(events, options) { }); const location = options.location; const mainUrl = options.mainUrl; - const evPubDate = options.evPubDate; - const buildDate = options.buildDate || new Date(); + const buildDate = options.buildDate = options.buildDate || new Date(); const thisYear = buildDate.getFullYear(); - const lastBuildDate = buildDate.toUTCString(); + const lastBuildDate = options.lastBuildDate = buildDate.toUTCString(); const title = options.title || getCalendarTitle(events, options); const description = options.description || title; + const utmSource = options.utmSource || 'shabbat1c'; + const utmMedium = options.utmMedium || 'rss'; const mainUrlEsc = appendIsraelAndTracking(mainUrl, location && location.getIsrael(), - options.utmSource, options.utmMedium, options.utmCampaign).replace(/&/g, '&'); + utmSource, utmMedium, options.utmCampaign).replace(/&/g, '&'); const selfUrlEsc = options.selfUrl.replace(/&/g, '&'); const lang = options.lang || localeToLg[options.locale] || options.locale || 'en-US'; let str = ` @@ -104,7 +107,7 @@ export function eventsToRss2(events, options) { ${lastBuildDate} `; events.forEach((ev) => { - str += eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl); + str += eventToRssItem2(ev, options); }); str += '\n\n'; return str; @@ -129,6 +132,22 @@ function getPubDate(ev, evPubDate, evDate, lastBuildDate) { return lastBuildDate; } +/** + * @param {Event} ev + * @param {HebrewCalendar.Options} options + * @return {string} + */ +export function eventToRssItem2(ev, options) { + return eventToRssItem( + ev, + options.evPubDate, + options.lastBuildDate, + options.dayFormat, + options.location, + options.mainUrl, + options); +} + /** * @param {Event} ev * @param {boolean} evPubDate @@ -136,15 +155,18 @@ function getPubDate(ev, evPubDate, evDate, lastBuildDate) { * @param {Intl.DateTimeFormat} dayFormat * @param {Location} location * @param {string} mainUrl + * @param {HebrewCalendar.Options} [options] * @return {string} */ -export function eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl) { +export function eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl, options) { let subj = ev.render(); const evDate = ev.getDate().greg(); const pubDate = getPubDate(ev, evPubDate, evDate, lastBuildDate); const il = location ? location.getIsrael() : false; const tzid = location ? location.getTzid() : 'UTC'; - const linkGuid = getLinkAndGuid(ev, il, tzid, mainUrl); + const utmSource = (options && options.utmSource) || 'shabbat1c'; + const utmMedium = (options && options.utmMedium) || 'rss'; + const linkGuid = getLinkAndGuid(ev, il, tzid, mainUrl, utmSource, utmMedium); const link = linkGuid[0]; const guid = linkGuid[1]; const categories = getEventCategories(ev); diff --git a/src/rss.spec.js b/src/rss.spec.js index 021b07e..472235d 100644 --- a/src/rss.spec.js +++ b/src/rss.spec.js @@ -51,6 +51,8 @@ test('eventsToRss2', (t) => { options.mainUrl = 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s'; options.selfUrl = 'https://www.hebcal.com/shabbat?cfg=r&geonameid=4887398&m=50&lg=s&pubDate=1'; options.buildDate = new Date(2021, 11, 15, 12, 34, 56); + options.utmSource = 'foobar'; + options.utmMedium = 'quux'; options.description = 'The quick brown fox'; const rss = eventsToRss2(events, options).split('\n'); const expected = [ @@ -58,7 +60,7 @@ test('eventsToRss2', (t) => { '', '', 'Hebcal Chicago April 1990', - 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&utm_source=undefined&utm_medium=undefined', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&utm_source=foobar&utm_medium=quux', '', 'The quick brown fox', 'en-US', @@ -66,7 +68,7 @@ test('eventsToRss2', (t) => { 'Wed, 15 Dec 2021 20:34:56 GMT', '', 'Candle lighting: 7:03pm', - 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-06&utm_source=shabbat1c&utm_medium=rss#19900406-candle-lighting', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-06&utm_source=foobar&utm_medium=quux#19900406-candle-lighting', 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-06#19900406-candle-lighting', 'Friday, April 06, 1990', 'candles', @@ -76,7 +78,7 @@ test('eventsToRss2', (t) => { '', '', 'Havdalah (50 min): 8:13pm', - 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-07&utm_source=shabbat1c&utm_medium=rss#19900407-havdalah', + 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-07&utm_source=foobar&utm_medium=quux#19900407-havdalah', 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s&dt=1990-04-07#19900407-havdalah', 'Saturday, April 07, 1990', 'havdalah',