Skip to content

Commit

Permalink
intermediate fix: slugs in search records (#1524)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Sep 23, 2024
1 parent 924c746 commit c988002
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 93 deletions.
60 changes: 0 additions & 60 deletions servers/fdr/src/__test__/unit-tests/algolia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,65 +281,5 @@ describe("algolia utils", () => {
slug: "/v1/someslug",
},
]);

console.log(
getMarkdownSections(
{
level: 0,
heading: "",
content: "\n",
children: [
{
level: 1,
heading: "A",
content: "this is line A\n",
children: [
{
level: 2,
heading: "B",
content: "this is line b\n```\n## somecrap\nfasdfafafdadf\n```\n",
children: [
{
level: 3,
heading: "C",
content: "this is line c\n\nthis is line c.2\n\n",
children: [],
},
{
level: 3,
heading: "D",
content: "this is line d\n",
children: [],
},
],
},
{
level: 2,
heading: "E",
content: "this is line e\n",
children: [
{
level: 3,
heading: "F",
content: "this is line f\nthis is line f.2\n",
children: [],
},
],
},
{
level: 2,
heading: "G",
content: "this is line g\n\n",
children: [],
},
],
},
],
},
[],
"testindex",
"/v1/someslug",
),
);
});
});
65 changes: 32 additions & 33 deletions servers/fdr/src/services/algolia/AlgoliaSearchRecordGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,10 @@ export class AlgoliaSearchRecordGenerator {
api != null ? FernNavigation.ApiDefinitionHolder.create(convertDbAPIDefinitionToRead(api)) : undefined;
const records: AlgoliaSearchRecord[] = [];

const breadcrumbs = context.pathParts.map((part) => part.name);
const breadcrumbs = context.pathParts.map((part) => ({
title: part.name,
slug: part.urlSlug,
}));

const version =
context.indexSegment.type === "versioned"
Expand All @@ -683,20 +686,18 @@ export class AlgoliaSearchRecordGenerator {
? false
: true,
)
.map((parent) => parent.title),
].reduce((acc: BreadcrumbsInfo[], breadcrumb: string) => {
if (acc.length === 0) {
return [{ title: breadcrumb, slug: breadcrumb }];
}
return [...acc, { title: breadcrumb, slug: `${acc[acc.length - 1]?.slug}/${breadcrumb}` }];
}, []);
.map((parent) => ({
title: parent.title,
slug: parent.slug,
})),
];
}

function anchorIdToSlug(
node: FernNavigation.EndpointNode | WebSocketNode | WebhookNode,
anchorIdParts: string[],
) {
return `${node.slug}#${anchorIdParts.join(".")}`;
return encodeURI(`${node.slug}#${anchorIdParts.join(".")}`);
}

FernNavigation.utils.traverseNavigation(root, (node, _index, parents) => {
Expand Down Expand Up @@ -1525,7 +1526,6 @@ export class AlgoliaSearchRecordGenerator {
...allReferencedTypes,
};
});

const contents = [];
if (Object.keys(referencedTypes).length > 0) {
contents.push("## Referenced Types\n");
Expand Down Expand Up @@ -1637,6 +1637,7 @@ export class AlgoliaSearchRecordGenerator {
: webhookProperties
: websocketProperties;

const baseSlug = encodeURI(`${metadata.slugPrefix}.${value.name}`);
fields.push({
type: "field-v1",
objectID: uuid(),
Expand All @@ -1645,7 +1646,7 @@ export class AlgoliaSearchRecordGenerator {
description: value.description,
availability: value.availability,
breadcrumbs: metadata.breadcrumbs,
slug: `${metadata.slugPrefix}.${key}`,
slug: baseSlug,
version: metadata.version,
indexSegmentId: metadata.indexSegmentId,
...additionalProperties,
Expand All @@ -1656,7 +1657,7 @@ export class AlgoliaSearchRecordGenerator {
object: (object) => {
object.properties.forEach((property) => {
if (metadata != null) {
const slug = `${metadata.slugPrefix}.${key}.${property.key}`;
const slug = encodeURI(`${baseSlug}.${property.key}`);
fields.push({
type: "field-v1",
objectID: uuid(),
Expand All @@ -1682,7 +1683,7 @@ export class AlgoliaSearchRecordGenerator {
enum: (enum_) => {
enum_.values.forEach((value) => {
if (metadata != null) {
const slug = `${metadata.slugPrefix}.${key}.${value.value}`;
const slug = encodeURI(`${baseSlug}.${value.value}`);
fields.push({
type: "field-v1",
objectID: uuid(),
Expand Down Expand Up @@ -1712,7 +1713,7 @@ export class AlgoliaSearchRecordGenerator {
? variant.type.value
: undefined;
if (metadata != null && title != null) {
const slug = `${metadata.slugPrefix}.${key}.${title}`;
const slug = encodeURI(`${baseSlug}.${title}`);
fields.push({
type: "field-v1",
objectID: uuid(),
Expand All @@ -1737,7 +1738,7 @@ export class AlgoliaSearchRecordGenerator {
value.variants.forEach((variant) => {
const title = variant.displayName ?? titleCase(variant.discriminantValue);
if (metadata != null) {
const slug = `${metadata.slugPrefix}.${key}.${title}`;
const slug = encodeURI(`${baseSlug}.${title}`);
fields.push({
type: "field-v1",
objectID: uuid(),
Expand Down Expand Up @@ -2241,34 +2242,32 @@ export function getMarkdownSections(
breadcrumbs: BreadcrumbsInfo[],
indexSegmentId: string,
slug: string,
firstNode = true,
): AlgoliaSearchRecord[] {
const sectionBreadcrumbs = markdownSection.heading
? breadcrumbs.concat([
{
title: markdownSection.heading,
slug: firstNode ? slug : `${slug}#${markdownSection.heading}`,
slug: markdownSection.level === 0 ? slug : `${slug}#${markdownSection.heading}`,
},
])
: breadcrumbs.slice(0);
const records: AlgoliaSearchRecord[] = firstNode
? []
: [
compact({
type: "markdown-section-v1",
objectID: uuid(),
title: markdownSection.heading,
content: markdownSection.content,
breadcrumbs: sectionBreadcrumbs,
indexSegmentId,
slug,
}),
];
const records: AlgoliaSearchRecord[] =
markdownSection.content.trim().length === 0
? []
: [
compact({
type: "markdown-section-v1",
objectID: uuid(),
title: markdownSection.heading,
content: markdownSection.content,
breadcrumbs: sectionBreadcrumbs,
indexSegmentId,
slug,
}),
];
return records.concat(
markdownSection.children.reduce((acc: AlgoliaSearchRecord[], markdownSectionChild: MarkdownNode) => {
return acc.concat(
getMarkdownSections(markdownSectionChild, sectionBreadcrumbs, indexSegmentId, slug, false),
);
return acc.concat(getMarkdownSections(markdownSectionChild, sectionBreadcrumbs, indexSegmentId, slug));
}, []),
);
}

0 comments on commit c988002

Please sign in to comment.