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
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export { lexicalToMarkdown } from './lexicalToMarkdown.js'
|
||||
export { RenderRichText } from './RenderRichText.js'
|
||||
export type { RenderRichTextProps } from './RenderRichText.js'
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
|
||||
|
||||
/**
|
||||
* Converts a Payload Lexical richText value to Markdown — a small, dependency-
|
||||
* free walker over the serialized node tree. Self-contained on purpose: it does
|
||||
* NOT rely on a specific `@payloadcms/richtext-lexical` markdown export (those
|
||||
* move between versions), so a Payload bump can't break it. Covers the node
|
||||
* types editors actually produce; unknown nodes degrade to their text/children
|
||||
* rather than throwing.
|
||||
*
|
||||
* Used for AI-readable output (per-page Markdown, llms-full.txt) — give an agent
|
||||
* clean Markdown instead of making it parse HTML. For on-page rendering use
|
||||
* RenderRichText (the official React converter); this is the text-only sibling.
|
||||
*/
|
||||
|
||||
type LexNode = { children?: unknown; type?: string } & Record<string, unknown>
|
||||
|
||||
// Lexical text format bitflags.
|
||||
const IS_BOLD = 1
|
||||
const IS_ITALIC = 1 << 1
|
||||
const IS_STRIKETHROUGH = 1 << 2
|
||||
const IS_CODE = 1 << 4
|
||||
|
||||
function isNodeArray(value: unknown): value is LexNode[] {
|
||||
return Array.isArray(value)
|
||||
}
|
||||
|
||||
function wrapText(text: string, format: number): string {
|
||||
if (!text) {return text}
|
||||
// Don't wrap pure whitespace — would produce "** **".
|
||||
if (!text.trim()) {return text}
|
||||
let out = text
|
||||
if (format & IS_CODE) {out = `\`${out}\``}
|
||||
if (format & IS_BOLD) {out = `**${out}**`}
|
||||
if (format & IS_ITALIC) {out = `_${out}_`}
|
||||
if (format & IS_STRIKETHROUGH) {out = `~~${out}~~`}
|
||||
return out
|
||||
}
|
||||
|
||||
/** Render inline children (text, links, line breaks) to a single string. */
|
||||
function inline(nodes: unknown): string {
|
||||
if (!isNodeArray(nodes)) {return ''}
|
||||
let out = ''
|
||||
for (const node of nodes) {
|
||||
if (!node || typeof node !== 'object') {continue}
|
||||
const type = node.type
|
||||
|
||||
if (type === 'text') {
|
||||
const text = typeof node.text === 'string' ? node.text : ''
|
||||
const format = typeof node.format === 'number' ? node.format : 0
|
||||
out += wrapText(text, format)
|
||||
} else if (type === 'linebreak') {
|
||||
out += '\n'
|
||||
} else if (type === 'link') {
|
||||
const fields = (node.fields ?? {}) as Record<string, unknown>
|
||||
const url =
|
||||
typeof fields.url === 'string'
|
||||
? fields.url
|
||||
: typeof (fields.doc as Record<string, unknown>)?.value === 'string'
|
||||
? ((fields.doc as Record<string, unknown>).value as string)
|
||||
: ''
|
||||
const label = inline(node.children)
|
||||
out += url ? `[${label}](${url})` : label
|
||||
} else if (node.children) {
|
||||
out += inline(node.children)
|
||||
} else if (typeof node.text === 'string') {
|
||||
out += node.text
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** Render a list (ordered/unordered), handling nesting via indentation. */
|
||||
function renderList(node: LexNode, depth: number): string {
|
||||
const ordered = node.listType === 'number'
|
||||
const items = isNodeArray(node.children) ? node.children : []
|
||||
const indent = ' '.repeat(depth)
|
||||
const lines: string[] = []
|
||||
let i = 1
|
||||
for (const item of items) {
|
||||
if (!item || typeof item !== 'object') {continue}
|
||||
const marker = ordered ? `${i}.` : '-'
|
||||
// A listitem may contain inline content and/or a nested list.
|
||||
const kids = isNodeArray(item.children) ? item.children : []
|
||||
const nested = kids.filter((k) => k && typeof k === 'object' && k.type === 'list')
|
||||
const inlineKids = kids.filter((k) => !(k && typeof k === 'object' && k.type === 'list'))
|
||||
const text = inline(inlineKids)
|
||||
if (text.trim()) {
|
||||
lines.push(`${indent}${marker} ${text}`)
|
||||
i++
|
||||
}
|
||||
for (const n of nested) {lines.push(renderList(n, depth + 1))}
|
||||
}
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
/** Render a block-level node to Markdown. */
|
||||
function block(node: LexNode): string {
|
||||
const type = node.type
|
||||
|
||||
switch (type) {
|
||||
case 'heading': {
|
||||
const level = typeof node.tag === 'string' ? Number(node.tag.replace('h', '')) || 2 : 2
|
||||
return `${'#'.repeat(Math.min(level, 6))} ${inline(node.children)}`
|
||||
}
|
||||
case 'horizontalrule':
|
||||
return '---'
|
||||
case 'list':
|
||||
return renderList(node, 0)
|
||||
case 'paragraph': {
|
||||
const text = inline(node.children)
|
||||
return text.trim() ? text : ''
|
||||
}
|
||||
case 'quote':
|
||||
return inline(node.children)
|
||||
.split('\n')
|
||||
.map((l) => `> ${l}`)
|
||||
.join('\n')
|
||||
case 'upload': {
|
||||
const value = (node.value ?? {}) as Record<string, unknown>
|
||||
const url = typeof value.url === 'string' ? value.url : ''
|
||||
const alt = typeof value.alt === 'string' ? value.alt : ''
|
||||
return url ? `` : ''
|
||||
}
|
||||
default:
|
||||
// Unknown block-level node: emit its inline content if any.
|
||||
return node.children ? inline(node.children) : ''
|
||||
}
|
||||
}
|
||||
|
||||
export function lexicalToMarkdown(data: null | SerializedEditorState | undefined): string {
|
||||
const root = (data as unknown as { root?: LexNode } | null | undefined)?.root
|
||||
if (!root || !isNodeArray(root.children)) {return ''}
|
||||
|
||||
const blocks: string[] = []
|
||||
for (const node of root.children) {
|
||||
if (!node || typeof node !== 'object') {continue}
|
||||
const md = block(node)
|
||||
if (md.trim()) {blocks.push(md)}
|
||||
}
|
||||
// Blank line between block-level elements — standard Markdown separation.
|
||||
return blocks.join('\n\n').trim()
|
||||
}
|
||||
Reference in New Issue
Block a user