on master help sign in
lib: pull ecosystem post matching into util
commit0befd7340e6e12e59c20295e3094b4d3a36cf5a0 authorMatilde Park <matilde.park@urbit.org> dateTue 29 Nov 2022, 16:36 (4 years ago) parentd8ebcba
4 files changed+94−161
lib/lib.js+84−1 pages/ecosystem/index.js+3−23 pages/ecosystem/spotlight/[slug].js+3−61 pages/index.js+4−76
lib/lib.js+84−1
@@ -3,7 +3,7 @@ import { join, parse } from "path";
33import matter from "gray-matter";
44import toml from "@iarna/toml";
55import { DateTime } from "luxon";
6−import { getPostBySlug, getPostSlugs } from "@urbit/foundation-design-system";
6+import { Markdown, getPostBySlug, getPostSlugs } from "@urbit/foundation-design-system";
77
88// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
99const options = {
@@ -166,3 +166,86 @@ export const pair = (arr) =>
166166 rows
167167 );
168168 }, []);
169+
170+// Match ecosystem report to posts to inject metadata
171+export const matchEcosystemPost = (ecosystem) => {
172+ const articles = getAllPosts(
173+ ["title", "image", "date", "podcast", "slug"],
174+ "articles",
175+ "date"
176+ );
177+
178+ const marketplaces = getAllPosts(["title", "image", "slug"], "marketplaces");
179+ const podcasts = getAllPosts(
180+ ["title", "image", "date", "podcast", "slug"],
181+ "podcasts",
182+ "date"
183+ );
184+ const organizations = getAllPosts(
185+ ["title", "image", "slug"],
186+ "organizations"
187+ );
188+ const applications = fs
189+ .readdirSync(join(process.cwd(), "content/applications"), {
190+ withFileTypes: true,
191+ })
192+ .filter((f) => f.isDirectory())
193+ .map((dir) =>
194+ getAllPosts(
195+ ["title", "bgColor", "image", "slug", "description"],
196+ `applications/${dir.name}`
197+ )
198+ .map((e) => ({ ...e, ship: dir.name }))
199+ .flat()
200+ )
201+ .flat()
202+ .sort((a, b) => {
203+ const nameA = a.title.toLowerCase();
204+ const nameB = b.title.toLowerCase();
205+ return nameA < nameB ? -1 : 1;
206+ });
207+
208+ const groups = fs
209+ .readdirSync(join(process.cwd(), "content/groups"), {
210+ withFileTypes: true,
211+ })
212+ .filter((f) => f.isDirectory())
213+ .map((dir) =>
214+ getAllPosts(
215+ ["title", "tile", "slug", "description"],
216+ `groups/${dir.name}`
217+ )
218+ .map((e) => ({ ...e, ship: dir.name }))
219+ .flat()
220+ )
221+ .flat()
222+ .sort((a, b) => {
223+ const nameA = a.title.toLowerCase();
224+ const nameB = b.title.toLowerCase();
225+ return nameA < nameB ? -1 : 1;
226+ });
227+
228+ ["featured-1", "featured-2", "featured-3"].forEach((feat) => {
229+ if (ecosystem?.[feat]) {
230+ const matchedPost = [
231+ ...articles.map((e) => ({ ...e, type: "Article" })),
232+ ...applications.map((e) => ({ ...e, type: "Application" })),
233+ ...organizations.map((e) => ({ ...e, type: "Organization" })),
234+ ...podcasts.map((e) => ({ ...e, type: "Podcast" })),
235+ ...marketplaces.map((e) => ({ ...e, type: "Marketplace" })),
236+ ...groups.map((e) => ({ ...e, type: "Group" }))
237+ ].filter((e) => e.title === ecosystem[feat].title)?.[0];
238+ ecosystem[feat].image =
239+ ecosystem[feat].image || matchedPost?.image || null;
240+ ecosystem[feat].type = matchedPost?.type || "Podcast";
241+ ecosystem[feat].matchedPost = matchedPost || null;
242+ ecosystem[feat].content = JSON.stringify(
243+ Markdown.parse({
244+ post: { content: ecosystem[feat].content },
245+ })
246+ );
247+ }
248+ });
249+
250+ return ecosystem;
251+}
pages/ecosystem/index.js+3−23
@@ -20,6 +20,7 @@ import Link from "next/link";
2020import fs from "fs";
2121import path from "path";
2222import Contact from "../../components/Contact";
23+import { matchEcosystemPost } from "../../lib/lib";
2324
2425export default function Ecosystem({
2526 search,
@@ -386,11 +387,11 @@ export async function getStaticProps({ }) {
386387 "ecosystem/spotlight",
387388 "date"
388389 );
389− let post = getAllPosts(
390+ let post = matchEcosystemPost(getAllPosts(
390391 ["title", "date", "featured-1", "featured-2", "featured-3"],
391392 "ecosystem/spotlight",
392393 "date"
393− )[0];
394+ )[0]);
394395 const marketplaces = getAllPosts(["title", "image", "slug"], "marketplaces");
395396 const podcasts = getAllPosts(
396397 ["title", "image", "date", "podcast", "slug"],
@@ -445,27 +446,6 @@ export async function getStaticProps({ }) {
445446 return nameA < nameB ? -1 : 1;
446447 });
447448
448− ["featured-1", "featured-2", "featured-3"].forEach((feat) => {
449− if (post?.[feat]) {
450− const matchedPost = [
451− ...applications.map((e) => ({ ...e, type: "Application" })),
452− ...organizations.map((e) => ({ ...e, type: "Organization" })),
453− ...podcasts.map((e) => ({ ...e, type: "Podcast" })),
454− ...articles.map((e) => ({ ...e, type: "Article" })),
455− ...marketplaces.map((e) => ({ ...e, type: "Marketplace" })),
456− ...groups.map((e) => ({ ...e, type: "Group" })),
457− ].filter((e) => e.title === post[feat].title)?.[0];
458− post[feat].image = post[feat]?.image || matchedPost?.image || null;
459− post[feat].type = matchedPost?.type || "Podcast";
460− post[feat].matchedPost = matchedPost || null;
461− post[feat].content = JSON.stringify(
462− Markdown.parse({
463− post: { content: post[feat].content },
464− })
465− );
466− }
467− });
468−
469449 return {
470450 props: {
471451 posts,
pages/ecosystem/spotlight/[slug].js+3−61
@@ -6,7 +6,6 @@ import {
66 Container,
77 SingleColumn,
88 Section,
9− Markdown,
109 IntraNav,
1110 getAllPosts,
1211 getPostBySlug,
@@ -14,8 +13,7 @@ import {
1413import Header from "../../../components/Header";
1514import Footer from "../../../components/Footer";
1615import Post from "../../../components/ecosystem/Post";
17−import fs from "fs";
18−import path from "path";
16+import { matchEcosystemPost } from "../../../lib/lib";
1917
2018export default function BasicPage({ post, markdown, search, index = false }) {
2119 const router = useRouter();
@@ -44,67 +42,11 @@ export default function BasicPage({ post, markdown, search, index = false }) {
4442}
4543
4644export async function getStaticProps({ params }) {
47− const post = getPostBySlug(
45+ const post = matchEcosystemPost(getPostBySlug(
4846 params.slug,
4947 ["title", "slug", "date", "featured-1", "featured-2", "featured-3"],
5048 "ecosystem/spotlight"
51− );
52− const marketplaces = getAllPosts(["title", "image", "slug"], "marketplaces");
53− const groups = getAllPosts(["title", "tile,", "description", "slug"], "groups");
54− const podcasts = getAllPosts(
55− ["title", "image", "date", "podcast", "slug"],
56− "podcasts",
57− "date"
58− );
59− const articles = getAllPosts(
60− ["title", "image", "date", "publication", "author", "slug"],
61− "articles",
62− "date"
63− );
64− const organizations = getAllPosts(
65− ["title", "image", "slug"],
66− "organizations"
67− );
68− const applications = fs
69− .readdirSync(path.join(process.cwd(), "content/applications"), {
70− withFileTypes: true,
71− })
72− .filter((f) => f.isDirectory())
73− .map((dir) =>
74− getAllPosts(
75− ["title", "bgColor", "image", "slug", "description"],
76− `applications/${dir.name}`
77− )
78− .map((e) => ({ ...e, ship: dir.name }))
79− .flat()
80− )
81− .flat()
82− .sort((a, b) => {
83− const nameA = a.title.toLowerCase();
84− const nameB = b.title.toLowerCase();
85− return nameA < nameB ? -1 : 1;
86− });
87−
88− ["featured-1", "featured-2", "featured-3"].forEach((feat) => {
89− if (post?.[feat]) {
90− const matchedPost = [
91− ...applications.map((e) => ({ ...e, type: "Application" })),
92− ...organizations.map((e) => ({ ...e, type: "Organization" })),
93− ...groups.map((e) => ({ ...e, type: "Group" })),
94− ...podcasts.map((e) => ({ ...e, type: "Podcast" })),
95− ...marketplaces.map((e) => ({ ...e, type: "Marketplace" })),
96− ...articles.map((e) => ({ ...e, type: "Article" })),
97− ].filter((e) => e.title === post[feat].title)?.[0];
98− post[feat].image = post[feat]?.image || matchedPost?.image || null;
99− post[feat].type = matchedPost?.type || "Podcast";
100− post[feat].matchedPost = matchedPost || null;
101− post[feat].content = JSON.stringify(
102− Markdown.parse({
103− post: { content: post[feat].content },
104− })
105− );
106− }
107− });
49+ ));
10850
10951 return {
11052 props: { post },
pages/index.js+4−76
@@ -22,6 +22,7 @@ import IndexCard from "../components/ecosystem/IndexCard";
2222import fs from "fs";
2323import path from "path";
2424import Meta from "../components/Meta";
25+import { matchEcosystemPost } from "../lib/lib";
2526
2627export default function Home({
2728 posts,
@@ -316,85 +317,12 @@ export async function getStaticProps() {
316317 "date"
317318 );
318319
319− // Pull all latest ecosystem information
320− const ecosystem = getAllPosts(
320+ // Latest ecosystem report with matched post metadata
321+ const ecosystem = matchEcosystemPost(getAllPosts(
321322 ["date", "featured-1", "featured-2", "featured-3"],
322323 "ecosystem/spotlight",
323324 "date"
324− )[0];
325−
326− const articles = getAllPosts(
327− ["title", "image", "date", "podcast", "slug"],
328− "articles",
329− "date"
330− );
331−
332− const marketplaces = getAllPosts(["title", "image", "slug"], "marketplaces");
333− const podcasts = getAllPosts(
334− ["title", "image", "date", "podcast", "slug"],
335− "podcasts",
336− "date"
337− );
338− const organizations = getAllPosts(
339− ["title", "image", "slug"],
340− "organizations"
341− );
342− const applications = fs
343− .readdirSync(path.join(process.cwd(), "content/applications"), {
344− withFileTypes: true,
345− })
346− .filter((f) => f.isDirectory())
347− .map((dir) =>
348− getAllPosts(
349− ["title", "bgColor", "image", "slug", "description"],
350− `applications/${dir.name}`
351− )
352− .map((e) => ({ ...e, ship: dir.name }))
353− .flat()
354− )
355− .flat()
356− .sort((a, b) => {
357− const nameA = a.title.toLowerCase();
358− const nameB = b.title.toLowerCase();
359− return nameA < nameB ? -1 : 1;
360− });
361−
362− const groups = fs
363− .readdirSync(path.join(process.cwd(), "content/groups"), {
364− withFileTypes: true,
365− })
366− .filter((f) => f.isDirectory())
367− .map((dir) =>
368− getAllPosts(
369− ["title", "tile", "slug", "description"],
370− `groups/${dir.name}`
371− )
372− .map((e) => ({ ...e, ship: dir.name }))
373− .flat()
374− )
375− .flat()
376− .sort((a, b) => {
377− const nameA = a.title.toLowerCase();
378− const nameB = b.title.toLowerCase();
379− return nameA < nameB ? -1 : 1;
380− });
381−
382− ["featured-1", "featured-2", "featured-3"].forEach((feat) => {
383− if (ecosystem?.[feat]) {
384− const matchedPost = [
385− ...articles.map((e) => ({ ...e, type: "Article" })),
386− ...applications.map((e) => ({ ...e, type: "Application" })),
387− ...organizations.map((e) => ({ ...e, type: "Organization" })),
388− ...podcasts.map((e) => ({ ...e, type: "Podcast" })),
389− ...marketplaces.map((e) => ({ ...e, type: "Marketplace" })),
390− ...groups.map((e) => ({ ...e, type: "Group" }))
391− ].filter((e) => e.title === ecosystem[feat].title)?.[0];
392− ecosystem[feat].image =
393− ecosystem[feat].image || matchedPost?.image || null;
394− ecosystem[feat].type = matchedPost?.type || "Podcast";
395− ecosystem[feat].matchedPost = matchedPost || null;
396− }
397− });
325+ )[0]);
398326
399327 // Latest grants
400328 const basePath = path.join(process.cwd(), 'content/grants')