summaryrefslogtreecommitdiffhomepage
path: root/src/pages/kodaira/events/data.json.ts
blob: 67bda610307e314d7abdf8575d04473188a73800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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' },
  });