| // src/components/article/ArticlePreview.tsx | |
| import { useEffect, useRef, useState } from "react"; | |
| import ReactMarkdown from "react-markdown"; | |
| import remarkGfm from "remark-gfm"; | |
| import remarkMath from "remark-math"; | |
| import rehypeKatex from "rehype-katex"; | |
| import rehypeHighlight from "rehype-highlight"; | |
| import rehypeSlug from "rehype-slug"; | |
| import rehypeAutolinkHeadings from "rehype-autolink-headings"; | |
| import { MathRenderer } from "./MathRenderer"; | |
| import "katex/dist/katex.min.css"; | |
| import "highlight.js/styles/github-dark.css"; | |
| interface ArticlePreviewProps { | |
| title: string; | |
| abstract: string; | |
| body: string; | |
| show: boolean; | |
| enableScrollSync?: boolean; // if true, tries to keep preview scrolled to editing position | |
| editorScrollRef?: React.RefObject<HTMLElement>; | |
| } | |
| export function ArticlePreview({ | |
| title, | |
| abstract, | |
| body, | |
| show, | |
| enableScrollSync = false, | |
| editorScrollRef, | |
| }: ArticlePreviewProps) { | |
| const previewRef = useRef<HTMLDivElement>(null); | |
| const [wordCount, setWordCount] = useState(0); | |
| const [charCount, setCharCount] = useState(0); | |
| const [readingTime, setReadingTime] = useState(0); | |
| // Compute text statistics | |
| useEffect(() => { | |
| const fullText = `${title} ${abstract} ${body}`.trim(); | |
| const words = fullText.split(/\s+/).filter((w) => w.length > 0); | |
| setWordCount(words.length); | |
| setCharCount(fullText.length); | |
| // average reading speed: 200 words per minute | |
| setReadingTime(Math.max(1, Math.ceil(words.length / 200))); | |
| }, [title, abstract, body]); | |
| // Optional scroll sync (very basic: adjust scrollTop proportionally) | |
| useEffect(() => { | |
| if (!enableScrollSync || !previewRef.current || !editorScrollRef?.current) return; | |
| const editor = editorScrollRef.current; | |
| const preview = previewRef.current; | |
| const handleEditorScroll = () => { | |
| const scrollPercent = editor.scrollTop / (editor.scrollHeight - editor.clientHeight); | |
| preview.scrollTop = scrollPercent * (preview.scrollHeight - preview.clientHeight); | |
| }; | |
| editor.addEventListener("scroll", handleEditorScroll); | |
| return () => editor.removeEventListener("scroll", handleEditorScroll); | |
| }, [enableScrollSync, editorScrollRef]); | |
| if (!show) return null; | |
| const hasContent = title || abstract || body; | |
| return ( | |
| <div className="bg-white dark:bg-gray-800 rounded-2xl shadow-lg border border-gray-100 dark:border-gray-700 h-fit sticky top-24 flex flex-col max-h-[calc(100vh-6rem)]"> | |
| {/* Header with stats */} | |
| <div className="p-4 border-b border-gray-100 dark:border-gray-700"> | |
| <div className="flex items-center justify-between mb-2"> | |
| <h2 className="text-lg font-semibold text-gray-800 dark:text-gray-200 flex items-center gap-2"> | |
| 📄 Live Preview | |
| </h2> | |
| <span className="text-xs text-gray-400">updated as you type</span> | |
| </div> | |
| <div className="flex gap-4 text-xs text-gray-500"> | |
| <span>{wordCount} words</span> | |
| <span>{charCount} characters</span> | |
| <span>{readingTime} min read</span> | |
| </div> | |
| </div> | |
| {/* Scrollable preview area */} | |
| <div | |
| ref={previewRef} | |
| className="flex-1 overflow-y-auto p-4 prose dark:prose-invert max-w-none" | |
| > | |
| {!hasContent ? ( | |
| <div className="text-center py-12 text-gray-400 dark:text-gray-500"> | |
| Start writing to see a preview here. | |
| </div> | |
| ) : ( | |
| <div className="space-y-4"> | |
| {title && ( | |
| <h1 className="text-2xl font-bold text-gray-900 dark:text-white border-b pb-2"> | |
| {title} | |
| </h1> | |
| )} | |
| {abstract && ( | |
| <p className="text-sm text-gray-600 dark:text-gray-400 italic border-l-4 border-primary pl-3"> | |
| {abstract} | |
| </p> | |
| )} | |
| {body && ( | |
| <ReactMarkdown | |
| remarkPlugins={[remarkGfm, remarkMath]} | |
| rehypePlugins={[ | |
| rehypeKatex, | |
| rehypeHighlight, | |
| rehypeSlug, | |
| [rehypeAutolinkHeadings, { behavior: "wrap" }], | |
| ]} | |
| components={{ | |
| // Reuse same custom components as in ArticleBody (optional) | |
| img: ({ src, alt }) => ( | |
| <img | |
| src={src} | |
| alt={alt || "Preview image"} | |
| className="rounded-xl max-w-full h-auto" | |
| loading="lazy" | |
| /> | |
| ), | |
| code: ({ className, children }: any) => { | |
| const match = /language-(\w+)/.exec(className || ""); | |
| return match ? ( | |
| <pre className="rounded-lg overflow-x-auto p-3 bg-gray-900 text-gray-100"> | |
| <code className={className}>{children}</code> | |
| </pre> | |
| ) : ( | |
| <code className="bg-gray-100 dark:bg-gray-800 rounded px-1 py-0.5"> | |
| {children} | |
| </code> | |
| ); | |
| }, | |
| a: ({ href, children }) => ( | |
| <a | |
| href={href} | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| className="text-primary hover:underline" | |
| > | |
| {children} ↗ | |
| </a> | |
| ), | |
| }} | |
| > | |
| {body} | |
| </ReactMarkdown> | |
| )} | |
| {/* Fallback MathRenderer for legacy content */} | |
| <MathRenderer content={body} /> | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 5.79 kB
- Xet hash:
- 57e5b38ae5aaa455e1c669ccc1a2577e2f4e028a1cf7fcf0e982b693498e6a35
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.