on master help sign in
pages: amend all grants pages for year folders
commit5e97faafea6c14db8f8dd85c30c9d6cacbdde69b authorMatilde Park <matilde.park@urbit.org> dateThu 20 Oct 2022, 00:45 (4 years ago) parentaeb7c86
3 files changed+46−19
pages/grants.js+20−13 pages/grants/[slug].js+17−5 pages/index.js+9−1
pages/grants.js+20−13
@@ -15,6 +15,8 @@ import GrantPreview from "../components/GrantPreview";
1515import JoinGroup from "../components/JoinGroup";
1616import omit from "lodash.omit";
1717import { getGrantsCategories, getGrantsTypes } from "../lib/lib";
18+import fs from 'fs';
19+import { join } from 'path';
1820
1921function isArray(arr) {
2022 return Array.isArray(arr);
@@ -264,9 +266,8 @@ export default function Grants({ posts, categories, types, search }) {
264266 <h5 className="text-wall-600 font-semibold my-2">Programs</h5>
265267 <div className="flex flex-wrap items-center pb-2">
266268 <a
267− className={`badge-lg mr-2 ${
268− !type ? "bg-black text-white" : "bg-wall-100 text-wall-500"
269− }`}
269+ className={`badge-lg mr-2 ${!type ? "bg-black text-white" : "bg-wall-100 text-wall-500"
270+ }`}
270271 onClick={() => push((({ type, ...obj }) => obj)(router.query))}
271272 >
272273 All <div className="opacity-50 ml-2">{allCount}</div>
@@ -288,11 +289,10 @@ export default function Grants({ posts, categories, types, search }) {
288289 }
289290 return (
290291 <a
291− className={`badge-lg mr-2 ${
292− isOrIsIn(each, type)
293− ? activeBg
294− : "bg-wall-100 text-wall-500"
295− }`}
292+ className={`badge-lg mr-2 ${isOrIsIn(each, type)
293+ ? activeBg
294+ : "bg-wall-100 text-wall-500"
295+ }`}
296296 onClick={() => push({ ...router.query, type: each })}
297297 >
298298 {each}
@@ -365,11 +365,18 @@ export default function Grants({ posts, categories, types, search }) {
365365export async function getStaticProps() {
366366 const categories = getGrantsCategories();
367367 const types = getGrantsTypes();
368− const posts = getAllPosts(
369− ["title", "slug", "date", "description", "extra", "taxonomies"],
370− "grants",
371− "date"
372− );
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) => {
371+ return getAllPosts(
372+ ["title", "slug", "date", "description", "extra", "taxonomies"],
373+ `grants/${year.name}`,
374+ "date"
375+ )
376+ }).flat().sort((a, b) => {
377+ return a.title.toLowerCase().localeCompare(b.title.toLowerCase())
378+ });
379+
373380 return {
374381 props: {
375382 posts: posts,
pages/grants/[slug].js+17−5
@@ -18,6 +18,8 @@ import Header from "../../components/Header";
1818import Footer from "../../components/Footer";
1919import ob from "urbit-ob";
2020import { DateTime } from "luxon";
21+import fs from 'fs';
22+import { join } from 'path';
2123
2224export default function Grant({ post, markdown, search }) {
2325 const router = useRouter();
@@ -146,13 +148,18 @@ export default function Grant({ post, markdown, search }) {
146148 );
147149}
148150
149−//
150151export 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+ });
151158 const post = getPostBySlug(
152159 params.slug,
153160 ["title", "slug", "date", "description", "content", "extra", "taxonomies"],
154− "grants"
155− );
161+ `grants/${dir.name}`
162+ )
156163
157164 const markdown = JSON.stringify(Markdown.parse({ post }));
158165 return {
@@ -161,13 +168,18 @@ export async function getStaticProps({ params }) {
161168}
162169
163170export 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()
165177
166178 return {
167179 paths: posts.map((post) => {
168180 return {
169181 params: {
170− slug: post.slug,
182+ slug: post,
171183 },
172184 };
173185 }),
pages/index.js+9−1
@@ -355,7 +355,15 @@ export async function getStaticProps() {
355355 });
356356
357357 // 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();
359367
360368 const grantNumbers = {
361369 starsAwarded: Math.floor(