Skip to content

(EAI-1033): include TOC index for snooty ingest #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("SnootyDataSource", () => {
],
};
const snootyDataApiBaseUrl = "https://snooty-data-api.mongodb.com/prod/";

describe("makeSnootyDataSource()", () => {
const sampleDataPath = Path.resolve(
SRC_ROOT,
Expand Down Expand Up @@ -251,6 +252,17 @@ describe("SnootyDataSource", () => {
expect(pages).toHaveLength(1);
noIndexMock.done();
});

it("includes tocIndex in metadata", async () => {
const source = await makeSnootyDataSource({
name: `snooty-test`,
project,
snootyDataApiBaseUrl,
});
const pages = await source.fetchPages();

expect(pages[0].metadata?.page?.tocIndex).toBe(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too familiar with snooty toc internals - do we just have a number or can we get some kind of TOC breadcrumb string?

e.g. something like breadcrumb: "Indexes > Create" is more useful at a glance than tocIndex: 112

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just have a number. and this is just to be used for things like concating page summaries a la skunkworks. doesn't get into embedded content. so just having index is sufficient for that

});
});
});

Expand All @@ -270,6 +282,7 @@ describe("handlePage()", () => {
baseUrl: "https://example.com",
tags: ["a"],
version: { label: "1.0", isCurrent: true },
toc: [],
});
expect(result).toMatchObject({
format: "openapi-yaml",
Expand All @@ -292,6 +305,7 @@ describe("handlePage()", () => {
baseUrl: "https://example.com",
tags: ["a"],
version: { label: "1.0", isCurrent: true },
toc: [],
});
expect(result).toMatchObject({
format: "md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@ export type SnootyPageEntry = SnootyManifestEntry & {
data: SnootyPageData;
};

export interface SnootyTocEntry {
title: {
type: "text";
position: {
start: {
line: number;
};
};
value: string;
}[];
slug?: string;
url?: string;
children: SnootyTocEntry[];
options?: {
drawer?: boolean;
};
}

/**
Represents metadata in a Snooty manifest file.
*/
export type SnootyMetadataEntry = SnootyManifestEntry & {
type: "metadata";
data: { title?: string };
data: { title?: string; toctree: SnootyTocEntry; toctreeOrder: string[] };
};

/**
Expand Down Expand Up @@ -202,6 +220,7 @@ export const makeSnootyDataSource = ({
const linePromises: Promise<void>[] = [];
const pages: Page[] = [];
let siteTitle: string | undefined = undefined;
const toc: string[] = [];
await new Promise<void>((resolve, reject) => {
stream.on("line", async (line) => {
const entry = JSON.parse(line) as SnootyManifestEntry;
Expand All @@ -227,6 +246,7 @@ export const makeSnootyDataSource = ({
...links,
baseUrl: branchUrl,
},
toc,
});
if (page !== undefined) {
pages.push(page);
Expand All @@ -249,6 +269,18 @@ export const makeSnootyDataSource = ({
case "metadata": {
const { data } = entry as SnootyMetadataEntry;
siteTitle = data.title;
const visitedUrls = new Set<string>();
const tocUrls = data.toctreeOrder
.filter((slug) => {
const url = makeUrl(slug, branchUrl);
if (visitedUrls.has(url)) {
return false;
}
visitedUrls.add(url);
return true;
})
.map((slug) => makeUrl(slug, branchUrl));
toc.push(...tocUrls);
return;
}
case "timestamp":
Expand Down Expand Up @@ -344,6 +376,7 @@ export const handlePage = async (
productName,
version,
links,
toc,
}: {
sourceName: string;
baseUrl: string;
Expand All @@ -354,6 +387,7 @@ export const handlePage = async (
isCurrent: boolean;
};
links?: RenderLinks;
toc: string[];
}
): Promise<Page | undefined> => {
// Strip first three path segments - according to Snooty team, they'll always
Expand All @@ -373,11 +407,8 @@ export const handlePage = async (
let body = "";
let title: string | undefined;
let format: PageFormat;
const baseUrlTrailingSlash = baseUrl.replace(/\/?$/, "/");
const url = new URL(pagePath, baseUrlTrailingSlash).href.replace(
/\/?$/, // Add trailing slash
"/"
);
const url = makeUrl(pagePath, baseUrl);

if (page.ast.options?.template === "openapi") {
format = "openapi-yaml";
body = await snootyAstToOpenApiSpec(page.ast);
Expand All @@ -395,6 +426,9 @@ export const handlePage = async (
return;
}

const maybeTocIndex = toc.findIndex((tocUrl) => tocUrl === url);
const tocIndex = maybeTocIndex === -1 ? undefined : maybeTocIndex;

return {
url,
sourceName,
Expand All @@ -403,10 +437,28 @@ export const handlePage = async (
format,
sourceType: "tech-docs",
metadata: {
page: pageMetadata,
page: { ...pageMetadata, tocIndex },
tags,
productName,
version,
},
};
};

function makeUrl(pagePath: string, baseUrl: string): string {
// Ensure trailing slash for baseUrl
const baseUrlTrailingSlash = baseUrl.replace(/\/?$/, "/");

// Handle empty pagePath or root path
if (!pagePath || pagePath === "/") {
return baseUrlTrailingSlash;
}

// For non-empty paths, remove leading slash and ensure trailing slash
const cleanPagePath = pagePath
.replace(/^\//, "") // Remove leading slash
.replace(/\/?$/, "/"); // Ensure trailing slash

// Concatenate the base URL with the clean page path
return baseUrlTrailingSlash + cleanPagePath;
}