Skip to content

Commit 71f582b

Browse files
committed
Add new & updated badges to functions in See Also
1 parent a79878f commit 71f582b

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-26
lines changed
Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
---
2-
import type { SeeAlsoLinkGroup } from '@src/utils/general';
2+
import type { SeeAlsoLinkGroup } from "@src/utils/general";
3+
import { Badge } from "@astrojs/starlight/components";
34
45
export interface Props {
56
seeAlsoLinks: SeeAlsoLinkGroup[];
67
currentId: string;
78
}
89
910
const { seeAlsoLinks, currentId } = Astro.props;
11+
12+
const badgeConfig: {
13+
[key: string]: {
14+
text: string;
15+
variant: "note" | "danger" | "success" | "caution" | "tip" | "default";
16+
};
17+
} = {
18+
new: { text: "New", variant: "success" },
19+
updated: { text: "Updated", variant: "caution" },
20+
};
1021
---
1122

12-
{seeAlsoLinks.length > 0 && (
13-
<div class="see-also-section" data-pagefind-ignore>
14-
<h4>See Also</h4>
15-
{seeAlsoLinks.map((group) => (
16-
<div class="see-also-group">
17-
<h5>{group.title}</h5>
18-
<ul>
19-
{group.links.map((link) => (
20-
<li>
21-
{link.name === currentId ? (
22-
<strong>{link.name.replace(/_/g, ' ')}</strong>
23-
) : (
24-
<a target={link.newTab ? '_blank' : '_self'}
25-
rel={link.newTab ? 'noopener noreferrer' : undefined}
26-
href={link.link}>{link.name.replace(/_/g, ' ')}</a>
27-
)}
28-
</li>
29-
))}
30-
</ul>
31-
</div>
32-
))}
33-
</div>
34-
)}
23+
{
24+
seeAlsoLinks.length > 0 && (
25+
<div class="see-also-section" data-pagefind-ignore>
26+
<h4>See Also</h4>
27+
{seeAlsoLinks.map((group) => (
28+
<div class="see-also-group">
29+
<h5>{group.title}</h5>
30+
<ul>
31+
{group.links.map((link) => (
32+
<li>
33+
{link.name === currentId ? (
34+
<strong>{link.name.replace(/_/g, " ")}</strong>
35+
) : (
36+
<a
37+
target={link.newTab ? "_blank" : "_self"}
38+
rel={link.newTab ? "noopener noreferrer" : undefined}
39+
href={link.link}
40+
>
41+
{link.name.replace(/_/g, " ")}
42+
</a>
43+
)}
44+
45+
{link.badge &&
46+
(() => {
47+
const config = badgeConfig[link.badge] ?? {
48+
text: link.badge,
49+
variant: "default",
50+
};
51+
52+
return (
53+
<Badge
54+
text={config.text}
55+
variant={config.variant}
56+
size="medium"
57+
style="font-weight: bold;"
58+
/>
59+
);
60+
})()}
61+
</li>
62+
))}
63+
</ul>
64+
</div>
65+
))}
66+
</div>
67+
)
68+
}

web/src/styles/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
padding-top: 1.5rem;
4141
}
4242

43+
.see-also-section .see-also-group ul li .sl-badge {
44+
margin-left: 0.5rem;
45+
}
46+
4347
:root[data-theme='dark'] .sl-badge {
4448
color: #fff;
4549
}

web/src/utils/general.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { marked } from 'marked';
22
import path from 'path';
3-
import { getFunctionsByCategory, getFunctionsByTypeByCategory } from '@src/utils/functions';
3+
import { getFunctionInfo, getFunctionsByCategory, getFunctionsByTypeByCategory } from '@src/utils/functions';
44
import { getEventsByCategory, getEventsByTypeByCategory } from '@src/utils/events';
55
import { getElementsByCategory, getElementCategory } from '@src/utils/elements';
66

@@ -36,6 +36,7 @@ type SeeAlsoLink = {
3636
name: string;
3737
link: string;
3838
newTab?: boolean;
39+
badge?: string;
3940
};
4041

4142
export type SeeAlsoLinkGroup = {
@@ -144,7 +145,22 @@ export function getSeeAlsoLinksFromList(seeAlsoList: string[]): SeeAlsoLinkGroup
144145

145146
const title = makeTitle(subType, category, type);
146147
if (!groupedMap.has(title)) groupedMap.set(title, []);
147-
const links = items.map((i: any) => ({ name: i.id, link: `/reference/${i.id}` }));
148+
const links = items.map((i: any) => {
149+
let badge: string | undefined;
150+
151+
if (i.collection === 'functions')
152+
{
153+
const funcInfo = getFunctionInfo(i.data);
154+
badge = funcInfo.version?.added ? `new` : (funcInfo.version?.updated ? `updated` : undefined);
155+
}
156+
157+
return {
158+
name: i.id,
159+
link: `/reference/${i.id}`,
160+
badge: badge,
161+
};
162+
});
163+
148164
groupedMap.get(title)!.push(...links);
149165
}
150166

0 commit comments

Comments
 (0)