diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-15 22:01:29 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-15 22:01:29 +0900 |
| commit | dea594c6dda0f84ee3ebf6982ea1c98ff760bf71 (patch) | |
| tree | f335463efbd05962e80a07ee6ea3497ea7c2a849 /src/utils | |
| parent | 2d287d60868a5059ad3ba3938ef0972ead0ff066 (diff) | |
SocialShare: X投稿のキャプションをページごとに生成
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/share-caption.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/utils/share-caption.ts b/src/utils/share-caption.ts new file mode 100644 index 0000000..73315cc --- /dev/null +++ b/src/utils/share-caption.ts @@ -0,0 +1,37 @@ +/** + * ページのスラッグとタイトルから、X などで共有するときのキャプションを組み立てる。 + * + * 一般質問ページは、どの年度・月定例会のものかがキャプションだけで分かるよう、 + * スラッグ(例: ippan-situmon/r8d/9gatu)から文言を自動生成する。 + * それ以外のページはフロントマターの title をそのまま使う。 + */ +const REIWA_RE = /^r(\d+)d$/; +const MONTH_RE = /^(\d+)gatu$/; + +function reiwaYear(n: string): string { + return `令和${n === "1" ? "元" : n}年`; +} + +export function shareCaption(slug: string, title?: string): string { + const parts = slug.split("/").filter(Boolean); + + // トップページは「小平市議 安竹洋平」のみにする + if (parts.length === 0) return ""; + + if (parts[0] === "ippan-situmon") { + if (parts.length === 1) return "安竹洋平の一般質問一覧"; + + const year = parts[1]?.match(REIWA_RE); + if (!year) return title ?? ""; + if (parts.length === 2) return `${reiwaYear(year[1])}度の一般質問一覧`; + + const month = parts[2]?.match(MONTH_RE); + if (!month) return title ?? ""; + const session = `${reiwaYear(year[1])}${month[1]}月定例会`; + + if (parts.length === 3) return `${session}一般質問一覧`; + return title ? `${session}一般質問${title}` : `${session}一般質問`; + } + + return title ?? ""; +} |
