Feat: Implement combined related posts logic for main graph
Adds tag similarity fallback for post connections on main knowledge graph, ensuring preview functionality remains intact.
This commit is contained in:
parent
dba0eac3e4
commit
50c24407f2
|
@ -63,13 +63,15 @@ const graphData = {
|
||||||
edges: []
|
edges: []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create edges between posts and their tags
|
// Create edges between posts and their tags, and between related posts
|
||||||
|
const processedPostPairs = new Set(); // Avoid duplicate A->B and B->A edges
|
||||||
|
|
||||||
sortedPosts
|
sortedPosts
|
||||||
.filter(post => !post.data.draft)
|
.filter(post => !post.data.draft)
|
||||||
.forEach(post => {
|
.forEach(post => {
|
||||||
const postTags = post.data.tags || [];
|
const postTags = post.data.tags || [];
|
||||||
|
|
||||||
// Add edges from post to tags
|
// 1. Add edges from post to tags
|
||||||
postTags.forEach(tag => {
|
postTags.forEach(tag => {
|
||||||
graphData.edges.push({
|
graphData.edges.push({
|
||||||
source: post.slug,
|
source: post.slug,
|
||||||
|
@ -79,21 +81,59 @@ sortedPosts
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if post references other posts (optional)
|
// --- Find Related Posts (Explicit + Implicit) ---
|
||||||
// This requires a related_posts field in frontmatter
|
const MAX_RELATED_LINKS = 3;
|
||||||
if (post.data.related_posts && Array.isArray(post.data.related_posts)) {
|
const MIN_SHARED_TAGS_FOR_LINK = 2;
|
||||||
post.data.related_posts.forEach(relatedSlug => {
|
|
||||||
// Make sure related post exists
|
let relatedLinks = new Map(); // Use Map to store related slug and relation type (explicit/implicit)
|
||||||
if (sortedPosts.some(p => p.slug === relatedSlug)) {
|
|
||||||
|
// a. Find explicitly related posts
|
||||||
|
const explicitSlugs = new Set(post.data.related_posts || []);
|
||||||
|
explicitSlugs.forEach(slug => {
|
||||||
|
if (sortedPosts.some(p => p.slug === slug && !p.data.draft)) {
|
||||||
|
relatedLinks.set(slug, 'explicit');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// b. Find implicitly related posts by tag similarity (if needed)
|
||||||
|
if (relatedLinks.size < MAX_RELATED_LINKS && postTags.length > 0) {
|
||||||
|
const potentialRelated = sortedPosts
|
||||||
|
.filter(otherPost =>
|
||||||
|
!otherPost.data.draft &&
|
||||||
|
otherPost.slug !== post.slug && // Not the same post
|
||||||
|
!relatedLinks.has(otherPost.slug) // Not already added
|
||||||
|
)
|
||||||
|
.map(otherPost => {
|
||||||
|
const otherTags = otherPost.data.tags || [];
|
||||||
|
const sharedTagsCount = postTags.filter(tag => otherTags.includes(tag)).length;
|
||||||
|
return { slug: otherPost.slug, score: sharedTagsCount };
|
||||||
|
})
|
||||||
|
.filter(item => item.score >= MIN_SHARED_TAGS_FOR_LINK)
|
||||||
|
.sort((a, b) => b.score - a.score); // Sort by most shared tags
|
||||||
|
|
||||||
|
potentialRelated.forEach(item => {
|
||||||
|
if (relatedLinks.size < MAX_RELATED_LINKS) {
|
||||||
|
relatedLinks.set(item.slug, 'implicit');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Create edges for the found related posts
|
||||||
|
relatedLinks.forEach((relationType, relatedSlug) => {
|
||||||
|
const pairKey1 = `${post.slug}-${relatedSlug}`;
|
||||||
|
const pairKey2 = `${relatedSlug}-${post.slug}`;
|
||||||
|
|
||||||
|
if (!processedPostPairs.has(pairKey1) && !processedPostPairs.has(pairKey2)) {
|
||||||
graphData.edges.push({
|
graphData.edges.push({
|
||||||
source: post.slug,
|
source: post.slug,
|
||||||
target: relatedSlug,
|
target: relatedSlug,
|
||||||
type: 'post-post',
|
type: 'post-post',
|
||||||
strength: 2
|
strength: relationType === 'explicit' ? 3 : 1.5 // Stronger edge for explicit links
|
||||||
});
|
});
|
||||||
|
processedPostPairs.add(pairKey1);
|
||||||
|
processedPostPairs.add(pairKey2); // Add both directions to set
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Terminal commands for tech effect
|
// Terminal commands for tech effect
|
||||||
|
|
Loading…
Reference in New Issue