Skip to content

Commit

Permalink
fix: add absolute path to frontmatter meta og:image (cohere) (#1223)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Jiang <[email protected]>
  • Loading branch information
RohinBhargava and abvthecity authored Jul 31, 2024
1 parent 5c57645 commit af1c6ea
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions clis/docs-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@fern-fern/docs-config": "^0.22.0",
"@fern-ui/core-utils": "workspace:*",
"@fern-api/fdr-sdk": "workspace:*",
"execa": "^5.1.1",
"gray-matter": "^4.0.3",
"lodash-es": "^4.17.21",
Expand Down
4 changes: 3 additions & 1 deletion clis/docs-migrator/src/fern.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DocsV1Read } from "@fern-api/fdr-sdk";

/**
* The layout used for guides. This is the default layout.
* Guides are typically long-form content that is meant to be read from start to finish.
Expand Down Expand Up @@ -47,7 +49,7 @@ export interface FernDocsFrontmatter {
/**
* The URL to the page's image. This is used for the <meta property="og:image"> tag in the HTML.
*/
image?: string;
image?: string | DocsV1Read.FileIdOrUrl;

/**
* Full slug of the page.
Expand Down
2 changes: 2 additions & 0 deletions clis/docs-migrator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,14 @@ export class MigrateFromMintlify {
content,
};
}

return {
path: mintlify.path,
data: {
title: data.title,
subtitle: data.description,
layout: data.mode != null ? "reference" : undefined,
// TODO: (rohin) investigate this more; it seems like mintlify isn't reliant on fdr, so not sure if there's a transformation here.
image: data["og:image"],
slug,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/mdx/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface FernDocsFrontmatter extends DocsV1Read.MetadataConfig {
/**
* The URL to the page's image. This is used for the <meta property="og:image"> tag in the HTML.
*/
image?: string;
image?: string | DocsV1Read.FileIdOrUrl;

/**
* Renders an "Edit this page" link at the bottom of the page.
Expand Down
29 changes: 26 additions & 3 deletions packages/ui/app/src/next-app/utils/getSeoProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,32 @@ export function getSeoProps(
const { data: frontmatter } = getFrontmatter(page.markdown);
ogMetadata = { ...ogMetadata, ...frontmatter };

// retrofit og:image
if (frontmatter.image != null) {
ogMetadata["og:image"] ??= { type: "url", value: frontmatter.image };
// retrofit og:image, preferring og:image
// TODO: (rohin) Come back here and support more image transformations (twitter, logo, etc)
for (const frontmatterImageVar of [frontmatter.image, frontmatter["og:image"]]) {
if (frontmatterImageVar != null) {
// TODO: (rohin) remove string check when fully migrated, but keeping for back compat
if (typeof frontmatterImageVar === "string") {
ogMetadata["og:image"] ??= {
type: "url",
value: frontmatterImageVar,
};
} else {
visitDiscriminatedUnion(frontmatterImageVar, "type")._visit({
fileId: (fileId) => {
const realId = fileId.value.split(":")[1];
if (realId != null) {
fileId.value = realId;
ogMetadata["og:image"] = fileId;
}
},
url: (url) => {
ogMetadata["og:image"] = url;
},
_other: undefined,
});
}
}
}

seo.title ??= frontmatter.title;
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/docs-bundle/src/pages/api/fern-docs/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function toFeedItem(
try {
let image: string | undefined;

if (frontmatter.image != null) {
// TODO: (rohin) Clean up after safe deploy, but include for back compat
if (frontmatter.image != null && typeof frontmatter.image === "string") {
image = frontmatter.image;
} else if (frontmatter["og:image"] != null) {
image = toUrl(frontmatter["og:image"], files);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit af1c6ea

Please sign in to comment.