Skip to content
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

fetch markdown from filesv2 #2055

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion fern/apis/fdr/definition/docs/v1/read/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ types:

PageContent:
properties:
markdown: string # eventually PageContent should just be a rootCommons.FileId ?
markdown: optional<string>
url: optional<rootCommons.Url>
editThisPageUrl: optional<rootCommons.Url>

DocsConfig:
Expand Down
3 changes: 2 additions & 1 deletion fern/apis/fdr/definition/docs/v1/write/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ types:

PageContent:
properties:
markdown: string # eventually PageContent should just be a rootCommons.FileId ?
markdown: optional<string>
url: optional<rootCommons.Url>
editThisPageUrl: optional<rootCommons.Url>

DocsConfig:
Expand Down
45 changes: 16 additions & 29 deletions packages/fdr-sdk/src/__test__/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,54 +98,44 @@ function testNavigationConfigConverter(fixtureName: string): void {
it("should have unique canonical urls for each page", () => {
const visitedPageIds = new Set<string>();
collector.indexablePageSlugs.forEach((slug) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const node = collector.slugMap.get(slug)!;
const node = collector.slugMap.get(slug);
expect(node).toBeDefined();
if (node == null) {
return;
}
if (!FernNavigation.isPage(node) || node.hidden) {
console.log(node);
}
expect(FernNavigation.isPage(node), `${slug} is a page`).toBe(true);
expect(node.hidden, `${slug} is not hidden`).not.toBe(true);
expect(FernNavigation.isPage(node)).toBe(true);
expect(node.hidden).not.toBe(true);

if (FernNavigation.hasMarkdown(node)) {
expect(node.noindex, `${slug} is indexable`).not.toBe(true);
expect(node.noindex).not.toBe(true);
}

const pageId = FernNavigation.isPage(node)
? FernNavigation.getPageId(node)
: undefined;
if (pageId != null) {
expect(
visitedPageIds.has(pageId),
`${slug} must not be repeated key=${pageId}`
).toBe(false);
expect(visitedPageIds.has(pageId)).toBe(false);
visitedPageIds.add(pageId);
}

if (node.type === "endpoint") {
const pageId = `${node.apiDefinitionId}-${node.endpointId}`;
expect(
visitedPageIds.has(pageId),
`${slug} must not be repeated key=${pageId})`
).toBe(false);
expect(visitedPageIds.has(pageId)).toBe(false);
visitedPageIds.add(pageId);
}

if (node.type === "webSocket") {
const pageId = `${node.apiDefinitionId}-${node.webSocketId}`;
expect(
visitedPageIds.has(pageId),
`${slug} must not be repeated key=${pageId})`
).toBe(false);
expect(visitedPageIds.has(pageId)).toBe(false);
visitedPageIds.add(pageId);
}

if (node.type === "webhook") {
const pageId = `${node.apiDefinitionId}-${node.webhookId}`;
expect(
visitedPageIds.has(pageId),
`${slug} must not be repeated key=${pageId})`
).toBe(false);
expect(visitedPageIds.has(pageId)).toBe(false);
visitedPageIds.add(pageId);
}
});
Expand Down Expand Up @@ -183,14 +173,11 @@ function sortObject(object: unknown): unknown {
return 0;
});

for (const index in keys) {
const key = keys[index];
if (key) {
if (typeof object[key] === "object") {
sortedObj[key] = sortObject(object[key]);
} else {
sortedObj[key] = object[key];
}
for (const key of keys) {
if (typeof object[key] === "object") {
sortedObj[key] = sortObject(object[key]);
} else {
sortedObj[key] = object[key];
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/fdr-sdk/src/navigation/utils/getFrontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
export function getFrontmatter(markdown: string): string | undefined {
const frontmatterMatch = /^---\s*([\s\S]*?)\s*---/.exec(markdown.trimStart());
if (!frontmatterMatch || frontmatterMatch[1] == null) {
if (frontmatterMatch?.[1] == null) {
return undefined;
}
return frontmatterMatch[1];
Expand Down
10 changes: 2 additions & 8 deletions packages/fdr-sdk/src/navigation/utils/toRootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ import { FernNavigationV1ToLatest } from "../migrators/v1ToV2";
import { mutableUpdatePointsTo } from "./updatePointsTo";

export function toRootNode(
docs: DocsV2Read.LoadDocsForUrlResponse,
disableEndpointPairs: boolean = false,
paginated?: boolean
docs: DocsV2Read.LoadDocsForUrlResponse
): FernNavigation.RootNode {
const v1 = FernNavigation.V1.toRootNode(
docs,
disableEndpointPairs,
paginated
);
const v1 = FernNavigation.V1.toRootNode(docs);
const latest = FernNavigationV1ToLatest.create().root(v1);
// update all `pointsTo`
mutableUpdatePointsTo(latest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,12 @@
import { mapValues } from "es-toolkit/object";
import { FernNavigation } from "../../../..";
import { APIV1Read, type DocsV2Read } from "../../../../client/types";
import { getFrontmatter } from "../../../utils/getFrontmatter";
import { getNoIndexFromFrontmatter } from "../../../utils/getNoIndexFromFrontmatter";
import { NavigationConfigConverter } from "./NavigationConfigConverter";
import { getFullSlugFromFrontmatter } from "./getFullSlugFromFrontmatter";
import { type DocsV2Read } from "../../../../client/types";

export function toRootNode(
response: DocsV2Read.LoadDocsForUrlResponse,
disableEndpointPairs: boolean = false,
paginated?: boolean
response: DocsV2Read.LoadDocsForUrlResponse
): FernNavigation.V1.RootNode {
const noindexMap: Record<FernNavigation.V1.PageId, boolean> = {};
const fullSlugMap: Record<FernNavigation.V1.PageId, FernNavigation.V1.Slug> =
{};
Object.entries(response.definition.pages).forEach(([pageId, page]) => {
const frontmatter = getFrontmatter(page.markdown);
if (frontmatter == null) {
return;
}

const noindex = getNoIndexFromFrontmatter(frontmatter);
if (noindex != null) {
noindexMap[FernNavigation.V1.PageId(pageId)] = noindex;
}

// get full slug from frontmatter
const fullSlug = getFullSlugFromFrontmatter(frontmatter);
if (fullSlug != null) {
fullSlugMap[FernNavigation.V1.PageId(pageId)] = fullSlug;
}
});

if (response.definition.config.root) {
return response.definition.config.root;
} else if (response.definition.config.navigation) {
return NavigationConfigConverter.convert(
response.definition.config.title,
response.definition.config.navigation,
fullSlugMap,
noindexMap,
hackReorderApis(response.definition.apis, response.baseUrl.domain),
response.baseUrl.basePath,
isLexicographicSortEnabled(response.baseUrl.domain),
disableEndpointPairs,
paginated
);
} else {
// eslint-disable-next-line no-console
console.error("No root node found");
return {
type: "root",
Expand Down Expand Up @@ -76,47 +35,3 @@ export function toRootNode(
};
}
}

function isLexicographicSortEnabled(domain: string): boolean {
// HACKHACK: This is a temporary solution to enable lexicographic sorting for AIA docs.
// Vercel's edge config UI is broken right now so we can't modify it there.
return domain.toLowerCase().includes("aia.docs.buildwithfern.com");
}

function hackReorderApis(
apis: Record<string, APIV1Read.ApiDefinition>,
domain: string
): Record<string, APIV1Read.ApiDefinition> {
if (!domain.includes("assemblyai")) {
return apis;
}

return mapValues(apis, (api) => hackReorderAssemblyApi(api));
}

function hackReorderAssemblyApi(
api: APIV1Read.ApiDefinition
): APIV1Read.ApiDefinition {
const SUBPACKAGE_REALTIME = APIV1Read.SubpackageId("subpackage_realtime");
const SUBPACKAGE_STREAMING = APIV1Read.SubpackageId("subpackage_streaming");

const realtime = api.subpackages[SUBPACKAGE_REALTIME];
const streaming = api.subpackages[SUBPACKAGE_STREAMING];

if (realtime == null || streaming == null) {
return api;
}

streaming.endpoints = [...realtime.endpoints, ...streaming.endpoints];
streaming.websockets = [...realtime.websockets, ...streaming.websockets];
streaming.webhooks = [...realtime.webhooks, ...streaming.webhooks];

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete api.subpackages[SUBPACKAGE_REALTIME];

api.rootPackage.subpackages = api.rootPackage.subpackages.filter(
(subpackageId) => subpackageId !== SUBPACKAGE_REALTIME
);

return api;
}
Loading
Loading