feat: add navbar caching, live GitHub releases edge API, and 1:1 Reims-Go desktop UI with mobile fidelity
This commit is contained in:
1 parent
84e0fed5b4
commit
4b1f8d76b5
35 files changed
+9253
-1409
No files matched your search
+166
-23
@@ -1,12 +1,99 @@
|
||||
import { writeFileSync, mkdirSync } from 'node:fs';
|
||||
import { mkdirSync, writeFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const DATA_DIR = resolve(process.cwd(), 'public/data');
|
||||
mkdirSync(DATA_DIR, { recursive: true });
|
||||
|
||||
const SKIP_SECTIONS = /^(下载|download|downloads|已知限制|known limitations|许可证|license|构建与许可证)/i;
|
||||
|
||||
function normalizeTitle(raw) {
|
||||
return String(raw || '')
|
||||
.replace(/<[^>]+>/g, '')
|
||||
.replace(/^#+\s*/, '')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function parseSections(markdown) {
|
||||
const lines = String(markdown || '').replace(/\r\n/g, '\n').split('\n');
|
||||
const sections = [];
|
||||
let current = null;
|
||||
let skipping = false;
|
||||
|
||||
for (const rawLine of lines) {
|
||||
const line = rawLine.trim();
|
||||
if (!line) continue;
|
||||
|
||||
const htmlHeading = line.match(/^<h[2-4][^>]*>(.*?)<\/h[2-4]>$/i);
|
||||
const mdHeading = !htmlHeading ? line.match(/^#{2,4}\s+(.+)$/) : null;
|
||||
|
||||
if (htmlHeading || mdHeading) {
|
||||
const title = normalizeTitle(htmlHeading ? htmlHeading[1] : mdHeading[1]);
|
||||
if (!title || SKIP_SECTIONS.test(title)) {
|
||||
skipping = true;
|
||||
current = null;
|
||||
} else {
|
||||
skipping = false;
|
||||
current = { title, items: [] };
|
||||
sections.push(current);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (skipping || /^\[中文\]\(#cn-/i.test(line) || line.startsWith('>')) continue;
|
||||
|
||||
const bullet = line.match(/^[-*]\s+(.+)$/);
|
||||
if (bullet && bullet[1].trim()) {
|
||||
if (!current) {
|
||||
current = { title: '', items: [] };
|
||||
sections.push(current);
|
||||
}
|
||||
current.items.push(bullet[1].trim());
|
||||
}
|
||||
}
|
||||
|
||||
return sections.filter((s) => s.items.length > 0);
|
||||
}
|
||||
|
||||
function parseBilingualBody(body) {
|
||||
const text = String(body || '').replace(/\r\n/g, '\n');
|
||||
if (!text.trim()) return { 'zh-CN': [], 'en-US': [] };
|
||||
|
||||
const cn = text.match(/<h[2-4][^>]*\bid=["']cn-[^"']*["'][^>]*>/i);
|
||||
const en = text.match(/<h[2-4][^>]*\bid=["']en-[^"']*["'][^>]*>/i);
|
||||
|
||||
if (cn && en && cn.index !== undefined && en.index !== undefined) {
|
||||
const cnChunk = cn.index < en.index ? text.slice(cn.index, en.index) : text.slice(cn.index);
|
||||
const enChunk = en.index > cn.index ? text.slice(en.index) : text.slice(en.index, cn.index);
|
||||
const zhSections = parseSections(cnChunk);
|
||||
const enSections = parseSections(enChunk);
|
||||
return {
|
||||
'zh-CN': zhSections.length ? zhSections : enSections,
|
||||
'en-US': enSections.length ? enSections : zhSections,
|
||||
};
|
||||
}
|
||||
|
||||
const single = parseSections(text);
|
||||
return { 'zh-CN': single, 'en-US': single };
|
||||
}
|
||||
|
||||
function classifyChannel(tag, prerelease) {
|
||||
const lower = tag.toLowerCase();
|
||||
if (lower === 'dev' || lower.includes('-dev')) return 'dev';
|
||||
if (prerelease || lower.includes('alpha') || lower.includes('beta') || lower.includes('rc')) return 'alpha';
|
||||
return 'stable';
|
||||
}
|
||||
|
||||
function isClientTag(tag) {
|
||||
if (!tag || /^runtime-/i.test(tag)) return false;
|
||||
return /^v\d+\.\d+/i.test(tag) || /^dev$/i.test(tag) || /^alpha/i.test(tag);
|
||||
}
|
||||
|
||||
async function fetchJson(url) {
|
||||
const res = await fetch(url, {
|
||||
headers: { 'User-Agent': 'Reims-Go-Landing-Sync/1.0' },
|
||||
headers: {
|
||||
Accept: 'application/vnd.github+json',
|
||||
'User-Agent': 'Reims-Go-Landing-Sync/2.0',
|
||||
},
|
||||
});
|
||||
if (!res.ok) throw new Error(`${res.status} ${url}`);
|
||||
return res.json();
|
||||
@@ -14,7 +101,7 @@ async function fetchJson(url) {
|
||||
|
||||
async function fetchText(url) {
|
||||
const res = await fetch(url, {
|
||||
headers: { 'User-Agent': 'Reims-Go-Landing-Sync/1.0' },
|
||||
headers: { 'User-Agent': 'Reims-Go-Landing-Sync/2.0' },
|
||||
});
|
||||
if (!res.ok) throw new Error(`${res.status} ${url}`);
|
||||
return res.text();
|
||||
@@ -22,39 +109,95 @@ async function fetchText(url) {
|
||||
|
||||
async function syncReleases() {
|
||||
try {
|
||||
const [repo, rel] = await Promise.all([
|
||||
const [repo, rawReleases] = await Promise.all([
|
||||
fetchJson('https://api.github.com/repos/Nwflower/Reims-Go'),
|
||||
fetchJson('https://api.github.com/repos/Nwflower/Reims-Go/releases/latest'),
|
||||
fetchJson('https://api.github.com/repos/Nwflower/Reims-Go/releases?per_page=25'),
|
||||
]);
|
||||
const assets = Array.isArray(rel.assets) ? rel.assets : [];
|
||||
const exeAsset = assets.find((a) => /windows-amd64\.exe$/i.test(a.name));
|
||||
const zipAsset = assets.find((a) => /windows-amd64\.zip$/i.test(a.name));
|
||||
const shaAsset = assets.find((a) => /^SHA256SUMS\.txt$/i.test(a.name));
|
||||
|
||||
const list = Array.isArray(rawReleases) ? rawReleases : [];
|
||||
const entries = [];
|
||||
let latestStableRaw = null;
|
||||
|
||||
for (const rel of list) {
|
||||
if (!rel || rel.draft) continue;
|
||||
const tag = String(rel.tag_name || '').trim();
|
||||
if (!isClientTag(tag)) continue;
|
||||
|
||||
const assets = Array.isArray(rel.assets) ? rel.assets : [];
|
||||
const exeAsset = assets.find((a) => /windows-amd64\.exe$/i.test(a.name));
|
||||
const zipAsset = assets.find((a) => /windows-amd64\.zip$/i.test(a.name));
|
||||
const shaAsset = assets.find((a) => /^SHA256SUMS\.txt$/i.test(a.name));
|
||||
const prerelease = Boolean(rel.prerelease);
|
||||
const channel = classifyChannel(tag, prerelease);
|
||||
const published = rel.published_at || rel.created_at || '';
|
||||
|
||||
if (channel === 'stable' && !latestStableRaw) {
|
||||
latestStableRaw = { rel, exeAsset, zipAsset, shaAsset };
|
||||
}
|
||||
|
||||
entries.push({
|
||||
tag,
|
||||
name: String(rel.name || `Reims-Go ${tag}`).trim(),
|
||||
published_at: published,
|
||||
date: published ? published.slice(0, 10) : '',
|
||||
html_url: rel.html_url || `https://github.com/Nwflower/Reims-Go/releases/tag/${encodeURIComponent(tag)}`,
|
||||
prerelease,
|
||||
channel,
|
||||
exe: exeAsset ? { name: exeAsset.name, size: exeAsset.size, url: exeAsset.browser_download_url } : null,
|
||||
zip: zipAsset ? { name: zipAsset.name, size: zipAsset.size, url: zipAsset.browser_download_url } : null,
|
||||
sha256_url: shaAsset?.browser_download_url || null,
|
||||
sections: parseBilingualBody(rel.body || ''),
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort((a, b) => {
|
||||
if (a.channel === 'stable' && b.channel !== 'stable') return -1;
|
||||
if (a.channel !== 'stable' && b.channel === 'stable') return 1;
|
||||
return (b.published_at || '').localeCompare(a.published_at || '');
|
||||
});
|
||||
|
||||
const top = latestStableRaw?.rel;
|
||||
const exeAsset = latestStableRaw?.exeAsset;
|
||||
const zipAsset = latestStableRaw?.zipAsset;
|
||||
const shaAsset = latestStableRaw?.shaAsset;
|
||||
|
||||
let sha256Text = '';
|
||||
if (shaAsset?.browser_download_url) {
|
||||
const digestLines = [exeAsset, zipAsset]
|
||||
.filter((a) => a?.name && a?.digest && /^sha256:[a-f0-9]{64}$/i.test(a.digest))
|
||||
.map((a) => `${a.digest.replace(/^sha256:/i, '')} ${a.name}`);
|
||||
if (digestLines.length > 0) {
|
||||
sha256Text = digestLines.join('\n');
|
||||
} else if (shaAsset?.browser_download_url) {
|
||||
try {
|
||||
sha256Text = await fetchText(shaAsset.browser_download_url);
|
||||
} catch {
|
||||
sha256Text = '';
|
||||
}
|
||||
}
|
||||
const payload = {
|
||||
synced_at: new Date().toISOString(),
|
||||
|
||||
const nowIso = new Date().toISOString();
|
||||
const latest = {
|
||||
synced_at: nowIso,
|
||||
stars: typeof repo.stargazers_count === 'number' ? repo.stargazers_count : 1600,
|
||||
version: rel.tag_name || 'v0.2.11',
|
||||
published_at: rel.published_at || '2026-12-01T00:00:00Z',
|
||||
html_url: rel.html_url || 'https://github.com/Nwflower/Reims-Go/releases/latest',
|
||||
exe: exeAsset
|
||||
? { name: exeAsset.name, size: exeAsset.size, url: exeAsset.browser_download_url }
|
||||
: null,
|
||||
zip: zipAsset
|
||||
? { name: zipAsset.name, size: zipAsset.size, url: zipAsset.browser_download_url }
|
||||
: null,
|
||||
version: top?.tag_name || entries[0]?.tag || 'v0.2.12',
|
||||
published_at: top?.published_at || nowIso,
|
||||
html_url: top?.html_url || 'https://github.com/Nwflower/Reims-Go/releases/latest',
|
||||
exe: exeAsset ? { name: exeAsset.name, size: exeAsset.size, url: exeAsset.browser_download_url } : null,
|
||||
zip: zipAsset ? { name: zipAsset.name, size: zipAsset.size, url: zipAsset.browser_download_url } : null,
|
||||
sha256_url: shaAsset?.browser_download_url || null,
|
||||
sha256_sums: sha256Text,
|
||||
};
|
||||
writeFileSync(resolve(DATA_DIR, 'releases.json'), JSON.stringify(payload, null, 2) + '\n', 'utf8');
|
||||
console.log(`[sync] releases.json updated: ${payload.version} (${payload.stars} stars)`);
|
||||
|
||||
const bundle = {
|
||||
source: 'static-fallback',
|
||||
synced_at: nowIso,
|
||||
latest,
|
||||
entries,
|
||||
};
|
||||
|
||||
writeFileSync(resolve(DATA_DIR, 'releases.json'), JSON.stringify(latest, null, 2) + '\n', 'utf8');
|
||||
writeFileSync(resolve(DATA_DIR, 'releases-bundle.json'), JSON.stringify(bundle, null, 2) + '\n', 'utf8');
|
||||
console.log(`[sync] releases.json & releases-bundle.json updated: ${latest.version} (${latest.stars} stars, ${entries.length} releases)`);
|
||||
} catch (err) {
|
||||
console.warn('[sync] skipped releases fetch, keeping local fallback:', err.message);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user