Skip to content

Commit

Permalink
feat(cli): Parse logo from frontmatter (#5399)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbelkadi authored Dec 14, 2024
1 parent 01e4d03 commit a66123f
Show file tree
Hide file tree
Showing 214 changed files with 4,346 additions and 1 deletion.
22 changes: 22 additions & 0 deletions fern/pages/fern-docs/content/front-matter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ Controls the conditional rendering of the on-page feedback form at the bottom of
<img src="./on-page-feedback.png" alt="On-page feedback feature" />
</Frame>

## Page logo

**Name**: `logo`<br />
**Type**: object<br />
**Default**: None

You can override the site-wide logo for a page by setting a field named `logo` in any page's frontmatter like this:

<CodeBlock title='index.mdx logo example'>

```mdx
---
logo:
light: /path/to/page-logo.png
dark: /path/to/page-logo.png
---
```

</CodeBlock>

This field specifies the logo for this page. If no logo is set, the site-wide [logo configuration](/learn/docs/getting-started/global-configuration#logo-configuration) is used.

## SEO metadata

Fern also supports adding SEO-specific metadata in the frontmatter.
Expand Down
26 changes: 26 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
- changelogEntry:
- summary: |
The Fern CLI now supports parsing a `logo` option from your frontmatter. If
you would like to override logo on a specific page you can do so by adding
the following:
```markdown intro.mdx
---
logo: /path/to/my/logo
---
```
or
```markdown intro.mdx
---
logo:
light: /path/to/my/light/logo
dark: /path/to/my/dark/logo
---
```
type: feat
irVersion: 53
version: 0.46.2

- changelogEntry:
- summary: |
Add support for setting the `User-Agent` header value for Go generators.
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/docs-markdown-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"typescript": "4.6.4",
"vitest": "^2.0.5"
"vitest": "^2.0.5",
"zod": "^3.22.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,90 @@ describe("parseImagePaths", () => {
);
});

it("should parse logo from frontmatter text", () => {
const page = '---\nlogo: "path/to/image.png"\n---';
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual(["/Volume/git/fern/my/docs/folder/path/to/image.png"]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
`
"---
logo:
type: fileId
value: /Volume/git/fern/my/docs/folder/path/to/image.png
---"
`
);
});

it("should parse url logo from frontmatter text", () => {
const page = '---\nlogo: "https://someurl.com"\n---';
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual([]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
`
"---
logo:
type: url
value: 'https://someurl.com'
---"
`
);
});

it("should parse light and dark logo from frontmatter json", () => {
const page = '---\nlogo:\n light: "path/to/light-image.png"\n dark: "path/to/dark-image.png"\n---';
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual([
"/Volume/git/fern/my/docs/folder/path/to/light-image.png",
"/Volume/git/fern/my/docs/folder/path/to/dark-image.png"
]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
`
"---
logo:
light:
type: fileId
value: /Volume/git/fern/my/docs/folder/path/to/light-image.png
dark:
type: fileId
value: /Volume/git/fern/my/docs/folder/path/to/dark-image.png
---"
`
);
});

it("should parse light logo from frontmatter json", () => {
const page = '---\nlogo:\n light: "path/to/light-image.png"\n---';
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual(["/Volume/git/fern/my/docs/folder/path/to/light-image.png"]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
`
"---
logo:
light:
type: fileId
value: /Volume/git/fern/my/docs/folder/path/to/light-image.png
---"
`
);
});

it("should parse dark logo from frontmatter json", () => {
const page = '---\nlogo:\n dark: "path/to/dark-image.png"\n---';
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual(["/Volume/git/fern/my/docs/folder/path/to/dark-image.png"]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
`
"---
logo:
dark:
type: fileId
value: /Volume/git/fern/my/docs/folder/path/to/dark-image.png
---"
`
);
});

it("should parse the same result when run twice for frontmatter text", () => {
const page = '---\nimage: "path/to/image.png"\n---';
const result = parseImagePaths(page, PATHS);
Expand Down
49 changes: 49 additions & 0 deletions packages/cli/docs-markdown-utils/src/parseImagePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { mdxFromMarkdown } from "mdast-util-mdx";
import { mdx } from "micromark-extension-mdx";
import { visit } from "unist-util-visit";
import { parseMarkdownToTree } from "./parseMarkdownToTree";
import { z } from "zod";

interface AbsolutePathMetadata {
absolutePathToMdx: AbsoluteFilePath;
Expand Down Expand Up @@ -48,6 +49,7 @@ export function parseImagePaths(
}

visitFrontmatterImages(data, ["image", "og:image", "og:logo", "twitter:image"], mapImage);
replaceFrontmatterImagesforLogo(data, mapImage);

const tree = parseMarkdownToTree(content);

Expand Down Expand Up @@ -206,6 +208,7 @@ export function replaceImagePathsAndUrls(
}

visitFrontmatterImages(data, ["image", "og:image", "og:logo", "twitter:image"], mapImage);
replaceFrontmatterImagesforLogo(data, mapImage);

visit(tree, (node) => {
if (node.position == null) {
Expand Down Expand Up @@ -402,3 +405,49 @@ function visitFrontmatterImages(
}
}
}

const LogoOverrideFrontmatterSchema = z.union([
z.string(),
z.object({
light: z.string().optional(),
dark: z.string().optional()
})
]);

export function convertImageToFileIdOrUrl(
value: string,
mapImage: (image: string | undefined) => string | undefined
): CjsFdrSdk.docs.latest.FileIdOrUrl {
const mappedImage = mapImage(value);
return mappedImage
? {
type: "fileId",
value: CjsFdrSdk.FileId(mappedImage)
}
: {
type: "url",
value: CjsFdrSdk.Url(value)
};
}

function replaceFrontmatterImagesforLogo(
data: Record<string, any>,

Check warning on line 434 in packages/cli/docs-markdown-utils/src/parseImagePaths.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
mapImage: (image: string | undefined) => string | undefined
) {
const parsedValue = LogoOverrideFrontmatterSchema.safeParse(data.logo);
if (!parsedValue.success) {
return;
}
const parsedFrontmatterLogo = parsedValue.data;

if (typeof parsedFrontmatterLogo === "string") {
data.logo = convertImageToFileIdOrUrl(parsedFrontmatterLogo, mapImage);
} else {
if (parsedFrontmatterLogo.light != null) {
data.logo.light = convertImageToFileIdOrUrl(parsedFrontmatterLogo.light, mapImage);
}
if (parsedFrontmatterLogo.dark != null) {
data.logo.dark = convertImageToFileIdOrUrl(parsedFrontmatterLogo.dark, mapImage);
}
}
}
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.

26 changes: 26 additions & 0 deletions seed/ts-sdk/streaming/no-serde-layer/dist/Client.d.ts

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

17 changes: 17 additions & 0 deletions seed/ts-sdk/streaming/no-serde-layer/dist/Client.js

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

1 change: 1 addition & 0 deletions seed/ts-sdk/streaming/no-serde-layer/dist/api/index.d.ts

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

17 changes: 17 additions & 0 deletions seed/ts-sdk/streaming/no-serde-layer/dist/api/index.js

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.

Loading

0 comments on commit a66123f

Please sign in to comment.