Files
ipal-kit/src/modules/content/markdownNegotiation.ts
T
radoslaw.smolinski 09925e4cfd feat: v1.7.0 — AI-readability suite, form engine & boilerplate helpers
AI / GEO:
- feat(content): lexicalToMarkdown, blocksToMarkdown, createMarkdownRenderer
  (+markdownResponse) — per-page Markdown for AI agents
- feat(content): markdown content negotiation + discovery (prefersMarkdown, markdownHref)
- feat(seo): buildRobots aiBots policy (allow/block-training/disallow)
- feat(seo): llms-full.txt mode in buildLlmsTxt/createLlmsTxtRoute
- feat(mcp): createMcpRoute — read-only MCP server (search_content, get_page)
- feat(content): searchContent + createSearchRoute — shared JSON search
- feat(seo): buildSpeakableJsonLd + buildSummaryField (aiSummary, auto-fill)

Forms:
- feat(forms): useForm/useContactForm + validators (logic-only, no markup)
- feat(forms): honeypot anti-spam (honeypotInputProps, isHoneypotTripped)

Helpers / hardening:
- feat(i18n): resolveLocalized — replaces ad-hoc getSafeString
- feat(hooks): buildCrossLocaleFieldSync — locales from config, no hardcoded list
- feat(seo): buildNoindexField — checkbox → meta.noindex, no
- feat(content): resolveRedirect — 301/308 from slugHistory
- feat(env): assertEnv + ipalEnvRules — fail-fast env validation
- feat(consent): ConsentScript — load third-party only after consent
- feat(health): createHealthRoute — Coolify/Docker DB liveness

Fixes:
- fix(seo): wrap getCachedPayload in try/catch in createManifestHandler
  (Docker/CI build no longer crashes when DB unreachable)

docs: migration.md (20 sections), new env.md/mcp.md, updated agents.md,
AGENTS-TEMPLATE.md, forms/hooks/content/seo/consent/deployment/richtext
2026-10-10 16:52:16 +02:00

67 lines
2.8 KiB
TypeScript

/**
* Helpers for making the per-page Markdown (createMarkdownRenderer) discoverable
* and negotiable, so AI agents reliably find and read it.
*
* Two mechanisms:
* - `markdownHref` — the `<link rel="alternate" type="text/markdown">` href a
* page advertises, so an agent fetching the HTML sees there's a Markdown
* version and can switch to it.
* - `prefersMarkdown` — reads the Accept header, so the same URL can serve
* Markdown when asked (content negotiation via a proxy rewrite).
*/
/** Default URL prefix the Markdown route is mounted at (see createMarkdownRenderer). */
export const MARKDOWN_PREFIX = '/md'
/**
* The Markdown URL for a given page path. Keeps the `/md` prefix in ONE place so
* the discovery link and the route agree.
*
* markdownHref('/pl/o-nas') // '/md/pl/o-nas'
* markdownHref('/pl/o-nas', '/ai') // '/ai/pl/o-nas'
*
* Advertise it from Next metadata so every page's <head> points to its Markdown:
*
* // generateMetadata
* return { ...meta, alternates: { ...meta.alternates, types: { 'text/markdown': markdownHref(path) } } }
*/
export function markdownHref(pathname: string, prefix: string = MARKDOWN_PREFIX): string {
const clean = pathname.startsWith('/') ? pathname : `/${pathname}`
const p = prefix.replace(/\/$/, '')
return `${p}${clean === '/' ? '' : clean}` || p
}
/**
* True when the client asked for Markdown via the Accept header (q-values
* respected: a higher-weighted text/html wins). Accepts a header string, a
* Headers object, or a Request. Use it in proxy.ts to serve the same URL as
* Markdown when an agent negotiates for it:
*
* // proxy.ts
* if (prefersMarkdown(request)) {
* return NextResponse.rewrite(new URL(markdownHref(request.nextUrl.pathname), request.url))
* }
*/
export function prefersMarkdown(input: Headers | null | Request | string | undefined): boolean {
let accept: null | string = null
if (!input) {return false}
if (typeof input === 'string') {accept = input}
else if (input instanceof Headers) {accept = input.get('accept')}
else if ('headers' in input && input.headers) {accept = input.headers.get('accept')}
if (!accept) {return false}
// Parse "type/subtype;q=0.8" entries into {type, q}.
const entries = accept.split(',').map((part) => {
const [type, ...params] = part.trim().split(';')
const qParam = params.find((p) => p.trim().startsWith('q='))
const q = qParam ? Number(qParam.split('=')[1]) : 1
return { type: type.trim().toLowerCase(), q: Number.isFinite(q) ? q : 1 }
})
const md = entries.find((e) => e.type === 'text/markdown')
if (!md) {return false}
// Markdown must be at least as preferred as html (and html not strictly higher).
const html = entries.find((e) => e.type === 'text/html')
return !html || md.q >= html.q
}