"use client"; import { useEffect, useRef } from "react"; import Reveal from 'reveal.js'; import 'reveal.js/dist/reveal.css'; import 'reveal.js/dist/theme/white.css'; import { marked } from 'marked'; export default function UI({ data, showNavButtons = false }) { const deckRef = useRef(null); const revealRef = useRef(null); useEffect(() => { if (!data) return; // 配置 marked 解析器 marked.setOptions({ breaks: true, gfm: true, headerIds: false, mangle: false, }); // 将 markdown 内容转换为幻灯片 const slides = parseMarkdownToSlides(data); // 更新 deck 内容 if (deckRef.current) { deckRef.current.innerHTML = slides; } // 初始化 Reveal.js if (revealRef.current) { revealRef.current.destroy(); } revealRef.current = new Reveal(deckRef.current, { hash: true, transition: 'slide', transitionSpeed: 'default', backgroundTransition: 'fade', controls: true, progress: true, center: true, touch: true, loop: false, rtl: false, navigationMode: 'default', shuffle: false, fragments: true, fragmentInURL: false, embedded: false, help: true, showNotes: false, autoPlayMedia: null, preloadIframes: null, autoSlide: 0, autoSlideStoppable: true, autoSlideMethod: Reveal.navigateNext, defaultTiming: null, mouseWheel: false, hideInactiveCursor: true, hideCursorTime: 5000, previewLinks: false, postMessage: true, postMessageEvents: false, focusBodyOnPageVisibilityChange: true, viewDistance: 3, mobileViewDistance: 2, display: 'block', hideAnchorsOnUrl: false, plugins: [] }); revealRef.current.initialize(); // 清理函数 return () => { if (revealRef.current) { revealRef.current.destroy(); } }; }, [data]); // 将数据解析为幻灯片 const parseMarkdownToSlides = (data) => { if (!data) { return `

演示文稿

暂无内容

`; } // 如果有 content 字段,直接使用 markdown 内容 if (data.content) { const slideSeparators = /^---$/gm; const sections = data.content.split(slideSeparators); return sections.map((section, index) => { const trimmedSection = section.trim(); if (!trimmedSection) return ''; const htmlContent = marked(trimmedSection); // 如果是第一个幻灯片且没有标题,添加标题 if (index === 0 && !htmlContent.includes('

') && data.title) { return `

${data.title}

${htmlContent}
`; } return `
${htmlContent}
`; }).join(''); } // 如果没有 content 字段,使用 items 数据构建幻灯片 const items = data.items || []; if (items.length === 0) { // 使用主数据构建单个幻灯片 return `

${data.title || '演示文稿'}

${data.subtitle ? `

${data.subtitle}

` : ''} ${data.description ? `
${marked(data.description)}
` : ''} ${data.image ? `${data.title}` : ''}
`; } // 使用 items 构建多个幻灯片 return items.map((item, index) => { const slideContent = []; // 标题 if (item.item_title) { slideContent.push(`

${item.item_title}

`); } // 副标题 if (item.item_subtitle) { slideContent.push(`

${item.item_subtitle}

`); } // 描述 if (item.item_description) { slideContent.push(`
${marked(item.item_description)}
`); } // 图片 if (item.item_image) { slideContent.push(`${item.item_title || 'Slide'}`); } // 视频 if (item.item_video_src) { slideContent.push(``); } // 按钮 if (item.item_button_text && item.item_button_link) { slideContent.push(`
${item.item_button_text}
`); } return `
${slideContent.join('')}
`; }).join(''); }; return (
{/* 初始内容,会被 JavaScript 替换 */}

{data?.title || '演示文稿'}

正在加载内容...

); }