--- // MiniKnowledgeGraph.astro - Inline version that replaces the Tags section // Designed to work within the existing sidebar structure export interface GraphNode { id: string; label: string; type: 'post' | 'tag' | 'category'; url?: string; } export interface GraphEdge { source: string; target: string; type: 'post-tag' | 'post-post'; } interface Props { currentPost: any; relatedPosts?: any[]; } const { currentPost, relatedPosts = [] } = Astro.props; // Generate unique ID for the graph container const graphId = `mini-cy-${Math.random().toString(36).substring(2, 9)}`; // Ensure currentPost has necessary properties const safeCurrentPost = { id: currentPost.slug || 'current-post', title: currentPost.data?.title || 'Current Post', tags: currentPost.data?.tags || [], category: currentPost.data?.category || 'Uncategorized', }; // Prepare graph data const nodes: GraphNode[] = []; const edges: GraphEdge[] = []; const addedTagIds = new Set(); const addedPostIds = new Set(); // Add current post node nodes.push({ id: safeCurrentPost.id, label: safeCurrentPost.title, type: 'post', url: `/posts/${safeCurrentPost.id}/` }); addedPostIds.add(safeCurrentPost.id); // Add tags from current post safeCurrentPost.tags.forEach((tag: string) => { const tagId = `tag-${tag}`; // Only add if not already added if (!addedTagIds.has(tagId)) { nodes.push({ id: tagId, label: tag, type: 'tag', url: `/tag/${tag}/` }); addedTagIds.add(tagId); } // Add edge from current post to tag edges.push({ source: safeCurrentPost.id, target: tagId, type: 'post-tag' }); }); // Add related posts and their connections if (relatedPosts && relatedPosts.length > 0) { relatedPosts.forEach(post => { if (!post) return; const postId = post.slug || `post-${Math.random().toString(36).substring(2, 9)}`; // Skip if already added or is the current post if (addedPostIds.has(postId) || postId === safeCurrentPost.id) { return; } // Add related post node nodes.push({ id: postId, label: post.data?.title || 'Related Post', type: 'post', url: `/posts/${postId}/` }); addedPostIds.add(postId); // Add edge from current post to related post edges.push({ source: safeCurrentPost.id, target: postId, type: 'post-post' }); // Add shared tags and their connections const postTags = post.data?.tags || []; postTags.forEach((tag: string) => { // Only add connections for tags that the current post also has if (safeCurrentPost.tags.includes(tag)) { const tagId = `tag-${tag}`; // Add edge from related post to shared tag edges.push({ source: postId, target: tagId, type: 'post-tag' }); } }); }); } // Generate graph data const graphData = { nodes, edges }; ---