Skip to content

Commit 38647b6

Browse files
Always use API fetched versions for syntax page generation
1 parent cef429d commit 38647b6

4 files changed

Lines changed: 47 additions & 58 deletions

File tree

env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare namespace App {
22
interface Locals {
33
syntaxesData: {
44
addons: string[],
5+
version: string,
56
types: string[],
67
},
78
}

src/components/syntaxes/filters/VersionFilter.astro

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,34 @@ interface VersionGroup {
66
versions: string[];
77
}
88
9-
// Fetch and organize versions
10-
let response: Response;
11-
if (Astro.params.id) { // generating nightly docs, skip version fetch
12-
// fake response
13-
response = {
14-
ok: false,
15-
} as Response;
16-
} else {
17-
response = await fetch('https://api.github.com/repos/SkriptLang/Skript/releases?per_page=100', {
18-
headers: {
19-
'Accept': 'application/vnd.github+json',
20-
'X-GitHub-Api-Version': '2022-11-28',
21-
'User-Agent': 'SkriptLang',
22-
},
9+
const versionPattern = /.*?(\d+\.\d+(?:\.\d+)?).*/i;
10+
const allVersions: string[] = (await getCollection("skriptReleasesJson"))
11+
.flatMap((entry: any) => {
12+
const matches = versionPattern.exec(entry.data.tag_name);
13+
return matches ? [matches[1]] : [];
2314
});
24-
}
25-
26-
let versionGroups: VersionGroup[] = [];
27-
28-
if (response.ok) {
29-
const versionPattern = /.*?(\d+\.\d+(?:\.\d+)?).*/i;
30-
const allVersions: string[] = (await response.json())
31-
.flatMap((entry: any) => {
32-
const matches = versionPattern.exec(entry.tag_name);
33-
return matches ? [matches[1]] : [];
34-
});
35-
36-
const uniqueVersions = [...new Set(allVersions)];
37-
38-
// Group by major version (e.g., "2.13")
39-
const groupMap = new Map<string, string[]>();
40-
uniqueVersions.forEach(version => {
41-
const parts = version.split('.');
42-
const major = `${parts[0]}.${parts[1]}`;
43-
if (!groupMap.has(major)) {
44-
groupMap.set(major, []);
45-
}
46-
groupMap.get(major)!.push(version);
47-
});
48-
49-
// Convert to array and sort
50-
versionGroups = Array.from(groupMap.entries())
51-
.map(([major, versions]) => ({
52-
major,
53-
versions: versions.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))
54-
}))
55-
.sort((a, b) => b.major.localeCompare(a.major, undefined, { numeric: true }));
56-
} else {
57-
// Fallback
58-
const syntaxes: any[] = (await getCollection("syntaxes"))
59-
.filter(syntax => syntax.id === Astro.params.id || (syntax.id === 'syntaxes' && !Astro.params.id))
60-
const currentMinor = parseInt(syntaxes[0].data.source.version.split(".")[1]);
61-
for (let minor = currentMinor; minor >= 0; minor--) {
62-
versionGroups.push({
63-
major: `2.${minor}`,
64-
versions: [`2.${minor}`]
65-
});
15+
const uniqueVersions = [...new Set(allVersions)];
16+
17+
// Group by major version (e.g., "2.13")
18+
const groupMap = new Map<string, string[]>();
19+
uniqueVersions.forEach(version => {
20+
const parts = version.split('.');
21+
const major = `${parts[0]}.${parts[1]}`;
22+
if (!groupMap.has(major)) {
23+
groupMap.set(major, []);
6624
}
67-
}
25+
groupMap.get(major)!.push(version);
26+
});
27+
28+
// Convert to array and sort
29+
let versionGroups: VersionGroup[] = Array.from(groupMap.entries())
30+
.map(([major, versions]) => ({
31+
major,
32+
versions: versions.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))
33+
}))
34+
.sort((a, b) => b.major.localeCompare(a.major, undefined, { numeric: true }));
35+
// For older docs (e.g. archives), remove newer versions
36+
versionGroups = versionGroups.slice(versionGroups.findIndex(group => Astro.locals.syntaxesData.version.startsWith(group.major)))
6837
6938
const INITIAL_VISIBLE_COUNT = 3;
7039
---

src/content.config.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineCollection, z } from 'astro:content';
1+
import { defineCollection } from 'astro:content';
22
import { glob } from 'astro/loaders';
33
import { docsLoader } from '@astrojs/starlight/loaders';
44
import { docsSchema } from '@astrojs/starlight/schema';
@@ -26,4 +26,22 @@ export const collections = {
2626
base: './src/assets/syntaxes/addons/',
2727
}),
2828
}),
29+
skriptReleasesJson: defineCollection({
30+
loader: async() =>
31+
fetch('https://api.github.com/repos/SkriptLang/Skript/releases?per_page=100', {
32+
headers: {
33+
'Accept': 'application/vnd.github+json',
34+
'X-GitHub-Api-Version': '2022-11-28',
35+
'User-Agent': 'SkriptLang',
36+
},
37+
}).then(response => {
38+
if (!response.ok) {
39+
throw new Error("Failed to fetch versions... aborting");
40+
}
41+
return response.json();
42+
}).then(json => json.map((release: any) => ({
43+
...release,
44+
id: release.id.toString(),
45+
})))
46+
}),
2947
};

src/pages/syntaxes/[...id].astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ if (currentVersion.endsWith(".0")) {
123123
124124
Astro.locals.syntaxesData = {
125125
addons: addons,
126+
version: currentVersion,
126127
types: syntaxes.filter(syntax => syntax.type === DocumentableType.TYPE).map(syntax => syntax.name),
127128
}
128129

0 commit comments

Comments
 (0)