Skip to content

Commit d7bc405

Browse files
authored
Upgrade Astro to v5 (#51)
* chore: upgrade Astro to v5 * fix: Astro v5 deprecated functionality
1 parent 114df16 commit d7bc405

File tree

7 files changed

+1267
-1234
lines changed

7 files changed

+1267
-1234
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
"astro": "astro"
1212
},
1313
"dependencies": {
14-
"@astrojs/mdx": "^3.1.2",
14+
"@astrojs/mdx": "^4.3.0",
1515
"@fortawesome/fontawesome-free": "^6.5.2",
1616
"@types/lodash": "^4.17.6",
17-
"astro": "^4.11.5",
17+
"astro": "^5.8.0",
1818
"autoprefixer": "^10.4.19",
1919
"axios": "^1.7.2",
2020
"dayjs": "^1.11.11",

pnpm-lock.yaml

Lines changed: 1220 additions & 1197 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/common/Search.astro

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
---
2+
import { render } from "astro:content";
3+
import { getCollection } from "astro:content";
24
import type { searchItem } from "~/services/search";
35
import { searchTerms } from "~/services/search";
46
5-
const getContentStaticURL = (path: string, target?: string): string | null => {
6-
const rgx = /\/src\/.+?\/(.+)\..*?/g;
7+
/**
8+
* Infers the static URL of a wiki article from its file path.
9+
* @param path filepath of article
10+
* @param target optional target heading
11+
*/
12+
const getContentStaticURL = (path: string, target?: string): string => {
13+
const rgx = /src\/.+?\/(.+)\..*?/g;
14+
15+
console.log(path);
716
817
const result = rgx.exec(path);
9-
if (result === null) return null;
18+
if (result === null) throw new Error(`Failed to infer static URL of ${path}`);
1019
1120
if (target) {
1221
return `/${result[1]}/#${target}`;
@@ -15,38 +24,36 @@ const getContentStaticURL = (path: string, target?: string): string | null => {
1524
}
1625
};
1726
18-
let searchTags: searchItem[] = [];
27+
const searchTags: searchItem[] = [...searchTerms];
1928
20-
if (searchTags.length === 0) {
21-
const pages = await Astro.glob("../../content/**/*.mdx");
29+
if (searchTags.length === searchTerms.length) {
30+
const pages = await getCollection("wiki");
2231
2332
for (const page of pages) {
24-
const frontmatter = page.frontmatter;
25-
const headings = page.getHeadings();
26-
27-
const href = getContentStaticURL(page.file);
28-
if (!href) {
29-
throw new Error(`Failed to get static url of ${page.url}`);
33+
if (!page.filePath) {
34+
throw new Error(`Page ${page.id} does not have a file path}`);
3035
}
3136
32-
// Add article title to search options
33-
searchTags.push({ text: page.frontmatter.title, href });
37+
const { headings } = await render(page);
38+
39+
const href = getContentStaticURL(page.filePath);
40+
41+
// Add article title to search options
42+
searchTags.push({ text: page.data.title, href });
3443
3544
// Add custom article tags to search options
36-
for (const tag of frontmatter.tags ?? []) {
45+
for (const tag of page.data.tags ?? []) {
3746
searchTags.push({ text: tag, href });
3847
}
3948
4049
// Add article headings to search options
4150
for (const heading of headings) {
42-
const href = getContentStaticURL(page.file, heading.slug);
43-
if (!href) {
44-
throw new Error(`Failed to get static url of ${page.url}`);
45-
}
46-
searchTags.push({ text: heading.text, href });
51+
searchTags.push({
52+
text: heading.text,
53+
href: getContentStaticURL(page.filePath, heading.slug),
54+
});
4755
}
4856
}
49-
searchTags = searchTags.concat(searchTerms);
5057
}
5158
---
5259

src/content/config.ts renamed to src/content.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { glob } from "astro/loaders";
12
import { defineCollection, z } from "astro:content";
23

34
const wikiArticleCollection = defineCollection({
4-
type: "content",
5+
loader: glob({pattern: '**/[^_]*.{md,mdx}', base: "./src/content/wiki" }),
56
schema: z.object({
67
title: z.string(),
78
description: z.string(),

src/layouts/WikiArticle.astro

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const articles = await getCollection("wiki");
1414
export const getStaticPaths = async () => {
1515
const articles = await getCollection("wiki");
1616
return articles.map((entry) => {
17-
return { params: { slug: entry.slug }, props: { entry } };
17+
return { params: { slug: entry.id }, props: { entry } };
1818
});
1919
};
2020
@@ -41,12 +41,12 @@ export interface Props {
4141
articles
4242
.sort((a, b) => a.data.title.localeCompare(b.data.title))
4343
.map((article) =>
44-
article.slug == hereSlug ? (
45-
<a href={`/wiki/${article.slug}/`} class="here">
44+
article.id == hereSlug ? (
45+
<a href={`/wiki/${article.id}/`} class="here">
4646
{article.data.title}
4747
</a>
4848
) : (
49-
<a href={`/wiki/${article.slug}/`}>{article.data.title}</a>
49+
<a href={`/wiki/${article.id}/`}>{article.data.title}</a>
5050
)
5151
)
5252
}
@@ -73,15 +73,15 @@ export interface Props {
7373
<a
7474
class="edit-page"
7575
target="_blank"
76-
href={`https://github.com/LuaLS/LuaLS.github.io/tree/main/src/content/wiki/${article.slug}.mdx`}
76+
href={`https://github.com/LuaLS/LuaLS.github.io/tree/main/src/content/wiki/${article.id}.mdx`}
7777
>
7878
<Icon name="pencil" group="solid" /> Edit this page
7979
</a>
8080
<a
8181
class="open-issue"
8282
target="_blank"
8383
href={`https://github.com/LuaLS/LuaLS.github.io/issues/new?title=${encodeURIComponent(
84-
`Issue with wiki page "${article.slug}"`
84+
`Issue with wiki page "${article.id}"`
8585
)}`}
8686
>
8787
<Icon name="circle-dot" group="solid" /> Open Issue

src/pages/wiki/[...slug].astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ import H6 from "~/components/wiki/headings/H6.astro";
1414
import dayjs from "~/services/time";
1515
import Tooltip from "~/components/common/Tooltip.astro";
1616
import Time from "~/components/common/Time.astro";
17+
import { render } from "astro:content";
1718
1819
export const getStaticPaths = async () => {
1920
const articles = await getCollection("wiki");
2021
return articles.map((article) => {
21-
return { params: { slug: article.slug }, props: { article } };
22+
return { params: { slug: article.id }, props: { article } };
2223
});
2324
};
2425
2526
const { article } = Astro.props;
2627
27-
const { Content, headings, remarkPluginFrontmatter } = await article.render();
28+
const { Content, headings, remarkPluginFrontmatter } = await render(article);
2829
2930
export interface Props {
3031
article: CollectionEntry<"wiki">;

src/pages/wiki/index.astro

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fetch from "~/util/fetch";
77
import ExternalLink from "~/components/common/ExternalLink.astro";
88
99
import { Image } from "astro:assets";
10+
import { render } from "astro:content";
1011
1112
const CONTRIBUTOR_COUNT = 30;
1213
@@ -15,14 +16,14 @@ const allWikiArticles = (await getCollection("wiki")).sort((a, b) =>
1516
);
1617
await Promise.all(
1718
allWikiArticles.map((article) =>
18-
article.render().then((r) => {
19+
render(article).then((r) => {
1920
article.data.lastModified = r.remarkPluginFrontmatter.lastModified;
2021
})
2122
)
2223
);
2324
const articleCount = allWikiArticles.length;
2425
const wordCount = allWikiArticles.reduce(
25-
(x, article) => x + Array.from(article.body.matchAll(/\w+/g)).length,
26+
(x, article) => x + Array.from((article.body ?? "").matchAll(/\w+/g)).length,
2627
0
2728
);
2829
const gettingStartedArticles = allWikiArticles.filter(
@@ -101,7 +102,7 @@ if (!contributors) {
101102
{
102103
gettingStartedArticles.map((article) => (
103104
<div class="article">
104-
<a href={`/wiki/${article.slug}/`}>
105+
<a href={`/wiki/${article.id}/`}>
105106
{article.data.title}
106107
<p>{article.data.description}</p>
107108
</a>
@@ -135,9 +136,9 @@ if (!contributors) {
135136
<div
136137
class="article"
137138
data-last-modified={article.data.lastModified}
138-
data-length={article.body.length}
139+
data-length={(article.body ?? "").length}
139140
>
140-
<a href={`/wiki/${article.slug}/`}>
141+
<a href={`/wiki/${article.id}/`}>
141142
<div class="name">{article.data.title}</div>
142143
<p class="description">{article.data.description}</p>
143144
</a>

0 commit comments

Comments
 (0)