Skip to content

Commit

Permalink
fix(ai): only index nodes with unique canonical slugs (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jan 13, 2025
1 parent 54d214c commit f39bf28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 24 additions & 1 deletion packages/fdr-sdk/src/navigation/NodeCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EMPTY_ARRAY } from "@fern-api/ui-core-utils";
import { once } from "es-toolkit/function";
import { FernNavigation } from "./..";
import { pruneVersionNode } from "./utils/pruneVersionNode";
import { NavigationNodeWithMetadata } from "./versions";

interface NavigationNodeWithMetadataAndParents {
node: FernNavigation.NavigationNodeWithMetadata;
Expand Down Expand Up @@ -134,7 +135,6 @@ export class NodeCollector {
this.#setNode(node.slug, node, parents);
} else {
if (FernNavigation.isPage(existing.node)) {
// eslint-disable-next-line no-console
console.warn(`Duplicate slug found: ${node.slug}`, node.title);
}
this.orphanedNodes.push(node);
Expand Down Expand Up @@ -236,6 +236,29 @@ export class NodeCollector {
return this.#getIndexablePageSlugs();
}

#getIndexablePageNodesWithAuth = once((): NavigationNodeWithMetadata[] => {
const slugRecord: Record<string, NavigationNodeWithMetadata> = {};

[...this.slugToNode.values()]
.filter(({ node }) => FernNavigation.isPage(node))
.filter(({ node }) => !node.hidden)
.filter(({ node }) =>
FernNavigation.hasMarkdown(node) ? !node.noindex : true
)
.forEach((node) => {
const canonicalSlug = node.node.canonicalSlug ?? node.node.slug;
// Only keep the first node we see for each canonical slug
if (!(canonicalSlug in slugRecord)) {
slugRecord[canonicalSlug] = node.node;
}
});

return Object.values(slugRecord);
});
get indexablePageNodesWithAuth(): NavigationNodeWithMetadata[] {
return this.#getIndexablePageNodesWithAuth();
}

public getVersionNodes = (): FernNavigation.VersionNode[] => {
return this.versionNodes;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
RootNode,
getPageId,
hasMarkdown,
isPage,
} from "@fern-api/fdr-sdk/navigation";
import { flatten } from "es-toolkit/array";
import { FernTurbopufferRecordWithoutVector } from "../types";
Expand Down Expand Up @@ -35,12 +34,7 @@ export async function createTurbopufferRecords({
> {
const collector = NodeCollector.collect(root);

const pageNodes = Array.from(collector.slugMap.values())
.filter(isPage)
// exclude hidden pages
.filter((node) => node.hidden !== true)
// exclude pages that are noindexed
.filter((node) => (hasMarkdown(node) ? node.noindex !== true : true));
const pageNodes = collector.indexablePageNodesWithAuth;

const markdownNodes = pageNodes.filter(hasMarkdown);
// const apiLeafNodes = pageNodes.filter(isApiLeaf);
Expand Down

0 comments on commit f39bf28

Please sign in to comment.