forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithBlogCrossLinks.tsx
More file actions
77 lines (64 loc) · 2.27 KB
/
withBlogCrossLinks.tsx
File metadata and controls
77 lines (64 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type { FC } from 'react';
import semver from 'semver';
import { getClientContext } from '#site/client-context';
import CrossLink from '#site/components/Common/CrossLink';
import getBlogData from '#site/next-data/blogData';
import type { BlogCategory } from '#site/types';
const extractVersionFromTitle = (title: string): string | null => {
const match = title.match(/v(\d+\.\d+\.\d+)/);
return match ? match[1] : null;
};
const WithBlogCrossLinks: FC = () => {
const { pathname } = getClientContext();
// Extracts from the static URL the components used for the Blog Post slug
const [, , category, postname] = pathname.split('/') as [
unknown,
unknown,
BlogCategory,
string,
];
const { posts } = getBlogData(category);
// Sort posts by semver for release category
const sortedPosts =
category === 'release'
? posts.toSorted((a, b) => {
const versionA = extractVersionFromTitle(a.title);
const versionB = extractVersionFromTitle(b.title);
if (versionA && versionB) {
// Sort by semver in descending order (newest first)
return semver.rcompare(versionA, versionB);
}
// Fallback to date sorting if version extraction fails
return b.date.getTime() - a.date.getTime();
})
: posts;
const currentItem = sortedPosts.findIndex(
({ slug }) => slug === `/blog/${category}/${postname}`
);
// For release posts sorted by semver (descending):
// - Previous should point to an older version (higher index)
// - Next should point to a newer version (lower index)
const [previousCrossLink, nextCrossLink] =
category === 'release'
? [sortedPosts[currentItem + 1], sortedPosts[currentItem - 1]]
: [sortedPosts[currentItem - 1], sortedPosts[currentItem + 1]];
return (
<div className="max-xs:grid-cols-1 mt-4 grid w-full grid-cols-2 gap-4">
{(previousCrossLink && (
<CrossLink
type="previous"
text={previousCrossLink.title}
link={previousCrossLink.slug}
/>
)) || <div />}
{nextCrossLink && (
<CrossLink
type="next"
text={nextCrossLink.title}
link={nextCrossLink.slug}
/>
)}
</div>
);
};
export default WithBlogCrossLinks;