Skip to content

Commit 772d896

Browse files
authored
feat: Allow to have top level alias for platforms (#13069)
1 parent 1c467ab commit 772d896

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

docs/platforms/javascript/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
title: JavaScript
1+
title: Browser JavaScript
2+
platformTitle: JavaScript
23
caseStyle: camelCase
34
supportLevel: production
45
sdk: 'sentry.javascript.browser'

src/components/breadcrumbs/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ type BreadcrumbsProps = {
99
};
1010

1111
export function Breadcrumbs({leafNode}: BreadcrumbsProps) {
12-
const nodes: DocNode[] = [];
12+
const breadcrumbs: {title: string; to: string}[] = [];
13+
1314
for (let node: DocNode | undefined = leafNode; node; node = node.parent) {
1415
if (node && !node.missing) {
15-
nodes.unshift(node);
16+
const to = node.path === '/' ? node.path : `/${node.path}/`;
17+
const title = node.frontmatter.platformTitle ?? node.frontmatter.title;
18+
19+
breadcrumbs.unshift({
20+
to,
21+
title,
22+
});
1623
}
1724
}
1825

1926
return (
2027
<ul className="list-none flex p-0 flex-wrap" style={{margin: 0}}>
21-
{nodes.map(n => {
22-
const to = n.path === '/' ? n.path : `/${n.path}/`;
28+
{breadcrumbs.map(b => {
2329
return (
24-
<li className={styles['breadcrumb-item']} key={n.path}>
25-
<SmartLink to={to}>{n.frontmatter.title}</SmartLink>
30+
<li className={styles['breadcrumb-item']} key={b.to}>
31+
<SmartLink to={b.to}>{b.title}</SmartLink>
2632
</li>
2733
);
2834
})}

src/components/platformFilter/client.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function PlatformFilterClient({platforms}: {platforms: Platform[]}) {
6262
return platformsAndGuides;
6363
}
6464
// any of these fields can be used to match the search value
65-
const keys = ['title', 'aliases', 'name', 'sdk', 'keywords'];
65+
const keys = ['title', 'aliases', 'name', 'sdk', 'keywords', 'platformTitle'];
6666
const matches_ = matchSorter(platformsAndGuides, filter, {
6767
keys,
6868
threshold: rankings.CONTAINS,
@@ -195,7 +195,14 @@ function PlatformWithGuides({
195195

196196
const guides = useMemo(() => {
197197
const showPlatformInContent = matchKeys.includes(platform.key);
198-
return showPlatformInContent ? [platform, ...platform.guides] : platform.guides;
198+
199+
// This is the case if `platformTitle` is configured for a platform
200+
// In this case, we do not need to add the platform to the list of guides
201+
const hasGuideWithPlatformKey = platform.guides.some(g => g.key === platform.key);
202+
203+
return showPlatformInContent && !hasGuideWithPlatformKey
204+
? [platform, ...platform.guides]
205+
: platform.guides;
199206
}, [matchKeys, platform]);
200207

201208
return (

src/components/platformSelector/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export function PlatformSelector({
9292

9393
const router = useRouter();
9494
const onPlatformChange = (platformKey: string) => {
95-
const platform_ = platformsAndGuides.find(platform => platform.key === platformKey);
95+
const platform_ = platformsAndGuides.find(
96+
platform => platform.key === platformKey.replace('-redirect', '')
97+
);
9698
if (platform_) {
9799
localStorage.setItem('active-platform', platform_.key);
98100
router.push(platform_.url);
@@ -277,6 +279,11 @@ function PlatformItem({
277279
isLastGuide: i === guides.length - 1,
278280
}));
279281

282+
// This is the case if `platformTitle` is configured for a platform
283+
// In this case, the top-level select item should get the `-redirect` suffix,
284+
// as we can't have two items with the same key
285+
const hasGuideWithPlatformKey = platform.guides.some(g => g.key === platform.key);
286+
280287
const guides = platform.isExpanded
281288
? markLastGuide(platform.guides.length > 0 ? platform.guides : platform.integrations)
282289
: [];
@@ -288,7 +295,7 @@ function PlatformItem({
288295
<RadixSelect.Label className="flex">
289296
<Fragment>
290297
<RadixSelect.Item
291-
value={platform.key}
298+
value={hasGuideWithPlatformKey ? `${platform.key}-redirect` : platform.key}
292299
asChild
293300
className={styles.item}
294301
data-platform-with-guides

src/docTree.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function nodeToPlatform(n: DocNode): Platform {
259259
name: n.slug,
260260
type: 'platform',
261261
url: '/' + n.path + '/',
262-
title: n.frontmatter.title,
262+
title: n.frontmatter.platformTitle ?? n.frontmatter.title,
263263
caseStyle,
264264
sdk: n.frontmatter.sdk,
265265
fallbackPlatform: n.frontmatter.fallbackPlatform,
@@ -369,9 +369,20 @@ function extractGuides(platformNode: DocNode): PlatformGuide[] {
369369
if (!guidesNode) {
370370
return [];
371371
}
372-
return guidesNode.children
372+
373+
// If a `platformTitle` is defined, we add a virtual guide
374+
const defaultGuide = platformNode.frontmatter.platformTitle
375+
? {
376+
...nodeToGuide(platformNode.slug, platformNode),
377+
key: platformNode.slug,
378+
}
379+
: undefined;
380+
381+
const childGuides = guidesNode.children
373382
.filter(({path}) => !isVersioned(path))
374383
.map(n => nodeToGuide(platformNode.slug, n));
384+
385+
return defaultGuide ? [defaultGuide, ...childGuides] : childGuides;
375386
}
376387

377388
const extractIntegrations = (p: DocNode): PlatformIntegration[] => {

src/types/platform.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ export interface PlatformConfig {
118118
* Keywords used for search etc.
119119
*/
120120
keywords?: string[];
121+
/**
122+
* The title of the platform as it should be displayed in the sidebar.
123+
* In most cases, you do not need to define this, as the title is used.
124+
* However, in some cases - e.g. JavaScript - the Platform title (JavaScript) is different from the default guide (Browser JavaScript).
125+
*/
126+
platformTitle?: string;
121127
/**
122128
* Used to map a platform to a specific SDK as defined by the SDK registry.
123129
*/
@@ -130,7 +136,6 @@ export interface PlatformConfig {
130136
* Is this a first-party or third-party SDK?
131137
*/
132138
supportLevel?: PlatformSupportLevel;
133-
134139
/**
135140
* The human readable name of the platform.
136141
*/

0 commit comments

Comments
 (0)