import React, { useState, useEffect } from 'react'; import CodeBlock from '@theme/CodeBlock'; export default function CodeFromFile({ src, language = 'python', title, startLine, endLine, highlightLines }) { const [content, setContent] = useState(''); const [error, setError] = useState(null); useEffect(() => { async function loadFile() { try { // File registration is now handled by the file-sync-plugin during build // Load file from static/imported-files directory const response = await fetch(`/imported-files/${src}`); if (!response.ok) { throw new Error(`Failed to fetch: ${response.status}`); } let text = await response.text(); // Handle line range if specified (filtering is done at build time) if (startLine || endLine) { const lines = text.split('\n'); const start = startLine ? Math.max(0, startLine - 1) : 0; const end = endLine ? Math.min(lines.length, endLine) : lines.length; text = lines.slice(start, end).join('\n'); } setContent(text); } catch (err) { console.error('Failed to load file:', err); setError(`Failed to load ${src}: ${err.message}`); } } loadFile(); }, [src, startLine, endLine]); if (error) { return