import React from 'react'; import type { PhrasingContent } from 'mdast'; import styles from '@san-siva/stylekit/styles/index.module.scss'; function escapeHtml(text: string): string { return text .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>'); } function toHtmlString(nodes: PhrasingContent[]): string { return nodes .map(node => { switch (node.type) { case 'text': { return escapeHtml(node.value); } case 'html': { return node.value; } case 'strong': { return `${toHtmlString(node.children)}`; } case 'emphasis': { return `${toHtmlString(node.children)}`; } case 'inlineCode': { return `${escapeHtml(node.value)}`; } case 'link': { return `${toHtmlString(node.children)}`; } case 'break': { return '
'; } case 'image': { return `${escapeHtml(node.alt ?? '')}`; } default: { return ''; } } }) .join(''); } export function renderPhrasingContent( nodes: PhrasingContent[] ): React.ReactNode { if (nodes.some(node => node.type === 'html')) { return ; } return nodes.map((node, index) => { switch (node.type) { case 'text': { return node.value; } case 'strong': { return ( {renderPhrasingContent(node.children)} ); } case 'emphasis': { return {renderPhrasingContent(node.children)}; } case 'inlineCode': { return {node.value}; } case 'link': { return ( {renderPhrasingContent(node.children)} ); } case 'break': { return
; } case 'image': { return ( {node.alt ); } default: { return null; } } }); }