Skip to content

Commit e77355a

Browse files
committed
assert entry is inside a product folder
1 parent 3053853 commit e77355a

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/util/changelog.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { RSSFeedItem } from "@astrojs/rss";
2-
import { getCollection, getEntries, type CollectionEntry } from "astro:content";
2+
import {
3+
getCollection,
4+
getEntries,
5+
getEntry,
6+
type CollectionEntry,
7+
} from "astro:content";
38
import { entryToString } from "~/util/container";
49

510
import { unified, type PluggableList } from "unified";
@@ -22,20 +27,30 @@ export async function getChangelogs({
2227
entries = entries.filter((e) => filter(e));
2328
}
2429

25-
entries = entries.map((e) => {
26-
const slug = e.id.split("/").slice(1).join("/");
27-
const folder = e.id.split("/")[0];
28-
const product = { collection: "products", id: folder } as const;
30+
entries = await Promise.all(
31+
entries.map(async (e) => {
32+
const slug = e.id.split("/").slice(1).join("/");
33+
const folder = e.id.split("/")[0];
34+
const product = { collection: "products", id: folder } as const;
2935

30-
if (!e.data.products.some((p) => p.id === product.id)) {
31-
e.data.products.push(product);
32-
}
36+
const isValidProduct = await getEntry(product);
3337

34-
return {
35-
...e,
36-
id: slug,
37-
};
38-
});
38+
if (!isValidProduct) {
39+
throw new Error(
40+
`[getChangelogs] ${e.id} is not located inside a valid product folder (received ${folder})`,
41+
);
42+
}
43+
44+
if (!e.data.products.some((p) => p.id === product.id)) {
45+
e.data.products.push(product);
46+
}
47+
48+
return {
49+
...e,
50+
id: slug,
51+
};
52+
}),
53+
);
3954

4055
return entries.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
4156
}

0 commit comments

Comments
 (0)