import { CMS_SITE_ORIGIN } from '../constants'
// Define minimal block structure for compatibility
type CMSBlock = {
blockType: string
[key: string]: any
}
/**
* Enhanced convert function that handles both text nodes and custom blocks
* Converts CMS blocks into existing www markdown syntax
*/
export function convertRichTextToMarkdownWithBlocks(content: any): {
markdown: string
blocks: CMSBlock[]
} {
if (!content?.root?.children) return { markdown: '', blocks: [] }
const blocks: CMSBlock[] = []
const markdown = content.root.children
.map((node: any) => {
// Handle basic text nodes
if (node.type === 'heading') {
const level = node.tag && typeof node.tag === 'string' ? node.tag.replace('h', '') : '1'
const text = node.children?.map((child: any) => child.text).join('') || ''
return `${'#'.repeat(Number(level))} ${text}`
}
if (node.type === 'paragraph') {
return node.children?.map((child: any) => child.text).join('') || ''
}
if (node.type === 'list') {
const items = node.children
?.map((item: any) => {
if (item.type === 'list-item') {
return `- ${item.children?.map((child: any) => child.text).join('') || ''}`
}
return ''
})
.filter(Boolean)
.join('\n')
return items
}
if (node.type === 'link') {
const text = node.children?.map((child: any) => child.text).join('') || ''
const url = node.url || ''
return `[${text}](${url})`
}
// Handle custom blocks - convert to existing www markdown syntax
if (node.type === 'block') {
const blockType = node.fields?.blockType
switch (blockType) {
case 'banner': {
// Convert banner to Admonition
const style = node.fields.style || 'info'
const content = extractTextFromField(node.fields.content)
const typeMap = {
info: 'note',
warning: 'warning',
error: 'destructive',
success: 'note',
} as const
return ``
} else {
imgComponent = `
`
}
return imgComponent
}
return ''
}
case 'code': {
// Convert code block to markdown code block
const language = node.fields.language || 'text'
const code = node.fields.code || ''
return `\`\`\`${language}\n${code}\n\`\`\``
}
case 'quote': {
// Convert quote to Quote component
const text = extractTextFromField(node.fields.text)
const caption = extractTextFromField(node.fields.caption)
const img = node.fields.img
let imgProp = ''
if (typeof img === 'object' && img?.url) {
const imageUrl = `${CMS_SITE_ORIGIN}${img.url}`
imgProp = ` img="${imageUrl}"`
}
const captionProp = caption ? ` caption="${caption}"` : ''
return `
\n${text}\n
`
}
case 'youtube': {
// Convert YouTube to video-container div with iframe
const youtubeId = node.fields.youtubeId || ''
return `
${child.text}`
return child.text
})
.join('') || ''
return `${text}
` } if (node.type === 'heading') { const level = node.tag && typeof node.tag === 'string' ? node.tag.replace('h', '') : '2' const text = node.children?.map((child: any) => child.text).join('') || '' return `