Skip to content

Commit

Permalink
Shorter URL redirect format for sedrot
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Oct 6, 2024
1 parent b58731c commit ba184ed
Show file tree
Hide file tree
Showing 10 changed files with 721 additions and 641 deletions.
1,221 changes: 615 additions & 606 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/rest-api",
"version": "5.1.2",
"version": "5.1.3",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal"
Expand Down Expand Up @@ -29,9 +29,9 @@
"url": "https://github.com/hebcal/hebcal-rest-api/issues"
},
"dependencies": {
"@hebcal/core": "^5.5.0",
"@hebcal/leyning": "^8.2.2",
"@hebcal/triennial": "^5.1.2"
"@hebcal/core": "^5.5.1",
"@hebcal/leyning": "^8.2.5",
"@hebcal/triennial": "^5.1.3"
},
"scripts": {
"build:rollup": "rollup -c",
Expand All @@ -44,8 +44,8 @@
},
"license": "BSD-2-Clause",
"devDependencies": {
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@babel/preset-env": "^7.25.7",
"@babel/preset-typescript": "^7.25.7",
"@hebcal/learning": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-json": "^6.1.0",
Expand All @@ -55,9 +55,9 @@
"@types/jest": "^29.5.13",
"gts": "^5.3.1",
"jest": "^29.7.0",
"rollup": "^4.22.4",
"rollup": "^4.24.0",
"ts-jest": "^29.2.5",
"typedoc": "^0.26.7",
"typedoc": "^0.26.8",
"typescript": "^5.6.2"
}
}
19 changes: 3 additions & 16 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { isoDateString } from '@hebcal/hdate';
import { getLeyningForHoliday, getLeyningForParshaHaShavua } from '@hebcal/leyning';
import countryNames0 from './countryNames.json';
import holidayDescription0 from './holidays.json';
import { shortenSedrotUrl } from './shorten';
import { makeAnchor } from './makeAnchor';

export interface StringMap {
[key: string]: string;
Expand Down Expand Up @@ -84,21 +86,6 @@ export function locationToPlainObj(location: Location | undefined): LocationPlai
}
}

/**
* Helper function to transform a string to make it more usable in a URL or filename.
* Converts to lowercase and replaces non-word characters with hyphen ('-').
* @example
* makeAnchor('Rosh Chodesh Adar II') // 'rosh-chodesh-adar-ii'
*/
export function makeAnchor(s: string): string {
return s.toLowerCase()
.replace(/'/g, '')
.replace(/[^\w]/g, '-')
.replace(/-+/g, '-')
.replace(/^-/g, '')
.replace(/-$/g, '');
}

export function getDownloadFilename(options: RestApiOptions): string {
let fileName = 'hebcal';
if (options.year) {
Expand Down Expand Up @@ -296,7 +283,7 @@ export function appendIsraelAndTracking(url: string, il: boolean,
if (isHolidays) {
u.pathname = '/h/' + path.substring(10);
} else if (isSedrot) {
u.pathname = '/s/' + path.substring(8);
shortenSedrotUrl(u);
} else { // isOmer
u.pathname = '/o/' + path.substring(6);
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './csv';
export * from './makeAnchor';
export * from './common';
export * from './classic-rest-api';
export * from './rss';
Expand Down
14 changes: 14 additions & 0 deletions src/makeAnchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Helper function to transform a string to make it more usable in a URL or filename.
* Converts to lowercase and replaces non-word characters with hyphen ('-').
* @example
* makeAnchor('Rosh Chodesh Adar II') // 'rosh-chodesh-adar-ii'
*/
export function makeAnchor(s: string): string {
return s.toLowerCase()
.replace(/'/g, '')
.replace(/[^\w]/g, '-')
.replace(/-+/g, '-')
.replace(/^-/g, '')
.replace(/-$/g, '');
}
3 changes: 2 additions & 1 deletion src/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
RestApiOptions,
StringMap,
appendIsraelAndTracking, getCalendarTitle,
getEventCategories, makeAnchor, makeMemo
getEventCategories, makeMemo
} from './common';
import { makeAnchor } from './makeAnchor';

function getLinkAndGuid(ev: Event, il: boolean, tzid: string, mainUrl: string, utmSource: string, utmMedium: string): string[] {
let link;
Expand Down
66 changes: 66 additions & 0 deletions src/shorten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { HDate, parshiot } from "@hebcal/core";
import { makeAnchor } from './makeAnchor';

const parsha2id = new Map<string, number>();
for (let id = 0; id < parshiot.length; id++) {
const name = parshiot[id];
parsha2id.set(makeAnchor(name), id + 1);
}

const doubledParshiyot: string[] = [
'Vayakhel-Pekudei',
'Tazria-Metzora',
'Achrei Mot-Kedoshim',
'Behar-Bechukotai',
'Chukat-Balak',
'Matot-Masei',
'Nitzavim-Vayeilech',
] as const;

const doubled = new Set<string>();
for (const name of doubledParshiyot) {
const anchor = makeAnchor(name);
doubled.add(anchor);
const [p1, p2] = anchor.split('-');
const id = parsha2id.get(p1);
parsha2id.set(anchor, id!);
}

function parse8digitDateStr(date: string): Date {
const gy = date.substring(0, 4);
const gm = date.substring(4, 6);
const gd = date.substring(6, 8);
const yy = parseInt(gy, 10);
const mm = parseInt(gm, 10);
const dd = parseInt(gd, 10);
const dt = new Date(yy, mm - 1, dd);
if (yy < 100) {
dt.setFullYear(yy);
}
return dt;
}

export function shortenSedrotUrl(u: URL) {
const path = u.pathname;
const dash = path.lastIndexOf('-');
const dateStr = path.substring(dash + 1);
const name = path.substring(8, dash);
const id = parsha2id.get(name);
if (id && dateStr.length === 8) {
const dt = parse8digitDateStr(dateStr);
const hd = new HDate(dt);
let p = '/s/' + hd.getFullYear();
if (u.searchParams.get('i') === 'on') {
p += 'i';
u.searchParams.delete('i');
}
const id = parsha2id.get(name);
p += '/' + id;
if (doubled.has(name)) {
p += 'd';
}
u.pathname = p;
} else {
u.pathname = '/s/' + path.substring(8);
}
}
4 changes: 2 additions & 2 deletions test/classic-rest-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('eventToClassicApiObject', () => {
'maftir': 'Numbers 1:52-1:54',
},
},
link: 'https://hebcal.com/s/bamidbar-20200523?us=js&um=api',
link: 'https://hebcal.com/s/5780/34?us=js&um=api',
};
expect(bamidbarActual).toEqual(bamidbarExpected);
const shavuotActual = apiObjs[7];
Expand Down Expand Up @@ -259,7 +259,7 @@ test('no-leyning', () => {
hdate: '29 Iyyar 5780',
category: 'parashat',
hebrew: 'פרשת במדבר',
link: 'https://hebcal.com/s/bamidbar-20200523?i=on&us=js&um=api',
link: 'https://hebcal.com/s/5780i/34?us=js&um=api',
};
expect(bamidbarActual).toEqual(bamidbarExpected);
});
Expand Down
14 changes: 8 additions & 6 deletions test/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ test('appendIsraelAndTracking', () => {
.toBe('https://www.hebcal.com/foo?i=on&utm_source=foo&utm_medium=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/foo', false, 'foo', 'bar'))
.toBe('https://www.hebcal.com/foo?utm_source=foo&utm_medium=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/foo-123', true, 'foo', 'bar'))
.toBe('https://hebcal.com/s/foo-123?i=on&us=foo&um=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/foo-345', false, 'foo', 'bar'))
.toBe('https://hebcal.com/s/foo-345?us=foo&um=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/foo-345', false, 'foo', 'bar', 'hello'))
.toBe('https://hebcal.com/s/foo-345?us=foo&um=bar&uc=hello');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/bamidbar-20250531', true, 'foo', 'bar'))
.toBe('https://hebcal.com/s/5785i/34?us=foo&um=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/lech-lecha-19991023', false, 'foo', 'bar'))
.toBe('https://hebcal.com/s/5760/3?us=foo&um=bar');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/lech-lecha-19991023', false, 'foo', 'bar', 'hello'))
.toBe('https://hebcal.com/s/5760/3?us=foo&um=bar&uc=hello');
expect(appendIsraelAndTracking('https://www.hebcal.com/sedrot/vayakhel-pekudei-20340318', false, undefined, undefined, 'ical-abc'))
.toBe('https://hebcal.com/s/5794/22d?uc=ical-abc');
expect(appendIsraelAndTracking('https://www.hebcal.com/holidays/quux-678', true, 'foo', 'bar', 'ical-abc'))
.toBe('https://hebcal.com/h/quux-678?i=on&uc=ical-abc');
expect(appendIsraelAndTracking('https://www.hebcal.com/holidays/quux-987', false, 'foo', 'bar', 'pdf-abc'))
Expand Down
4 changes: 2 additions & 2 deletions test/rss.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test('parsha', () => {
const item = eventToRssItem2(events[0], options);
const expected = '<item>\n' +
'<title>Parashat Vayetzei</title>\n' +
'<link>https://hebcal.com/s/vayetzei-20201128?us=shabbat1c&amp;um=rss</link>\n' +
'<link>https://hebcal.com/s/5781/7?us=shabbat1c&amp;um=rss</link>\n' +
'<guid isPermaLink="false">https://www.hebcal.com/sedrot/vayetzei-20201128#20201128-parashat-vayetzei</guid>\n' +
'<description>Torah: Genesis 28:10-32:3\n' +
'Haftarah: Hosea 12:13-14:10\n' +
Expand Down Expand Up @@ -161,7 +161,7 @@ test('parsha-il', () => {
const item = eventToRssItem2(events[0], options);
const expected = '<item>\n' +
'<title>Parashat Vayetzei</title>\n' +
'<link>https://hebcal.com/s/vayetzei-20201128?i=on&amp;us=shabbat1c&amp;um=rss</link>\n' +
'<link>https://hebcal.com/s/5781i/7?us=shabbat1c&amp;um=rss</link>\n' +
'<guid isPermaLink="false">https://www.hebcal.com/sedrot/vayetzei-20201128#20201128-parashat-vayetzei</guid>\n' +
'<description>Torah: Genesis 28:10-32:3\n' +
'Haftarah: Hosea 12:13-14:10\n' +
Expand Down

0 comments on commit ba184ed

Please sign in to comment.