feat(doc-viewer): show repository icons, star counts and version badge

- add monochrome GitHub and Gitee assets with light/dark CSS backgrounds
- replace the GitHub text link with icon links for both repositories
- fetch and cache repository star counts at build time and render them
  beside the icons when available
- abbreviate counts of 1000 or more as `Nk` and separate the navbar
  actions with vertical dividers
- move the version number from the navbar to a badge below the Home title
- harden the star script for cold checkouts: root-relative paths, request
  timeouts, cache fallbacks and CI coverage
This commit is contained in:
Liu
2026-11-21 20:53:30 +08:00
parent 331e07840a
commit 2c4958b427
15 changed files with 421 additions and 19 deletions
+6
View File
@@ -86,6 +86,12 @@ jobs:
- name: Install lcui-cli
run: npm install -g @lcui/cli
- name: Test repository star script
run: node --test scripts/fetch-repo-stars.test.js
- name: Generate repository star counts
run: node scripts/fetch-repo-stars.js
- name: Generate doc-viewer sources
working-directory: examples/doc-viewer
run: bun run compile
+2
View File
@@ -72,3 +72,5 @@ vsxmake*
compile_commands.json
docs/build/
docs/superpowers/plans
examples/doc-viewer/.cache/
examples/doc-viewer/app/components/repo-stars.generated.ts
+11 -1
View File
@@ -310,6 +310,10 @@ interface FieldData {
## 构建验证
```bash
# 在仓库根目录执行:生成 GitHub/Gitee star 数据(缓存 24 小时)
npm run doc-viewer:stars
# 以下在 examples/doc-viewer 目录执行
bun run compile # 全量编译 MDX → page.tsx
bun run compile --watch # 监听变更
npx lcui build app --force # 仅编译 app 目录,重生成 C 层 .tsx.h(不更新 main.h)
@@ -335,7 +339,13 @@ xmake build doc-viewer
构建或排查涉及 `@lcui/cli`、`@lcui/react`、`@lcui/fluent-icons` 的问题,以及运行 `npm install` / `npm ci` 前,必须遵循仓库根目录 `AGENTS.md` 中“lcui-toolkit 本地开发包与依赖安装”规则,核实本地开发版包的软链接;如果用户提供过本地仓库路径但当前包未链接,汇报并暂停,请用户手动修复。
项目根目录 `package.json` **没有** `build-doc-viewer` 脚本;可用的是 `npm run doc-viewer:compile-docs`(等价于 doc-viewer 的 `bun run compile`)与 `npm run doc-viewer:compile-tsx`(`lcui build --skip-xmake`),或手动执行上面的三步。脚本内容变更时,更新本节以保持实际命令与文档一致。
项目根目录 `package.json` **没有** `build-doc-viewer` 脚本;可用的是 `npm run doc-viewer:compile-docs`(先生成 star 数据,再执行 doc-viewer 的 `bun run compile`)与 `npm run doc-viewer:compile-tsx`(先生成 star 数据,再执行 `lcui build --force --skip-xmake`),或手动执行上面的三步。脚本内容变更时,更新本节以保持实际命令与文档一致。
> `npm run doc-viewer:compile-docs` / `doc-viewer:compile-tsx` 会先执行
> `npm run doc-viewer:stars`,生成 `app/components/repo-stars.generated.ts`
> (GitHub/Gitee star 数,缓存 24 小时,文件不提交)。直接运行 `lcui build`
> 前需要先执行一次该脚本,否则 navbar 会因缺少生成文件而编译失败;无网络时
> 生成 `null`,界面只显示图标。
> ⚠️ `bun run compile` 自动复制 `widget-fields/` 并触发页面生成;任何一步失败都会产生连锁错误。`lcui build` 输出末尾的 `unknown target(lcui) for doc-viewer.deps!` 警告无害,是已知问题,可忽略。
Binary file not shown.

After

Width:  |  Height:  |  Size: 512 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 685 B

@@ -7,6 +7,7 @@ import {
Scrollbar,
Text,
} from "@lcui/react";
import packageJson from "../../../../package.json";
type Locale = "zh-CN" | "en";
@@ -314,7 +315,12 @@ const HomeGallery: FunctionWidget = ({ locale }: { locale: Locale }) => {
<div className="home-page">
<div className="home-inner">
<div className="home-hero">
<Text className="home-title">LCUI</Text>
<div className="home-title-row">
<Text className="home-title">LCUI</Text>
<badge className="home-title-badge" variant="secondary">
v{packageJson.version}
</badge>
</div>
<Text className="home-desc">{heroDesc[locale]}</Text>
<div className="home-cta">
<RouterLink
+48 -10
View File
@@ -44,8 +44,8 @@
}
.navbar.compact .navbar-center,
.navbar.compact .navbar-version,
.navbar.compact .navbar-link {
.navbar.compact .navbar-link,
.navbar.compact .navbar-divider {
display: none;
}
@@ -64,13 +64,6 @@
@apply dark:text-muted-foreground-dark dark:bg-muted-dark dark:border-border-dark;
}
.navbar-version {
font-size: 13px;
@apply text-muted-foreground;
@apply dark:text-muted-foreground-dark;
font-family: monospace;
}
.navbar-link {
padding: 4px 8px;
font-size: 14px;
@@ -79,6 +72,52 @@
border-radius: 6px;
}
.navbar-icon-link {
display: inline-flex;
flex-direction: row;
align-items: center;
gap: 4px;
padding: 4px 6px;
}
.navbar-icon {
width: 16px;
height: 16px;
background-repeat: no-repeat;
background-position: center;
background-size: 16px 16px;
}
.navbar-icon-github {
background-image: url("../assets/github-mark-black.png");
}
.dark .navbar-icon-github {
background-image: url("../assets/github-mark-white.png");
}
.navbar-icon-gitee {
background-image: url("../assets/gitee-mark-black.png");
}
.dark .navbar-icon-gitee {
background-image: url("../assets/gitee-mark-white.png");
}
.navbar-icon-count {
font-size: 12px;
@apply text-muted-foreground;
@apply dark:text-muted-foreground-dark;
}
.navbar-divider {
flex: 0 0 1px;
width: 1px;
height: 16px;
@apply bg-border;
@apply dark:bg-border-dark;
}
.navbar-link:hover {
@apply bg-muted text-foreground;
@apply dark:bg-muted-dark dark:text-foreground-dark;
@@ -90,7 +129,6 @@
white-space: nowrap;
}
.navbar-version,
.navbar-link {
flex-shrink: 0;
white-space: nowrap;
+35 -5
View File
@@ -1,6 +1,13 @@
import { Button, Text } from "@lcui/react";
import { Button, Text, Widget } from "@lcui/react";
import "./navbar.css";
import packageJson from "../../../../package.json";
import { repoStars } from "./repo-stars.generated";
function formatStars(count: number): string {
if (count < 1000) {
return String(count);
}
return `${Math.floor(count / 1000)}k`;
}
export default function Navbar() {
return (
@@ -60,6 +67,7 @@ export default function Navbar() {
</dropdown-menu-radio-group>
</dropdown-menu-content>
</dropdown-menu>
<Widget className="navbar-divider" />
<Button
variant="ghost"
size="icon"
@@ -68,9 +76,31 @@ export default function Navbar() {
>
<icon name="dark-theme" />
</Button>
<Text className="navbar-version">{packageJson.version}</Text>
<a href="https://github.com/lc-soft/LCUI" className="navbar-link">
<Text>GitHub</Text>
<Widget className="navbar-divider" />
<a
href="https://github.com/lc-soft/LCUI"
className="navbar-link navbar-icon-link"
title="GitHub"
>
<Widget className="navbar-icon navbar-icon-github" />
{repoStars.github !== null && (
<Text className="navbar-icon-count">
{formatStars(repoStars.github)}
</Text>
)}
</a>
<Widget className="navbar-divider" />
<a
href="https://gitee.com/lc-soft/LCUI"
className="navbar-link navbar-icon-link"
title="Gitee"
>
<Widget className="navbar-icon navbar-icon-gitee" />
{repoStars.gitee !== null && (
<Text className="navbar-icon-count">
{formatStars(repoStars.gitee)}
</Text>
)}
</a>
</div>
</div>
+11
View File
@@ -945,6 +945,17 @@ li {
line-height: 1.4;
}
.home-title-row {
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
}
.home-title-badge {
white-space: nowrap;
}
.home-desc {
max-width: 720px;
margin-top: 12px;
+4 -2
View File
@@ -14,9 +14,11 @@
"format:check": "node scripts/clang-format-changed.js",
"check-theme": "node scripts/check-theme-css.js",
"test-postcss-dark": "node --test scripts/postcss-lcui-dark.test.js",
"test-repo-stars": "node --test scripts/fetch-repo-stars.test.js",
"build-styles": "lcui build src/styles/style-nova.css --force --skip-xmake",
"doc-viewer:compile-docs": "cd examples/doc-viewer && npm run compile",
"doc-viewer:compile-tsx": "cd examples/doc-viewer && lcui build --skip-xmake",
"doc-viewer:stars": "node scripts/fetch-repo-stars.js",
"doc-viewer:compile-docs": "npm run doc-viewer:stars && cd examples/doc-viewer && npm run compile",
"doc-viewer:compile-tsx": "npm run doc-viewer:stars && cd examples/doc-viewer && lcui build --force --skip-xmake",
"prepare": "husky"
},
"lint-staged": {
+1
View File
@@ -10,6 +10,7 @@ const sourceFiles = [
const generatedFiles = [
"src/styles/style-nova.css.h",
"examples/doc-viewer/app/global.css.h",
"examples/doc-viewer/app/components/navbar.css.h",
];
const colorPattern = /#[0-9a-fA-F]{3,8}\b|rgba?\(/g;
+144
View File
@@ -0,0 +1,144 @@
const fs = require("node:fs");
const path = require("node:path");
const HOUR = 60 * 60 * 1000;
const DAY = 24 * HOUR;
const SOURCES = {
github: "https://api.github.com/repos/lc-soft/LCUI",
gitee: "https://gitee.com/api/v5/repos/lc-soft/LCUI",
};
const ROOT = path.resolve(__dirname, "..");
const DEFAULT_CACHE = path.join(
ROOT,
"examples/doc-viewer/.cache/repo-stars.json"
);
const DEFAULT_OUT = path.join(
ROOT,
"examples/doc-viewer/app/components/repo-stars.generated.ts"
);
const REQUEST_TIMEOUT_MS = 10000;
function readCache(cachePath) {
try {
const data = JSON.parse(fs.readFileSync(cachePath, "utf8"));
return data && typeof data === "object" ? data : {};
} catch {
return {};
}
}
function isFresh(entry, now, maxAgeMs) {
return (
entry &&
typeof entry.count === "number" &&
typeof entry.fetchedAt === "number" &&
now - entry.fetchedAt < maxAgeMs
);
}
async function requestCount(key, fetchImpl) {
const response = await fetchImpl(SOURCES[key], {
headers: { "User-Agent": "lcui-doc-viewer" },
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
if (!response.ok) {
throw new Error(`request failed with status ${response.status}`);
}
const data = await response.json();
if (typeof data.stargazers_count !== "number") {
throw new Error("response has no stargazers_count");
}
return data.stargazers_count;
}
function writeGeneratedFile(outPath, result) {
const github = result.github ? result.github.count : null;
const gitee = result.gitee ? result.gitee.count : null;
const values = `{ github: ${github === null ? "null" : github}, gitee: ${gitee === null ? "null" : gitee} }`;
fs.mkdirSync(path.dirname(outPath), { recursive: true });
fs.writeFileSync(
outPath,
[
"// Generated by scripts/fetch-repo-stars.js",
"export const repoStars: { github: number | null; gitee: number | null } = " +
values +
";",
"",
].join("\n")
);
}
async function fetchRepoStars(options = {}) {
const cachePath = options.cachePath || DEFAULT_CACHE;
const outPath = options.outPath || DEFAULT_OUT;
const maxAgeMs = options.maxAgeMs ?? DAY;
const now = options.now ? options.now() : Date.now();
const fetchImpl = options.fetchImpl || fetch;
const cache = readCache(cachePath);
const result = {};
for (const key of Object.keys(SOURCES)) {
const entry = cache[key];
if (isFresh(entry, now, maxAgeMs)) {
result[key] = entry;
continue;
}
try {
result[key] = { count: await requestCount(key, fetchImpl), fetchedAt: now };
} catch (err) {
if (entry && typeof entry.count === "number") {
result[key] = { count: entry.count, fetchedAt: entry.fetchedAt };
} else {
result[key] = null;
}
console.warn(`[stars] ${key}: ${err.message}`);
}
}
writeGeneratedFile(outPath, result);
try {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
fs.writeFileSync(cachePath, JSON.stringify(result, null, 2) + "\n");
} catch (err) {
console.warn(`[stars] cannot write cache: ${err.message}`);
}
return result;
}
function parseArgs(argv) {
const options = {};
for (let i = 0; i < argv.length; ++i) {
switch (argv[i]) {
case "--max-age": {
const hours = Number(argv[++i]);
if (Number.isFinite(hours) && hours > 0) {
options.maxAgeMs = hours * HOUR;
} else {
console.warn(`[stars] ignoring invalid --max-age value`);
}
break;
}
case "--cache":
options.cachePath = argv[++i];
break;
case "--out":
options.outPath = argv[++i];
break;
default:
break;
}
}
return options;
}
if (require.main === module) {
fetchRepoStars(parseArgs(process.argv.slice(2))).catch((err) => {
console.warn(`[stars] ${err.message}`);
});
}
module.exports = { fetchRepoStars };
+152
View File
@@ -0,0 +1,152 @@
const test = require("node:test");
const assert = require("node:assert");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { fetchRepoStars } = require("./fetch-repo-stars");
const DAY = 24 * 60 * 60 * 1000;
function tempPaths() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "repo-stars-"));
return {
dir,
cachePath: path.join(dir, "repo-stars.json"),
outPath: path.join(dir, "repo-stars.generated.ts"),
};
}
function fakeFetch(counts) {
return async (url) => {
const key = url.includes("github.com") ? "github" : "gitee";
if (counts[key] === "fail") {
return { ok: false, status: 500, json: async () => ({}) };
}
return { ok: true, json: async () => ({ stargazers_count: counts[key] }) };
};
}
test("uses a fresh cache without requesting", async () => {
const p = tempPaths();
fs.writeFileSync(
p.cachePath,
JSON.stringify({
github: { count: 10, fetchedAt: 1000 },
gitee: { count: 20, fetchedAt: 1000 },
})
);
let calls = 0;
const result = await fetchRepoStars({
...p,
now: () => 2000,
maxAgeMs: DAY,
fetchImpl: async () => {
calls++;
return { ok: false, json: async () => ({}) };
},
});
assert.equal(calls, 0);
assert.equal(result.github.count, 10);
assert.equal(result.gitee.count, 20);
});
test("refreshes expired entries and writes the generated file", async () => {
const p = tempPaths();
fs.writeFileSync(
p.cachePath,
JSON.stringify({
github: { count: 10, fetchedAt: 0 },
gitee: { count: 20, fetchedAt: 0 },
})
);
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: fakeFetch({ github: 111, gitee: 222 }),
});
assert.equal(result.github.count, 111);
assert.equal(result.gitee.count, 222);
assert.match(fs.readFileSync(p.outPath, "utf8"), /github: 111/);
});
test("keeps the old value when one source fails", async () => {
const p = tempPaths();
fs.writeFileSync(
p.cachePath,
JSON.stringify({
github: { count: 10, fetchedAt: 0 },
gitee: { count: 20, fetchedAt: 0 },
})
);
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: fakeFetch({ github: 333, gitee: "fail" }),
});
assert.equal(result.github.count, 333);
assert.equal(result.gitee.count, 20);
});
test("writes null when there is no cache and the request fails", async () => {
const p = tempPaths();
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: fakeFetch({ github: "fail", gitee: "fail" }),
});
assert.equal(result.github, null);
assert.equal(result.gitee, null);
assert.match(fs.readFileSync(p.outPath, "utf8"), /github: null/);
});
test("recovers from a corrupt cache file", async () => {
const p = tempPaths();
fs.writeFileSync(p.cachePath, "{ not json");
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: fakeFetch({ github: 42, gitee: 43 }),
});
assert.equal(result.github.count, 42);
assert.equal(result.gitee.count, 43);
});
test("keeps a fresh value when the other source rejects", async () => {
const p = tempPaths();
fs.writeFileSync(
p.cachePath,
JSON.stringify({
github: { count: 10, fetchedAt: 9 * DAY },
gitee: { count: 20, fetchedAt: 0 },
})
);
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: async () => {
throw new Error("network down");
},
});
assert.equal(result.github.count, 10);
assert.equal(result.gitee.count, 20);
});
test("writes null when a request rejects and there is no cache", async () => {
const p = tempPaths();
const result = await fetchRepoStars({
...p,
now: () => 10 * DAY,
maxAgeMs: DAY,
fetchImpl: async () => {
throw new Error("network down");
},
});
assert.equal(result.github, null);
assert.equal(result.gitee, null);
assert.match(fs.readFileSync(p.outPath, "utf8"), /gitee: null/);
});