'use client'; import { useState, useEffect } from 'react'; import { useSearchParams } from 'next/navigation'; import { MarkdownViewer } from '@/components/MarkdownViewer'; import { FileUploader } from '@/components/FileUploader'; export function ViewerContent() { const [markdown, setMarkdown] = useState(''); const [title, setTitle] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const searchParams = useSearchParams(); // Load markdown from URL query parameter ?parse=https://url/file.md useEffect(() => { const parseUrl = searchParams.get('parse'); if (parseUrl) { setLoading(true); fetch(parseUrl) .then((res) => { if (!res.ok) throw new Error(`Failed to fetch: ${res.statusText}`); return res.text(); }) .then((content) => { // Extract title from first H1 const h1Match = content.match(/^#\s+(.+)$/m); const extractedTitle = h1Match ? h1Match[1] : 'Document'; setTitle(extractedTitle); setMarkdown(content); setLoading(false); }) .catch((err) => { setError(`Error loading document: ${err.message}`); setLoading(false); }); } }, [searchParams]); if (loading) { return (
Loading document...
{error}