Skip to content

Commit 1b5368f

Browse files
authored
Merge pull request #1654 from lyj715824/bugfix/superteam
fix(docs): load VitePress preview assets at runtime
2 parents fa4a407 + 9a2c15f commit 1b5368f

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

docs/.vitepress/examples.data.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Build-time data loader: reads the community workflow examples under examples/
22
// and exposes their metadata to the docs site gallery.
3-
import { readFileSync } from "node:fs";
3+
import { existsSync, readFileSync } from "node:fs";
44
import { basename, dirname } from "node:path";
55

66
export interface ExampleMeta {
@@ -15,17 +15,12 @@ export interface ExampleMeta {
1515
event: string;
1616
repoPath: string; // e.g. examples/history-knowledge-qa
1717
hasPreview: boolean;
18-
previewUrl: string;
18+
previewPath: string;
1919
}
2020

2121
declare const data: ExampleMeta[];
2222
export { data };
2323

24-
const previewAssets = import.meta.glob("../../examples/*/preview.png", {
25-
eager: true,
26-
import: "default"
27-
}) as Record<string, string>;
28-
2924
function parseFrontmatter(md: string): Record<string, string | string[]> | null {
3025
const m = md.match(/^---\r?\n([\s\S]*?)\r?\n---/);
3126
if (!m) return null;
@@ -64,6 +59,8 @@ export default {
6459
if (id === "TEMPLATE") continue;
6560
const fm = parseFrontmatter(readFileSync(file, "utf8"));
6661
if (!fm || !fm.id) continue;
62+
const previewPath = `${dirname(file)}/preview.png`;
63+
const hasPreview = existsSync(previewPath);
6764
examples.push({
6865
id: String(fm.id),
6966
title: String(fm.title ?? fm.id),
@@ -75,8 +72,8 @@ export default {
7572
dslVersion: String(fm.dslVersion ?? ""),
7673
event: String(fm.event ?? ""),
7774
repoPath: `examples/${id}`,
78-
hasPreview: Boolean(previewAssets[`../../examples/${id}/preview.png`]),
79-
previewUrl: previewAssets[`../../examples/${id}/preview.png`] ?? ""
75+
hasPreview,
76+
previewPath: hasPreview ? `examples/${id}/preview.png` : ""
8077
});
8178
}
8279
return examples.sort((a, b) => a.category.localeCompare(b.category) || a.title.localeCompare(b.title));

docs/.vitepress/theme/ExamplesGallery.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { computed, ref } from "vue";
33
import { useData } from "vitepress";
44
import { data as examples } from "../examples.data";
55
6+
const previewAssets = import.meta.glob("../../../examples/*/preview.png", {
7+
eager: true,
8+
import: "default",
9+
query: "?url"
10+
}) as Record<string, string>;
11+
12+
const previewUrlsById = Object.fromEntries(
13+
Object.entries(previewAssets).map(([path, url]) => {
14+
const segments = path.split("/");
15+
return [segments[segments.length - 2], url];
16+
})
17+
);
18+
619
const REPO = "https://github.com/iflytek/astron-agent";
720
const { lang } = useData();
821
const isZh = computed(() => lang.value === "zh-CN");
@@ -38,6 +51,8 @@ const hideBrokenPreview = (id: string): void => {
3851
failedPreviews.value = new Set(failedPreviews.value).add(id);
3952
};
4053
54+
const getPreviewUrl = (id: string): string => previewUrlsById[id] ?? "";
55+
4156
const contributeHref = computed(() => (isZh.value ? "/zh/contribute-to-docs" : "/contribute-to-docs"));
4257
</script>
4358

@@ -70,7 +85,7 @@ const contributeHref = computed(() => (isZh.value ? "/zh/contribute-to-docs" : "
7085
<img
7186
v-if="e.hasPreview && !failedPreviews.has(e.id)"
7287
class="exg__preview"
73-
:src="e.previewUrl"
88+
:src="getPreviewUrl(e.id)"
7489
:alt="`${e.title} workflow preview`"
7590
loading="lazy"
7691
@error="hideBrokenPreview(e.id)"

0 commit comments

Comments
 (0)