Files
ipal-kit/src/modules/seo/formatOpeningHours.ts
T
radoslaw.smolinskiandClaude Opus 5.5 c9a50abc33 SEO/GEO tools and project requests: site index, IndexNow, page JSON-LD, fixes
GEO / SEO
- collectSiteIndex: one list of public docs per locale (pages + archive
  entries) on sitemap rules; llms.txt/llms-full and MCP list_pages now include
  blog/content entries and every language. Fixes hard-coded "## Strony" heading
  and homepage not collapsing to the locale root in llms.txt.
- Markdown frontmatter: canonical url + updated date.
- Metadata: X/Twitter card, OG image width/height/alt, og:type article with
  published/modified times for entries.
- Fix: Site Settings "defaultShareImage" was never used — now the OG fallback.
- getPageJsonLd (createContentHelpers): WebPage/CollectionPage/BlogPosting,
  BreadcrumbList from the real route, FAQPage via a project mapper.
- IndexNow (opt-in ipalKit({ indexNow: true })): submit published URLs in all
  languages, key generated in Site Integrations or INDEXNOW_KEY, key route.
- sameAs profiles in Site Settings + sameAsUrls(); sitemap <image:image>.

From project reports
- Fix: package exports were missing ./env and ./fluid.css although the docs
  import them (TS2307); regression test checks every documented subpath.
- Fix: plugins run before Payload adds its default users collection, so roles
  and 2FA had nothing to attach to — ipalKit injects an auth collection when
  the project has none (project's own one wins). Audit no longer errors on it.
- createMarkdownRoute, createSubmitAction (/server, headers injected).
- formatOpeningHours (PL/EN/DE text → schema.org), used by
  buildLocalBusinessJsonLd, which also takes a schema.org `type`.
- buildNavigationJsonLd straight from the header global.
- buildCsp googleMaps: full Maps JS API domain set; values de-duplicated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 02:19:08 +02:00

113 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Turns opening hours as an editor types them into schema.org `openingHours`
* strings, so the panel field can stay human text in the site's language:
*
* formatOpeningHours('Pn-Pt: 08:00 - 17:00, Sob: 8-14, Nd: nieczynne')
* // → ['Mo-Fr 08:00-17:00', 'Sa 08:00-14:00']
* formatOpeningHours('Mon, Wed, Fri 9:30–17') // → ['Mo,We,Fr 09:30-17:00']
* formatOpeningHours('Mo–Fr 8–16 Uhr; Sa 9–12') // → ['Mo-Fr 08:00-16:00', 'Sa 09:00-12:00']
* formatOpeningHours('całodobowo') // → ['Mo-Su 00:00-23:59']
*
* Understands Polish, English and German day names and abbreviations (with or
* without diacritics and dots), ranges (`-`, `–`, `do`, `to`, `bis`), lists,
* `8`, `8:00`, `8.00`, closed days (skipped) and "24/7". Strings already in
* schema.org form pass through. Accepts one string or an array of lines. Anything
* it can't read is dropped rather than emitted invalid — check the result.
*/
type Day = 'Fr' | 'Mo' | 'Sa' | 'Su' | 'Th' | 'Tu' | 'We'
// Normalized (lowercase, no diacritics, no dots) day words → schema.org day.
// "so" is German Sonntag; Polish Saturday is "sob"/"sb"/"sobota".
const DAYS: Record<string, Day> = {
// pl
cz: 'Th', czw: 'Th', czwartek: 'Th', nd: 'Su', ndz: 'Su', niedz: 'Su', niedziela: 'Su', piat: 'Fr', piatek: 'Fr',
pn: 'Mo', pon: 'Mo', poniedzialek: 'Mo', pt: 'Fr', sb: 'Sa', sob: 'Sa', sobota: 'Sa', sr: 'We', sro: 'We',
sroda: 'We', wt: 'Tu', wto: 'Tu', wtorek: 'Tu',
// en
fr: 'Fr', fri: 'Fr', friday: 'Fr', mo: 'Mo', mon: 'Mo', monday: 'Mo', sa: 'Sa', sat: 'Sa', saturday: 'Sa', su: 'Su',
sun: 'Su', sunday: 'Su', th: 'Th', thu: 'Th', thur: 'Th', thurs: 'Th', thursday: 'Th', tu: 'Tu',
tue: 'Tu', tues: 'Tu', tuesday: 'Tu', we: 'We', wed: 'We', wednesday: 'We',
// de
di: 'Tu', dienstag: 'Tu', do: 'Th', donnerstag: 'Th', freitag: 'Fr', mi: 'We', mittwoch: 'We', montag: 'Mo',
samstag: 'Sa', so: 'Su', sonntag: 'Su',
}
const CLOSED = /\b(?:nieczynne|nieczynny|zamkniete|zamkniety|closed|geschlossen|ruhetag)\b/g
const ALWAYS = /\b(?:calodobowo|cala doba|cala dobe|24\s*\/\s*7|24h|non-?stop|rund um die uhr|open 24 hours)\b/
const TIME = /(?<fromH>\d{1,2})(?:[:.](?<fromM>\d{2}))?\s*(?:-|do|to|bis)\s*(?<toH>\d{1,2})(?:[:.](?<toM>\d{2}))?/g
const SCHEMA = /^(?:Mo|Tu|We|Th|Fr|Sa|Su)(?:[-,](?:Mo|Tu|We|Th|Fr|Sa|Su))*(?:\s+\d{2}:\d{2}-\d{2}:\d{2})?$/
function normalize(text: string): string {
return text
.toLowerCase()
.normalize('NFD')
.replace(/\p{M}/gu, '') // drop combining accents (ą → a)
.replace(/ł/g, 'l')
.replace(/[–—−]/g, '-')
.replace(/\./g, (m, i: number, s: string) => (/\d/.test(s[i - 1] ?? '') && /\d/.test(s[i + 1] ?? '') ? m : ' '))
.replace(/\buhr\b/g, ' ')
}
function hhmm(h: string, m?: string): null | string {
const hour = Number(h)
const minute = m ? Number(m) : 0
if (hour > 24 || minute > 59) {return null}
return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
}
/** Day words in a fragment → "Mo-Fr" (range) or "Mo,We,Fr" (list). */
function parseDays(fragment: string): null | string {
const days: Day[] = []
let range = false
for (const w of fragment.match(/[a-z]+|-/g) ?? []) {
// "do" is both Polish "to" and German Donnerstag: a range word only right after the first day.
const rangeWord = w === '-' || w === 'to' || w === 'bis' || (w === 'do' && days.length === 1)
if (rangeWord) {
if (days.length === 1) {range = true}
continue
}
const day = DAYS[w]
if (day) {days.push(day)}
}
if (days.length === 0) {return null}
if (range && days.length === 2) {return `${days[0]}-${days[1]}`}
return [...new Set(days)].join(',')
}
export function formatOpeningHours(input: null | readonly string[] | string | undefined): string[] {
if (!input) {return []}
const lines = (Array.isArray(input) ? input : [input as string]).map((l) => l.trim()).filter(Boolean)
if (lines.every((l) => SCHEMA.test(l))) {return [...lines]}
const out: string[] = []
for (const raw of lines) {
if (SCHEMA.test(raw)) {
out.push(raw)
continue
}
const text = normalize(raw)
if (ALWAYS.test(text)) {
out.push('Mo-Su 00:00-23:59')
continue
}
let last = 0
for (const m of text.matchAll(TIME)) {
// The day spec is what precedes this time range — after the previous
// range, the last line break / semicolon, and any "closed" day.
let spec = text.slice(last, m.index)
last = (m.index ?? 0) + m[0].length
spec = spec.split(/[;\n]/).pop() ?? ''
const closedAt = [...spec.matchAll(CLOSED)].pop()
if (closedAt) {spec = spec.slice((closedAt.index ?? 0) + closedAt[0].length)}
const days = parseDays(spec)
const g = m.groups ?? {}
const from = hhmm(g.fromH, g.fromM)
const to = hhmm(g.toH, g.toM)
if (!days || !from || !to) {continue}
out.push(`${days} ${from}-${to === '24:00' ? '23:59' : to}`)
}
}
return out
}