Skip to content

Commit 68dc2de

Browse files
authored
Remove Page/Space label from BlockContentRef (#2374)
1 parent 81b372f commit 68dc2de

File tree

6 files changed

+84
-37
lines changed

6 files changed

+84
-37
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
["style \\=([^;]*);", "\"([^\"]*)\""],
88
["style \\=([^;]*);", "\\`([^\\`]*)\\`"]
99
],
10-
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"]
10+
"tailwindCSS.classAttributes": [
11+
"class",
12+
"className",
13+
"style",
14+
".*Style"
15+
]
1116
}

src/components/DocumentView/BlockContentRef.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
import { DocumentBlockContentRef } from '@gitbook/api';
22

3+
import { LogoIcon } from '@/components/icons';
34
import { Card, Emoji } from '@/components/primitives';
5+
import { getSpaceCustomization, ignoreAPIError } from '@/lib/api';
6+
import { ResolvedContentRef } from '@/lib/references';
47

58
import { BlockProps } from './Block';
69

710
export async function BlockContentRef(props: BlockProps<DocumentBlockContentRef>) {
811
const { block, context, style } = props;
9-
const kind = block?.data?.ref?.kind;
1012

1113
const resolved = await context.resolveContentRef(block.data.ref, {
1214
resolveAnchorText: true,
1315
});
16+
1417
if (!resolved) {
1518
return null;
1619
}
1720

21+
const kind = block?.data?.ref?.kind;
22+
if (resolved.active && kind === 'space') {
23+
return <SpaceRefCard {...props} resolved={resolved} />;
24+
}
25+
26+
return (
27+
<Card
28+
leadingIcon={resolved.emoji ? <Emoji code={resolved.emoji} style="text-xl" /> : null}
29+
href={resolved.href}
30+
title={resolved.text}
31+
style={style}
32+
/>
33+
);
34+
}
35+
36+
async function SpaceRefCard(props: { resolved: ResolvedContentRef } & BlockProps<DocumentBlockContentRef>) {
37+
const { context, style, resolved } = props;
38+
const spaceId = context.contentRefContext?.space.id;
39+
40+
if (!spaceId) { return null; }
41+
42+
const spaceCustomization = await ignoreAPIError(getSpaceCustomization(spaceId));
43+
const customFavicon = spaceCustomization?.favicon;
44+
const customEmoji = customFavicon && 'emoji' in customFavicon ? customFavicon.emoji : undefined;
45+
const customIcon = customFavicon && 'icon' in customFavicon ? customFavicon.icon : undefined;
46+
1847
return (
1948
<Card
20-
leadingIcon={resolved.emoji ? <Emoji code={resolved.emoji} /> : null}
49+
leadingIcon={<LogoIcon icon={customIcon} emoji={customEmoji} alt='' sizes={[{ width: 24 }]} style={['object-contain', 'size-6']} />}
2150
href={resolved.href}
22-
preTitle={kind}
2351
title={resolved.text}
2452
style={style}
2553
/>

src/components/Header/HeaderLogo.tsx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@gitbook/api';
99

1010
import { HeaderMobileMenu } from '@/components/Header/HeaderMobileMenu';
11+
import { LogoIcon } from '@/components/icons';
1112
import { Image } from '@/components/utils';
1213
import { absoluteHref } from '@/lib/links';
1314
import { tcls } from '@/lib/tailwind';
@@ -95,40 +96,11 @@ export function HeaderLogo(props: HeaderLogoProps) {
9596

9697
function LogoFallback(props: HeaderLogoProps) {
9798
const { parent, space, customization } = props;
98-
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
99-
99+
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : undefined;
100+
const customEmoji = 'emoji' in customization.favicon ? customization.favicon.emoji : undefined;
100101
return (
101102
<>
102-
<Image
103-
alt="Logo"
104-
sources={
105-
customIcon
106-
? {
107-
light: { src: customIcon.light, aspectRatio: '1' },
108-
dark: customIcon.dark
109-
? { src: customIcon.dark, aspectRatio: '1' }
110-
: null,
111-
}
112-
: {
113-
light: {
114-
src: absoluteHref('~gitbook/icon?size=medium&theme=light', true),
115-
size: { width: 256, height: 256 },
116-
},
117-
dark: {
118-
src: absoluteHref('~gitbook/icon?size=medium&theme=dark', true),
119-
size: { width: 256, height: 256 },
120-
},
121-
}
122-
}
123-
sizes={[
124-
{
125-
width: 32,
126-
},
127-
]}
128-
fetchPriority="high"
129-
style={['w-8', 'h-8', 'object-contain']}
130-
/>
131-
103+
<LogoIcon icon={customIcon} emoji={customEmoji} alt='' sizes={[{ width: 32 }]} style={['object-contain', 'size-8']} fetchPriority='high'/>
132104
<h1
133105
className={tcls(
134106
'text-pretty',

src/components/icons/LogoIcon.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CustomizationThemedURL } from "@gitbook/api";
2+
3+
import { Image } from '@/components/utils';
4+
import { absoluteHref } from "@/lib/links";
5+
6+
import { Emoji } from "../primitives";
7+
8+
9+
export function LogoIcon(props: { icon?: CustomizationThemedURL; emoji?: string } & Omit<React.ComponentProps<typeof Image>, 'sources'>) {
10+
const { icon, emoji, alt, ...imageProps } = props;
11+
12+
if (emoji && !icon) {
13+
return <Emoji code={emoji} style="text-xl" />;
14+
}
15+
16+
return <Image
17+
alt={alt ?? ''}
18+
sources={icon ? {
19+
light: {
20+
src: icon.light,
21+
aspectRatio: '1',
22+
},
23+
dark: {
24+
src: icon.dark,
25+
aspectRatio: '1',
26+
},
27+
} : {
28+
light: {
29+
src: absoluteHref('~gitbook/icon?size=medium&theme=light', true),
30+
size: { width: 256, height: 256 },
31+
},
32+
dark: {
33+
src: absoluteHref('~gitbook/icon?size=medium&theme=dark', true),
34+
size: { width: 256, height: 256 },
35+
},
36+
}}
37+
{...imageProps}
38+
/>;
39+
}

src/components/icons/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './types';
2+
export * from './LogoIcon';

src/lib/references.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getBlockById, getBlockTitle } from './document';
1515
import { gitbookAppHref, pageHref, PageHrefContext } from './links';
1616
import { getPagePath, resolvePageId } from './pages';
1717

18+
1819
export interface ResolvedContentRef {
1920
/** Text to render in the content ref */
2021
text: string;
@@ -164,6 +165,7 @@ export async function resolveContentRef(
164165
contentRef.space === space.id
165166
? space
166167
: await ignoreAPIError(getSpace(contentRef.space));
168+
167169
if (!targetSpace) {
168170
return {
169171
href: gitbookAppHref(`/s/${contentRef.space}`),
@@ -175,7 +177,7 @@ export async function resolveContentRef(
175177
return {
176178
href: targetSpace.urls.published ?? targetSpace.urls.app,
177179
text: targetSpace.title,
178-
active: true,
180+
active: true
179181
};
180182
}
181183

0 commit comments

Comments
 (0)