on master help sign in
pages: amend all grants pages for year folders
5e97faaMatilde Park
pages/grants.js+20−13
@@ -15,6 +15,8 @@ import GrantPreview from "../components/GrantPreview";
15import JoinGroup from "../components/JoinGroup";
15import JoinGroup from "../components/JoinGroup";
16import omit from "lodash.omit";
16import omit from "lodash.omit";
17import { getGrantsCategories, getGrantsTypes } from "../lib/lib";
17import { getGrantsCategories, getGrantsTypes } from "../lib/lib";
18+import fs from 'fs';
19+import { join } from 'path';
18
20
19function isArray(arr) {
21function isArray(arr) {
20 return Array.isArray(arr);
22 return Array.isArray(arr);
@@ -264,9 +266,8 @@ export default function Grants({ posts, categories, types, search }) {
264 <h5 className="text-wall-600 font-semibold my-2">Programs</h5>
266 <h5 className="text-wall-600 font-semibold my-2">Programs</h5>
265 <div className="flex flex-wrap items-center pb-2">
267 <div className="flex flex-wrap items-center pb-2">
266 <a
268 <a
267− className={`badge-lg mr-2 ${
268− !type ? "bg-black text-white" : "bg-wall-100 text-wall-500"
269+ className={`badge-lg mr-2 ${!type ? "bg-black text-white" : "bg-wall-100 text-wall-500"
269− }`}
270+ }`}
270 onClick={() => push((({ type, ...obj }) => obj)(router.query))}
271 onClick={() => push((({ type, ...obj }) => obj)(router.query))}
271 >
272 >
272 All <div className="opacity-50 ml-2">{allCount}</div>
273 All <div className="opacity-50 ml-2">{allCount}</div>
@@ -288,11 +289,10 @@ export default function Grants({ posts, categories, types, search }) {
288 }
289 }
289 return (
290 return (
290 <a
291 <a
291− className={`badge-lg mr-2 ${
292+ className={`badge-lg mr-2 ${isOrIsIn(each, type)
292− isOrIsIn(each, type)
293− ? activeBg
293+ ? activeBg
294− : "bg-wall-100 text-wall-500"
294+ : "bg-wall-100 text-wall-500"
295− }`}
295+ }`}
296 onClick={() => push({ ...router.query, type: each })}
296 onClick={() => push({ ...router.query, type: each })}
297 >
297 >
298 {each}
298 {each}
@@ -365,11 +365,18 @@ export default function Grants({ posts, categories, types, search }) {
365export async function getStaticProps() {
365export async function getStaticProps() {
366 const categories = getGrantsCategories();
366 const categories = getGrantsCategories();
367 const types = getGrantsTypes();
367 const types = getGrantsTypes();
368+ const basePath = join(process.cwd(), 'content/grants')
369+ const years = fs.readdirSync(basePath, { 'withFileTypes': true }).filter((f) => f.isDirectory());
370+ const posts = years.map((year) => {
368− const posts = getAllPosts(
371+ return getAllPosts(
369− ["title", "slug", "date", "description", "extra", "taxonomies"],
372+ ["title", "slug", "date", "description", "extra", "taxonomies"],
370− "grants",
373+ `grants/${year.name}`,
371− "date"
374+ "date"
375+ )
376+ }).flat().sort((a, b) => {
377+ return a.title.toLowerCase().localeCompare(b.title.toLowerCase())
372− );
378+ });
379+
373 return {
380 return {
374 props: {
381 props: {
375 posts: posts,
382 posts: posts,
pages/grants/[slug].js+17−5
@@ -18,6 +18,8 @@ import Header from "../../components/Header";
18import Footer from "../../components/Footer";
18import Footer from "../../components/Footer";
19import ob from "urbit-ob";
19import ob from "urbit-ob";
20import { DateTime } from "luxon";
20import { DateTime } from "luxon";
21+import fs from 'fs';
22+import { join } from 'path';
21
23
22export default function Grant({ post, markdown, search }) {
24export default function Grant({ post, markdown, search }) {
23 const router = useRouter();
25 const router = useRouter();
@@ -146,13 +148,18 @@ export default function Grant({ post, markdown, search }) {
146 );
148 );
147}
149}
148
150
149−//
150export async function getStaticProps({ params }) {
151export async function getStaticProps({ params }) {
152+ const basePath = join(process.cwd(), 'content/grants')
153+ const years = fs.readdirSync(basePath, { 'withFileTypes': true }).filter((f) => f.isDirectory());
154+ const dir = years.find((year) => {
155+ const yearDir = fs.readdirSync(join(basePath, year.name), { 'withFileTypes': true });
156+ return yearDir.some((file) => file.name === `${params.slug}.md`)
157+ });
151 const post = getPostBySlug(
158 const post = getPostBySlug(
152 params.slug,
159 params.slug,
153 ["title", "slug", "date", "description", "content", "extra", "taxonomies"],
160 ["title", "slug", "date", "description", "content", "extra", "taxonomies"],
154− "grants"
161+ `grants/${dir.name}`
155− );
162+ )
156
163
157 const markdown = JSON.stringify(Markdown.parse({ post }));
164 const markdown = JSON.stringify(Markdown.parse({ post }));
158 return {
165 return {
@@ -161,13 +168,18 @@ export async function getStaticProps({ params }) {
161}
168}
162
169
163export async function getStaticPaths() {
170export async function getStaticPaths() {
164− const posts = getAllPosts(["slug", "date"], "grants", "date");
171+ const basePath = join(process.cwd(), 'content/grants')
172+ const years = fs.readdirSync(basePath, { 'withFileTypes': true });
173+ const posts = years.filter((year) => year.isDirectory()).map((year) => {
174+ const yearDir = fs.readdirSync(join(basePath, year.name), { 'withFileTypes': true });
175+ return yearDir.filter((entry) => entry.isFile()).map((entry) => entry.name.replace(/\.md$/, ""))
176+ }).flat()
165
177
166 return {
178 return {
167 paths: posts.map((post) => {
179 paths: posts.map((post) => {
168 return {
180 return {
169 params: {
181 params: {
170− slug: post.slug,
182+ slug: post,
171 },
183 },
172 };
184 };
173 }),
185 }),
pages/index.js+9−1
@@ -355,7 +355,15 @@ export async function getStaticProps() {
355 });
355 });
356
356
357 // Latest grants
357 // Latest grants
358− const grants = getAllPosts(["extra", "taxonomies"], "grants", "date");
358+ const basePath = path.join(process.cwd(), 'content/grants')
359+ const years = fs.readdirSync(basePath, { 'withFileTypes': true }).filter((f) => f.isDirectory());
360+ const grants = years.map((year) => {
361+ return getAllPosts(
362+ ["title", "slug", "date", "description", "extra", "taxonomies"],
363+ `grants/${year.name}`,
364+ "date"
365+ )
366+ }).flat();
359
367
360 const grantNumbers = {
368 const grantNumbers = {
361 starsAwarded: Math.floor(
369 starsAwarded: Math.floor(