Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 1b0ef55

Browse files
committed
Images in api with full path
1 parent 590f3b3 commit 1b0ef55

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

gatsby-node.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
blogAuthorsApiAdapter,
1010
blogPostDetailsApiAdapter,
1111
blogPostsApiAdapter,
12-
projectsApiAdapter
12+
projectsApiAdapter,
1313
} from "./src/logic/api/api-adapters";
1414

1515
export const createPages: GatsbyNode["createPages"] = async ({
@@ -168,7 +168,10 @@ export const onPostBuild: GatsbyNode["onPostBuild"] = async ({ graphql }) => {
168168
blogPostsQuery,
169169
authorsApi,
170170
);
171-
const blogPostDetailApis = blogPostDetailsApiAdapter(blogPostsQuery);
171+
const blogPostDetailApis = blogPostDetailsApiAdapter(
172+
blogPostsQuery,
173+
authorsApi,
174+
);
172175
const projectsApi = projectsApiAdapter(imagesApiQuery);
173176
const artApi = artApiAdapter(imagesApiQuery);
174177

src/logic/api/api-adapters.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
ProjectApi,
1010
ProjectsApi,
1111
} from "./api-model";
12-
import { blogAuthors } from "../blog-authors";
12+
import { BlogAuthor, blogAuthors } from "../blog-authors";
1313
import { projects } from "../projects";
1414
import { artDescriptions } from "../art";
15+
import config from "../../../gatsby-config";
1516

1617
const postResourceNameForEndpointCreator = (slug: string) =>
1718
slug!
@@ -35,7 +36,8 @@ export const blogPostsApiAdapter = (
3536
description: frontmatter.description!,
3637
date: frontmatter.date!,
3738
readingTime: node.fields!.readingTime!.text!,
38-
featuredImageUrl: frontmatter.image!.publicURL!,
39+
featuredImageUrl: `${config.siteMetadata!.siteUrl}${frontmatter.image!
40+
.publicURL!}`,
3941
authors: authorsApi.authors.filter((it) =>
4042
frontmatter.authors!.find(
4143
(author) =>
@@ -48,7 +50,7 @@ export const blogPostsApiAdapter = (
4850
apiBasePath,
4951
node.fields!.slug!,
5052
),
51-
webEndpoint: node.fields!.slug!,
53+
webEndpoint: `${config.siteMetadata!.siteUrl}${node.fields!.slug!}`,
5254
};
5355
});
5456

@@ -59,6 +61,7 @@ export const blogPostsApiAdapter = (
5961

6062
export const blogPostDetailsApiAdapter = (
6163
blogPostsApiQueryResult: Queries.BlogPostsApiQuery,
64+
authorsApi: BlogAuthorsApi,
6265
): Record<string, BlogPostDetailApi> =>
6366
blogPostsApiQueryResult!.allMarkdownRemark.edges.reduce<
6467
Record<string, BlogPostDetailApi>
@@ -69,8 +72,15 @@ export const blogPostDetailsApiAdapter = (
6972
description: frontmatter.description!,
7073
date: frontmatter.date!,
7174
readingTime: edge.node.fields!.readingTime!.text!,
72-
featuredImageUrl: frontmatter.image!.publicURL!,
73-
authors: frontmatter.authors!.map((it) => it!),
75+
featuredImageUrl: `${config.siteMetadata!.siteUrl}${frontmatter.image!
76+
.publicURL!}`,
77+
authors: authorsApi.authors.filter((it) =>
78+
frontmatter.authors!.find(
79+
(author) =>
80+
author!.split("_").join(" ").toUpperCase() ===
81+
it.name.toUpperCase(),
82+
),
83+
),
7484
tags: frontmatter.tags!.map((it) => it!),
7585
math: frontmatter.math!,
7686
content: edge.node!.html!,
@@ -81,14 +91,17 @@ export const blogPostDetailsApiAdapter = (
8191
export const blogAuthorsApiAdapter = (
8292
authorsImages: Queries.ImagesApiQuery,
8393
): BlogAuthorsApi => {
84-
const authors: BlogAuthorApi[] = Object.values(blogAuthors).map((author) => ({
85-
name: author.name,
86-
url: author.url,
87-
imageUrl: authorsImages.allFile.edges.find(
94+
const getImageUrl = (author: BlogAuthor) =>
95+
authorsImages.allFile.edges.find(
8896
(authorImage) =>
8997
authorImage.node.name ===
9098
author.name!.split(" ").join("-").toLowerCase(),
91-
)?.node?.publicURL,
99+
)?.node?.publicURL;
100+
101+
const authors: BlogAuthorApi[] = Object.values(blogAuthors).map((author) => ({
102+
name: author.name,
103+
url: author.url,
104+
imageUrl: `${config.siteMetadata!.siteUrl}${getImageUrl(author)}`,
92105
}));
93106
return {
94107
authors,
@@ -101,6 +114,10 @@ export const projectsApiAdapter = (
101114
const projectsApi: ProjectApi[] = Object.keys(projects).map((projectKey) => {
102115
const project = projects[projectKey];
103116

117+
const getImageUrl = () =>
118+
images.allFile.edges.find((image) => image.node.name === projectKey)!
119+
.node!.publicURL;
120+
104121
return {
105122
id: projectKey,
106123
name: project.name,
@@ -110,9 +127,7 @@ export const projectsApiAdapter = (
110127
text: callToAction.label,
111128
url: callToAction.link,
112129
})),
113-
imageUrl: images.allFile.edges.find(
114-
(authorImage) => authorImage.node.name === projectKey,
115-
)!.node!.publicURL,
130+
imageUrl: `${config.siteMetadata!.siteUrl}${getImageUrl()}`,
116131
};
117132
});
118133

@@ -125,12 +140,14 @@ export const artApiAdapter = (images: Queries.ImagesApiQuery): ArtApi => {
125140
const projectsApi: DrawingApi[] = Object.keys(artDescriptions).map((date) => {
126141
const description = artDescriptions[date];
127142

143+
const getImageUrl = () =>
144+
images.allFile.edges.find((drawImage) => drawImage.node.name === date)!
145+
.node!.publicURL!;
146+
128147
return {
129148
date,
130149
description,
131-
imageUrl: images.allFile.edges.find(
132-
(drawImage) => drawImage.node.name === date,
133-
)!.node!.publicURL!,
150+
imageUrl: `${config.siteMetadata!.siteUrl}${getImageUrl()}`,
134151
};
135152
});
136153

src/logic/api/api-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface BlogPostDetailApi {
44
date: string;
55
readingTime: string;
66
featuredImageUrl: string;
7-
authors: string[];
7+
authors: BlogAuthorApi[];
88
tags: string[];
99
math: boolean;
1010
content: string;

0 commit comments

Comments
 (0)