on a054df2 help sign in
urbit.org/lib/lib.js
290 lines 8.4 kB history raw
1import remark from "remark";
2import html from "remark-html";
3import fs from "fs";
4import { join, parse } from "path";
5import matter from "gray-matter";
6import toml from "@iarna/toml";
8const directories = {
9 blog: join(process.cwd(), "content/blog"),
10 events: join(process.cwd(), "content/events"),
11 docs: join(process.cwd(), "content/docs"),
12 grants: join(process.cwd(), "content/grants"),
13 media: join(process.cwd(), "content/media"),
14};
16export function getPostSlugs(key) {
17 return fs.readdirSync(directories[key]);
20// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
21const options = {
22 engines: {
23 toml: toml.parse.bind(toml),
24 },
25 language: "toml",
26 delimiters: "+++",
27};
29export function getPostBySlug(slug, fields = [], key) {
30 const realSlug = slug.replace(/\.md$/, "");
31 const fullPath = join(directories[key], `${realSlug}.md`);
32 const fileContents = fs.readFileSync(fullPath, "utf8");
33 const { data, content } = matter(fileContents, options);
34 const items = {};
36 // Ensure only the minimal needed data is exposed
37 fields.forEach((field) => {
38 if (field === "slug") {
39 items[field] = realSlug;
40 }
42 if (field === "content") {
43 items[field] = content;
44 }
46 if (data[field]) {
47 items[field] = data[field];
48 }
49 });
51 // NB Gavin: This could be removed if the TOML engine returned a JSON date (string) instead of a JS Date object. The YAML engine did this parsing by default.
52 items.date = JSON.stringify(items.date).replace(/\"/g, "");
54 return items;
57export function getAllPosts(fields = [], key) {
58 const slugs = getPostSlugs(key);
59 const posts = slugs
60 .map((slug) => getPostBySlug(slug, fields, key))
61 // sort posts by date in descending order
62 .sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
64 return posts;
67function getAllPaths(dirPath, arrayOfFiles) {
68 let files = fs.readdirSync(dirPath);
70 arrayOfFiles = arrayOfFiles || [];
72 files.forEach(function (file) {
73 if (fs.statSync(dirPath + "/" + file).isDirectory()) {
74 arrayOfFiles = getAllPaths(dirPath + "/" + file, arrayOfFiles);
75 } else {
76 arrayOfFiles.push(join(dirPath, "/", file));
77 }
78 });
80 return arrayOfFiles;
83export function getDocsPaths() {
84 return getAllPaths(directories.docs, []);
87export function getNextPost(slug, fields = [], key) {
88 let resultPost = null;
90 getAllPosts(fields, key).forEach((post, index, array) => {
91 if (post.slug === slug) {
92 resultPost = array[index - 1];
93 }
94 });
95 return resultPost;
98export function getPreviousPost(slug, fields = [], key) {
99 let resultPost = null;
101 getAllPosts(fields, key).forEach((post, index, array) => {
102 if (post.slug === slug) {
103 resultPost = array[index + 1];
104 }
105 });
106 return resultPost;
109export function formatDate(d) {
110 // const wk = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
111 const mn = [
112 "January",
113 "February",
114 "March",
115 "April",
116 "May",
117 "June",
118 "July",
119 "August",
120 "September",
121 "October",
122 "November",
123 "December",
124 ];
125 return mn[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
128// export function capitalize(str) {
129// return str.charAt(0).toUpperCase() + str.slice(1);
130// }
132// export function generateCrumbs(path) {
133// const crumbs = path.split('/')
134// return crumbs
135// .filter(v => v !== '')
136// .map((crumb, index) => ({
137// name: capitalize(crumb),
138// key: crumb,
139// path: crumbs.slice(0, index+2).join('/'),
140// }))
141// }
143/**
144 * buildPageTree is written to provide a recursive index of posts
145 * for nested directories for creating sidebars
146 * @param {string} path Absolute path to the subdirectory.
147 * @returns {Object.<string,string|Object<string,string[]|number>>} Title of the directory, child pages, and child directories (recursive structure).
148 *
149 */
151// type PageTree =
152// { title: string,
153// pages: [{
154// title: string,
155// base: string,
156// slug: string,
157// weight: number,
158// }],
159// children: { [string]: PageTree },
160// }
162export function buildPageTree(path, ordering = "", content = false) {
163 const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
164 // get a list of contents at the path, with file types so that the item at the path can be identified as file or folder
165 const children = fs.readdirSync(path, { withFileTypes: true });
166 // get a list of files
167 const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
168 // get a list of folders
169 const subdirs = children.filter((f) => f.isDirectory());
170 // return a pagetree datastructure
171 return {
172 title: metadata.data.title,
173 pages: pages
174 .map((page) => {
175 // retrieve topmatter as JSON
176 const { content, data } = matter(
177 fs.readFileSync(join(path, page.name)),
178 options
179 );
181 return {
182 title: data.title,
183 base: page.name,
184 slug: page.name.replace(/.md$/, ""),
185 ...(ordering === "weight" && { weight: data?.weight ?? 0 }),
186 ...(ordering === "date" && {
187 date: data?.date ?? "2000-01-01T00:00:00.000Z",
188 }),
189 };
190 })
191 .sort((a, b) => a.weight - b.weight),
192 children: Object.fromEntries(
193 subdirs.map((subdir) => [
194 subdir.name,
195 buildPageTree(join(path, subdir.name), ordering, content),
196 ])
197 ),
198 };
201export const getPage = (path) => {
202 try {
203 let fileContents = fs.readFileSync(`${path}.md`, "utf8");
204 if (fileContents) {
205 const { data, content } = matter(fileContents, options);
206 return { data, content };
207 }
208 } catch {
209 try {
210 let fileContents = fs.readFileSync(`${path}/_index.md`, "utf8");
211 if (fileContents) {
212 const { data, content } = matter(fileContents, options);
213 return { data, content };
214 }
215 } catch {
216 console.error("no md file for slug");
217 }
218 }
219};
221// Returns an array of unique grants categories
222export function getGrantsCategories() {
223 const paths = fs.readdirSync(directories.grants);
224 const uniqueCategories = paths
225 // Get only markdown files
226 .filter((path) => parse(path).ext === ".md")
227 // Parse frontmatter
228 .map((path) =>
229 matter(fs.readFileSync(join(directories.grants, path)), options)
230 )
231 // Each markdown file has an array of categories. Collapse these arrays into one array.
232 .reduce((acc, metadata) => {
233 return [...acc, ...metadata.data.taxonomies.grant_category];
234 }, [])
235 // Deduplicate single array of categories such that the output is an array where each category is represented only once.
236 .reduce((acc, uniqueItem) => {
237 if (acc.includes(uniqueItem)) {
238 return acc;
239 }
240 return [...acc, uniqueItem];
241 }, []);
242 return uniqueCategories;
245// Returns an array of unique grants types
246export function getGrantsTypes() {
247 const paths = fs.readdirSync(directories.grants);
248 const uniqueTypes = paths
249 // Get only markdown files
250 .filter((path) => parse(path).ext === ".md")
251 // Parse frontmatter to JSON
252 .map((path) =>
253 matter(fs.readFileSync(join(directories.grants, path)), options)
254 )
255 // Each markdown file has an array of types. Collapse these arrays into one array.
256 .reduce((acc, metadata) => {
257 return [...acc, ...metadata.data.taxonomies.grant_type];
258 }, [])
259 // Deduplicate single array of types such that the output is an array where each type is represented only once.
260 .reduce((acc, uniqueItem) => {
261 if (acc.includes(uniqueItem)) {
262 return acc;
263 }
264 return [...acc, uniqueItem];
265 }, []);
266 return uniqueTypes;
269// Returns a number which represents how many grants are open, or uncompleted
270export function getOpenGrantsCount() {
271 const paths = fs.readdirSync(directories.grants);
272 const count = paths
273 // Get only markdown files
274 .filter((path) => parse(path).ext === ".md")
275 // Parse frontmatter to JSON
276 .map((path) =>
277 matter(fs.readFileSync(join(directories.grants, path)), options)
278 )
279 // Count grants which are both uncompleted and not assigned
280 .reduce((acc, metadata) => {
281 if (
282 !metadata.data.extra.completed &&
283 metadata.data.extra.assignee === ""
284 ) {
285 return acc + 1;
286 }
287 return acc;
288 }, 0);
289 return count;