summaryrefslogtreecommitdiffhomepage
path: root/src/pages/kodaira/events/data.json.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages/kodaira/events/data.json.ts')
-rw-r--r--src/pages/kodaira/events/data.json.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/pages/kodaira/events/data.json.ts b/src/pages/kodaira/events/data.json.ts
new file mode 100644
index 0000000..67bda61
--- /dev/null
+++ b/src/pages/kodaira/events/data.json.ts
@@ -0,0 +1,57 @@
+// 「今日の予定」アプリ用のデータ(/kodaira/events/data.json)。
+// 日付の解釈はビルド時に済ませ、端末側は今日・明日の日付で絞り込むだけにする。
+import type { APIRoute } from 'astro';
+import events from '@/data/schedule/events.json';
+import meetings from '@/data/schedule/meetings.json';
+import { entryDates } from '@/lib/schedule-dates';
+
+type Row = { date: string; title: string; place: string; note: string };
+
+const strip = (value: string) =>
+ String(value ?? '')
+ .replace(/<[^>]*>/g, '')
+ .replace(/&nbsp;/g, ' ')
+ .replace(/&amp;/g, '&')
+ .replace(/&lt;/g, '<')
+ .replace(/&gt;/g, '>')
+ .replace(/&quot;/g, '"')
+ .replace(/&#39;/g, "'")
+ .replace(/\s+/g, ' ')
+ .trim();
+
+const pad = (n: number) => String(n).padStart(2, '0');
+const toKey = (d: Date) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
+
+const now = new Date();
+const from = new Date(now);
+from.setDate(from.getDate() - 31);
+const to = new Date(now);
+to.setDate(to.getDate() + 400);
+const fromKey = toKey(from);
+const toKeyStr = toKey(to);
+
+const collect = (rows: Row[], type: 'event' | 'meeting') =>
+ rows
+ .map((row) => {
+ const dates = entryDates(type, row.date, row.place).filter(
+ (d) => d >= fromKey && d <= toKeyStr,
+ );
+ if (dates.length === 0) return null;
+ return {
+ t: strip(row.title),
+ p: strip(row.place),
+ n: strip(row.note).slice(0, 140),
+ dates,
+ };
+ })
+ .filter(Boolean);
+
+const data = {
+ generated: now.toISOString(),
+ rows: [...collect(events.rows as Row[], 'event'), ...collect(meetings.rows as Row[], 'meeting')],
+};
+
+export const GET: APIRoute = () =>
+ new Response(JSON.stringify(data), {
+ headers: { 'Content-Type': 'application/json; charset=utf-8' },
+ });