@asafarim/markdown-explorer-viewer
Version:
A reusable React component for exploring and viewing markdown files with file tree navigation
124 lines (89 loc) • 3.23 kB
Markdown
The Markdown Explorer Viewer provides a comprehensive API for customizing and controlling the component behavior.
The main component that orchestrates the entire experience.
**Props:**
- `fileTree`: FileNode - The hierarchical file structure
- `theme`: 'light' | 'dark' | 'auto' - Color theme
- `enableSearch`: boolean - Enable/disable search functionality
- `showBreadcrumbs`: boolean - Show navigation breadcrumbs
- `onNavigate`: function - Callback for navigation events
Standalone file tree component for custom layouts.
**Props:**
- `fileTree`: FileNode - File structure to display
- `currentPath`: string - Currently selected path
- `onNodeClick`: function - Handle file/folder clicks
- `enableSearch`: boolean - Enable search within tree
Pure markdown rendering component.
**Props:**
- `content`: string - Markdown content to render
- `theme`: 'light' | 'dark' | 'auto' - Color theme
- `components`: object - Custom React components for markdown elements
```typescript
interface FileNode {
name: string; // Display name
path: string; // Full path identifier
type: 'file' | 'folder'; // Node type
children?: FileNode[]; // Child nodes (folders only)
content?: string; // File content (files only)
lastModified?: string; // ISO date string
size?: number; // File size in bytes
}
```
Converts a flat file structure into a hierarchical tree.
```typescript
const files = {
'/docs/readme.md': '# Documentation',
'/docs/api/overview.md': '# API Overview'
};
const tree = parseFileTree(files);
```
Locates a specific node in the file tree.
```typescript
const node = findNodeByPath(fileTree, '/docs/api/overview.md');
```
Searches for nodes matching the given query.
```typescript
const results = searchNodes(fileTree, 'api');
```
```typescript
const handleNavigate = (path: string, node: FileNode) => {
// Called when user navigates to a different file/folder
console.log('Navigated to:', path);
// Update browser URL
window.history.pushState({}, '', `/docs\$path}`);
// Track analytics
analytics.track('documentation_view', { path });
};
```
Search is handled internally, but you can control it programmatically:
```typescript
// Custom search implementation
const MyCustomExplorer = () => {
const [searchQuery, setSearchQuery] = useState('');
const filteredTree = useMemo(() => {
return searchQuery
? filterTreeByQuery(fileTree, searchQuery)
: fileTree;
}, [fileTree, searchQuery]);
return (
<MarkdownExplorer
fileTree={filteredTree}
enableSearch={false} // Disable built-in search
/>
);
};
```
For more detailed examples, see the [API Reference](/docs/api/reference.md).