Skip to content

Commit 2221e36

Browse files
babblebeyCopilot
andauthored
refactor: modify codebase to astro@v5 (#151)
### Description <!-- Please add PR description (don't leave blank) - example: This PR [adds/removes/fixes/replaces] the [feature/bug/etc] --> This Pull Request refactors the codebase to meet the new requirement of `astro@v5` following the upgrade in #147; It introduces several changes to improve the handling of word slugs across the codebase, streamline content collection definitions, and enhance utility functions. The most significant updates include replacing hardcoded slugs with dynamically generated slugs, refactoring content collection logic, and introducing new utility functions for building word slugs. #### Word Slug Improvements: * [`src/components/islands/search.jsx`](diffhunk://#diff-9b7b1c6e6107530c0f69ea6f9a66511c9dab654d0eff83736b92475dba39858aL30-R31): Updated the `slug` property to use the new `buildWordSlug` utility function instead of hardcoding values. This change improves flexibility and consistency in slug generation. * `src/pages/browse/[alpha]/[page].astro`: Replaced hardcoded slugs with dynamically generated slugs using the `buildWordSlug` utility function in the word list rendering logic. ([src/pages/browse/[alpha]/[page].astroL46-R47](diffhunk://#diff-c87fe48708b5daee9f4aa138cb2970623ed74a334fa0912fef67f3a71905ee39L46-R47)) * `src/pages/browse/[...slug].astro`: Refactored the `getStaticPaths` function to use `word.id` instead of `word.slug`, ensuring alignment with the new slug generation approach. ([src/pages/browse/[...slug].astroL2-R19](diffhunk://#diff-5b7720b6f66d6cb09271ebf726a219ddf89ea09f9e3e4cb1e27c4088e8dd6796L2-R19)) #### Content Collection Refactoring: * [`src/content.config.ts`](diffhunk://#diff-0d78708eb6a4cf32e1758b9f04d0f05b751a26662664493a7c066252a0012f35R1-R8): Introduced a new `dictionary` collection definition using `astro:content` to simplify and centralize content loading. #### Utility Enhancements: * [`src/lib/utils/index.js`](diffhunk://#diff-15cd75e36ffbf7470b9767c164c4cbc65fb58d685753f23441ee7b050998193eR81-R89): Added the `buildWordSlug` utility function to dynamically generate slugs based on word IDs, improving maintainability and reducing duplication. #### Dependency and Import Updates: * [`astro.config.mjs`](diffhunk://#diff-e0f0c5adbe0b9ca5d0b57caf5cea33a8d88899fd02a43df1e9862b185f8a1e5fR4-L6): Updated the `vercel` integration to use the default export from `@astrojs/vercel` instead of the `serverless` variant. * [`src/lib/actions/do-edit-word.js`](diffhunk://#diff-b56e8ad6eb2e7f82bcca6ecffe96fce797e5bd64259ffbf637712d46994260a4R2): Added the `decrypt` utility to the imports, potentially for future use in word editing functionality. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent defdfc2 commit 2221e36

7 files changed

Lines changed: 49 additions & 17 deletions

File tree

astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { defineConfig } from "astro/config";
22
import mdx from "@astrojs/mdx";
33
import react from "@astrojs/react";
4+
import vercel from "@astrojs/vercel";
45
import tailwind from "@astrojs/tailwind";
56
import partytown from "@astrojs/partytown";
6-
import vercel from "@astrojs/vercel/serverless";
77

88
// https://astro.build/config
99
export default defineConfig({

src/components/islands/search.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import Flexsearch from "flexsearch";
22
import { useEffect, useState } from "react";
33
import { useStore } from "@nanostores/react";
44
import useRouter from "../../lib/hooks/use-router.js";
5+
import { buildWordPathname, buildWordSlug } from "../../lib/utils/index.js";
56
import useIsMacOS from "../../lib/hooks/use-is-mac-os.js";
67
import useLockBody from "../../lib/hooks/use-lock-body.js";
7-
import { $isSearchOpen, $addToRecentSearchesFn } from "../../lib/stores/search.js";
8+
import { $isSearchOpen } from "../../lib/stores/search.js";
89

910
// Create Search Index
1011
const searchIndex = new Flexsearch.Document({
@@ -25,9 +26,13 @@ export default function Search({ triggerSize, dictionary }) {
2526

2627
for (const word of dictionary) {
2728
searchIndex.add({
29+
/**
30+
* `word.id` could be a `slug` or a `slug` with the `mdx` extension i.e. `word-id.mdx`
31+
* @see https://github.com/withastro/astro/issues/14073
32+
*/
2833
id: word.id,
2934
title: word.data.title,
30-
slug: word.slug
35+
slug: buildWordPathname(buildWordSlug(word.id))
3136
});
3237
}
3338

@@ -231,10 +236,7 @@ function SearchResult({ result = [], cursor, searchTerm }) {
231236
) : (
232237
result.map(({ doc }, i) => (
233238
<a key={i}
234-
/**
235-
* @todo find better ways - don't hardcode `browse` string to the word slug
236-
*/
237-
href={`/browse/${doc.slug}`}
239+
href={doc.slug}
238240
onClick={(e) => {
239241
e.preventDefault();
240242
router.push(e.currentTarget.href);

src/content.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { glob } from "astro/loaders";
2+
import { defineCollection, z } from "astro:content";
3+
4+
const dictionary = defineCollection({
5+
loader: glob({ pattern: "**/*.mdx", base: "./src/content/dictionary" }),
6+
});
7+
8+
export const collections = { dictionary };

src/lib/actions/do-edit-word.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import app from "../octokit/app.js";
2+
import { decrypt } from "../utils/crypto.js";
23
import { getExistingWord } from "../word-editor.js";
34
import { PROJECT_REPO_DETAILS } from "../../../constants.js";
45

src/lib/utils/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,22 @@ export function generateBranchName(action, wordTitle) {
7777
*/
7878
export function buildStatsUrl(repoFullname, queryString) {
7979
return `https://github.com/${repoFullname}/pulls?q=${encodeURIComponent(queryString)}`;
80+
}
81+
82+
/**
83+
* Build Word Pathname From Slug
84+
* @param {string} slug
85+
* @returns {string}
86+
*/
87+
export function buildWordPathname(slug) {
88+
return `/browse/${slug}`;
89+
}
90+
91+
/**
92+
* Build Word Slug From ID
93+
* @param {string} id
94+
* @returns {string}
95+
*/
96+
export function buildWordSlug(id) {
97+
return id.split(".")[0];
8098
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
---
2-
import { getCollection } from "astro:content";
3-
4-
const { entry } = Astro.props;
5-
6-
//@ts-expect-error - `entry` is percieved as "never" type
7-
const { Content } = await entry.render();
2+
import { buildWordSlug } from "../../lib/utils";
3+
import { getCollection, render } from "astro:content";
84
95
export const prerender = true;
106
117
export async function getStaticPaths() {
128
const dictionary = await getCollection("dictionary");
13-
return dictionary.map(entry => ({
14-
params: { slug: entry.slug }, props: { entry },
15-
}));
9+
return dictionary.map(word => ({
10+
params: { slug: buildWordSlug(word.id) },
11+
props: { word }
12+
})
13+
);
1614
}
15+
16+
const { word } = Astro.props;
17+
const { Content } = await render(word);
1718
---
1819
<Content />

src/pages/browse/[alpha]/[page].astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ALPHABETS } from "../../../../constants.js";
44
import BaseLayout from "../../../layouts/base.astro";
55
import Navbar from "../../../components/navbar.astro";
66
import Search from "../../../components/islands/search";
7+
import { buildWordPathname } from "../../../lib/utils/index.js";
78
import ContributionCTA from "../../../components/contribution-cta.astro";
89
910
const { page } = Astro.props;
@@ -16,6 +17,7 @@ export const prerender = true;
1617
export async function getStaticPaths({ paginate }) {
1718
const dictionary = await getCollection("dictionary");
1819
return ALPHABETS.flatMap(alpha => {
20+
// @ts-expect-error - `slug` property is not defined in `CollectionEntry` type - see https://github.com/withastro/astro/issues/14070
1921
const filteredWords = dictionary.filter(word => word.slug[0] === alpha);
2022
return paginate(filteredWords, {
2123
params: { alpha },
@@ -43,7 +45,7 @@ export async function getStaticPaths({ paginate }) {
4345
<!-- Words List -->
4446
<div>
4547
{page.data.length ? page.data.map(word => (
46-
<a href={`/browse/${word.slug}`}
48+
<a href={buildWordPathname(word.slug)}
4749
class="flex items-center md:text-lg justify-between no-underline w-full p-4 even:bg-gray-100 hover:bg-gray-100/50"
4850
>
4951
<span>{ word.data.title }</span>

0 commit comments

Comments
 (0)