Skip to content

Commit f9f8011

Browse files
authored
Add alt text support to card covers (#3715)
1 parent 63bf4db commit f9f8011

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

.changeset/proud-kings-know.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
'gitbook': patch
4+
---
5+
6+
Add alt text support to card covers

bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@
343343
"react-dom": "catalog:",
344344
},
345345
"catalog": {
346-
"@gitbook/api": "0.145.0",
346+
"@gitbook/api": "0.146.0",
347347
"@scalar/api-client-react": "^1.3.46",
348348
"@tsconfig/node20": "^20.1.6",
349349
"@tsconfig/strictest": "^2.0.6",
@@ -724,7 +724,7 @@
724724

725725
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
726726

727-
"@gitbook/api": ["@gitbook/api@0.145.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-pq+lqUPdvrVstpojs7uOimaNePg16uE1X2Dx7VDulqResmNp/FiV8a1i99vlYHhfhVA/U7i6wAN0iX4zi0/YZA=="],
727+
"@gitbook/api": ["@gitbook/api@0.146.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-gWcSCbN+9Zc/XOEk4t8v70kKyaVJQytHMnnstArr8av1YpHzZWEpVQCeQ20SnJvkvO5y+P7TCVxJCLG2ciT9SQ=="],
728728

729729
"@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"],
730730

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"catalog": {
4343
"@tsconfig/strictest": "^2.0.6",
4444
"@tsconfig/node20": "^20.1.6",
45-
"@gitbook/api": "0.145.0",
45+
"@gitbook/api": "0.146.0",
4646
"@scalar/api-client-react": "^1.3.46",
4747
"@types/react": "^19.0.0",
4848
"@types/react-dom": "^19.0.0",

packages/gitbook/src/components/DocumentView/InlineIcon.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DocumentInlineIcon, DocumentMarkColor } from '@gitbook/api';
1+
import type { DocumentInlineIcon } from '@gitbook/api';
22

33
import { tcls } from '@/lib/tailwind';
44
import { Icon, type IconName } from '@gitbook/icons';
@@ -7,14 +7,11 @@ import { textColorToStyle } from './utils/colors';
77

88
export async function InlineIcon(props: InlineProps<DocumentInlineIcon>) {
99
const { inline } = props;
10-
const icon = inline.data.icon as IconName;
11-
const color = inline.data.color
12-
? (inline.data.color as DocumentMarkColor['data']['text'])
13-
: undefined;
10+
const { color, icon } = inline.data;
1411

1512
return (
1613
<Icon
17-
icon={icon}
14+
icon={icon as IconName}
1815
className={tcls('inline size-[1em]', color ? textColorToStyle[color] : null)}
1916
/>
2017
);

packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ export async function RecordCard(
8585
light: {
8686
src: lightCover.href,
8787
size: lightCover.file?.dimensions,
88+
alt: light.alt,
8889
},
8990
dark: darkCover
9091
? {
9192
src: darkCover.href,
9293
size: darkCover.file?.dimensions,
94+
alt: dark.alt,
9395
}
9496
: null,
9597
}}

packages/gitbook/src/components/DocumentView/Table/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export function getRecordValue<T extends number | string | boolean | string[] |
2121
return record.values[definitionId];
2222
}
2323

24+
type RecordCardCover = {
25+
contentRef: ContentRefFile | ContentRefURL | null;
26+
objectFit?: CardsImageObjectFit;
27+
alt?: string;
28+
};
29+
2430
/**
2531
* Get the covers for a record card.
2632
* Returns both the light and dark covers with their content refs and optional object fit.
@@ -31,10 +37,7 @@ export function getRecordCardCovers(
3137
record: DocumentTableRecord,
3238
view: DocumentTableViewCards
3339
): {
34-
[key in 'light' | 'dark']: {
35-
contentRef: ContentRefFile | ContentRefURL | null;
36-
objectFit?: CardsImageObjectFit;
37-
};
40+
[key in 'light' | 'dark']: RecordCardCover;
3841
} {
3942
const lightValue = view.coverDefinition
4043
? (getRecordValue(record, view.coverDefinition) as DocumentTableImageRecord | string[])
@@ -53,10 +56,9 @@ export function getRecordCardCovers(
5356
/**
5457
* Process a cover value and return the content ref and object fit.
5558
*/
56-
function processCoverValue(value: DocumentTableImageRecord | string[] | null | undefined): {
57-
contentRef: ContentRefFile | ContentRefURL | null;
58-
objectFit?: CardsImageObjectFit;
59-
} {
59+
function processCoverValue(
60+
value: DocumentTableImageRecord | string[] | null | undefined
61+
): RecordCardCover {
6062
if (!value) {
6163
return { contentRef: null };
6264
}
@@ -78,6 +80,7 @@ function processCoverValue(value: DocumentTableImageRecord | string[] | null | u
7880
return {
7981
contentRef: imageValue.ref,
8082
objectFit: imageValue.objectFit,
83+
alt: imageValue.alt,
8184
};
8285
}
8386

packages/gitbook/src/components/utils/Image.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ import type { PolymorphicComponentProp } from './types';
88

99
export type ImageSize = { width: number; height: number };
1010

11-
type ImageSource = {
11+
type DefaultImageSource = {
1212
src: string;
13-
size?: ImageSize;
1413
aspectRatio?: string;
14+
/**
15+
* Override the alt attribute.
16+
*/
17+
alt?: string;
1518
};
1619

17-
export type ImageSourceSized = {
18-
src: string;
20+
type ImageSource = DefaultImageSource & {
21+
size?: ImageSize;
22+
};
23+
24+
export type ImageSourceSized = DefaultImageSource & {
1925
size: ImageSize | null;
20-
aspectRatio?: string;
2126
};
2227

2328
export type ImageResponsiveSize = {
@@ -105,7 +110,7 @@ export function Image(
105110
} & ImageCommonProps
106111
>
107112
) {
108-
const { sources, style, inline = false, ...rest } = props;
113+
const { sources, style, inline = false, alt, ...rest } = props;
109114

110115
return (
111116
<>
@@ -119,6 +124,7 @@ export function Image(
119124
sources.dark ? 'dark:hidden' : null,
120125
style
121126
)}
127+
alt={sources.light.alt || alt}
122128
/>
123129
{sources.dark ? (
124130
<ImagePicture
@@ -134,6 +140,7 @@ export function Image(
134140
inline ? 'dark:inline' : 'dark:block',
135141
style
136142
)}
143+
alt={sources.dark.alt || sources.light.alt || alt}
137144
/>
138145
) : null}
139146
</>

0 commit comments

Comments
 (0)