Files
ipal-kit/dist/modules/richtext/lexicalToMarkdown.js
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

137 lines
4.5 KiB
JavaScript

// 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) {
return Array.isArray(value);
}
function wrapText(text, format) {
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) {
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 ?? {};
const url = typeof fields.url === 'string' ? fields.url : typeof fields.doc?.value === 'string' ? fields.doc.value : '';
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, depth) {
const ordered = node.listType === 'number';
const items = isNodeArray(node.children) ? node.children : [];
const indent = ' '.repeat(depth);
const lines = [];
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) {
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 ?? {};
const url = typeof value.url === 'string' ? value.url : '';
const alt = typeof value.alt === 'string' ? value.alt : '';
return url ? `![${alt}](${url})` : '';
}
default:
// Unknown block-level node: emit its inline content if any.
return node.children ? inline(node.children) : '';
}
}
export function lexicalToMarkdown(data) {
const root = data?.root;
if (!root || !isNodeArray(root.children)) {
return '';
}
const blocks = [];
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();
}
//# sourceMappingURL=lexicalToMarkdown.js.map