分享页: 照片按时间倒序、统一间距、卡片右下角显示日期与城市

This commit is contained in:
gallery
2026-11-15 20:33:21 +08:00
parent 8c376575f5
commit 6bec039595
12 changed files with 213 additions and 8 deletions
+37
View File
@@ -0,0 +1,37 @@
import { mediaCaption } from './mediaCaption'
describe('mediaCaption', () => {
test('parses date and city from a compact filename', () => {
expect(mediaCaption('20251115@DG000075.jpg')).toEqual({
date: '2025/11/15',
city: '东莞',
})
})
test('maps Guangzhou', () => {
expect(mediaCaption('20261115@GZ000120.jpg')).toEqual({
date: '2026/11/15',
city: '广州',
})
})
test('parses a chinese filename', () => {
expect(mediaCaption('2026年11月15日在广州.jpg')).toEqual({
date: '2026/11/15',
city: '广州',
})
})
test('infers city for early TD photos by date', () => {
expect(mediaCaption('20220806@TD000001.jpg')?.city).toBe('珠海')
expect(mediaCaption('20230910@TD000020.jpg')?.city).toBe('拉萨')
expect(mediaCaption('20240616@TD000054.jpg')?.city).toBe('广州')
expect(mediaCaption('20260913@TD000115.jpg')?.city).toBe('北京')
})
test('returns null when no date can be parsed', () => {
expect(mediaCaption('random.jpg')).toBeNull()
expect(mediaCaption(null)).toBeNull()
expect(mediaCaption(undefined)).toBeNull()
})
})
+125
View File
@@ -0,0 +1,125 @@
export type MediaCaption = {
date: string
city: string | null
}
// City abbreviations used in media filenames, e.g. `20251115@GZ000001.jpg`.
// Mirrors the abbreviation table in `photos/_plan.py`.
const CITY_BY_CODE: Record<string, string> = {
GZ: '广州',
FS: '佛山',
DG: '东莞',
SZ: '深圳',
ZS: '中山',
ZH: '珠海',
JM: '江门',
QY: '清远',
ZQ: '肇庆',
SG: '韶关',
ST: '汕头',
ZJ: '湛江',
YC: '盐城',
NJ: '南京',
SH: '上海',
HZ: '杭州',
NB: '宁波',
CD: '成都',
CQ: '重庆',
LS: '拉萨',
CZ: '长治',
TY: '太原',
BJ: '北京',
TJ: '天津',
XA: '西安',
WH: '武汉',
CS: '长沙',
KM: '昆明',
XM: '厦门',
FZ: '福州',
QD: '青岛',
JN: '济南',
ZZ: '郑州',
WLMQ: '乌鲁木齐',
XN: '西宁',
LZ: '兰州',
GY: '贵阳',
NN: '南宁',
HK: '海口',
SY: '三亚',
DL: '大连',
HEB: '哈尔滨',
CC: '长春',
HHHT: '呼和浩特',
}
const CITY_NAMES = Array.from(new Set(Object.values(CITY_BY_CODE)))
function pad(value: number): string {
return value < 10 ? `0${value}` : `${value}`
}
function parseDate(
title: string
): [year: number, month: number, day: number] | null {
const compact = title.match(/^(\d{4})(\d{2})(\d{2})/)
if (compact) {
return [Number(compact[1]), Number(compact[2]), Number(compact[3])]
}
const chinese = title.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/)
if (chinese) {
return [Number(chinese[1]), Number(chinese[2]), Number(chinese[3])]
}
const separated = title.match(/(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})/)
if (separated) {
return [Number(separated[1]), Number(separated[2]), Number(separated[3])]
}
return null
}
// Early photos could not be assigned a city when they were renamed and were all
// tagged as `TD`. Infer their city from the shooting date, matching the
// inference done by `photos/_plan.py`.
function cityForTd(year: number, month: number, day: number): string {
const ymd = year * 10000 + month * 100 + day
if (ymd <= 20221231) return '珠海'
if (ymd <= 20240113) return '拉萨'
if (ymd <= 20260501) return '广州'
return '北京'
}
function parseCity(
title: string,
[year, month, day]: [number, number, number]
): string | null {
// Some original filenames contain the city name directly.
const namedCity = CITY_NAMES.find(name => title.includes(name))
if (namedCity) return namedCity
const codeMatch = title.match(/[@@]([A-Za-z]{2,5})/)
if (!codeMatch) return null
const code = codeMatch[1].toUpperCase()
if (code === 'TD') return cityForTd(year, month, day)
return CITY_BY_CODE[code] ?? null
}
/**
* Build the caption shown on a media card from its filename, e.g.
* `20251115@GZ000075.jpg` becomes `{ date: '2025/11/15', city: '广州' }`.
*/
export function mediaCaption(title?: string | null): MediaCaption | null {
if (!title) return null
const date = parseDate(title)
if (!date) return null
const [year, month, day] = date
return {
date: `${year}/${pad(month)}/${pad(day)}`,
city: parseCity(title, date),
}
}