1import remark from "remark";
2import html from "remark-html";
4import { join, parse } from "path";
5import matter from "gray-matter";
6import toml from "@iarna/toml";
8const directories = (dir) => {
11 return join(process.cwd(), "content/blog");
13 return join(process.cwd(), "content/events");
15 return join(process.cwd(), "content/docs");
17 return join(process.cwd(), "content/grants");
19 return join(process.cwd(), "content/media");
21 return join(process.cwd(), "content/updates");
23 return join(process.cwd(), "content");
25 return join(process.cwd(), `content/${dir}`);
29export function getPostSlugs(key) {
30 const dir = fs.readdirSync(directories(key), { withFileTypes: true });
32 .filter((f) => f.isFile() && f.name !== "_index.md")
36// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
39 toml: toml.parse.bind(toml),
45export function getPostBySlug(slug, fields = [], key) {
46 const realSlug = slug.replace(/\.md$/, "");
47 const fullPath = join(directories(key), `${realSlug}.md`);
48 const fileContents = fs.readFileSync(fullPath, "utf8");
49 const { data, content } = matter(fileContents, options);
52 // Ensure only the minimal needed data is exposed
53 fields.forEach((field) => {
54 if (field === "slug") {
55 items[field] = realSlug;
58 if (field === "content") {
59 items[field] = content;
63 items[field] = data[field];
67 // 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.
68 items.date = JSON.stringify(items?.date)?.replace(/\"/g, "") || "";
73export function getAllPosts(fields = [], key, sort = "date") {
74 const slugs = getPostSlugs(key);
76 .map((slug) => getPostBySlug(slug, fields, key))
77 // sort posts by date in descending order
78 .sort((post1, post2) => {
79 if (sort === "date") {
80 return post1.date > post2.date ? -1 : 1;
81 } else if (sort === "weight") {
82 return post1.weight > post2.weight ? -1 : 1;
89function getAllPaths(dirPath, arrayOfFiles) {
90 let files = fs.readdirSync(dirPath);
92 arrayOfFiles = arrayOfFiles || [];
94 files.forEach(function (file) {
95 if (fs.statSync(dirPath + "/" + file).isDirectory()) {
96 arrayOfFiles = getAllPaths(dirPath + "/" + file, arrayOfFiles);
98 arrayOfFiles.push(join(dirPath, "/", file));
105export function getDocsPaths() {
106 return getAllPaths(directories("docs"), []);
109export function getNextPost(slug, fields = [], key, sort = "date") {
110 let resultPost = null;
112 getAllPosts(fields, key, sort).forEach((post, index, array) => {
113 if (post.slug === slug) {
114 resultPost = array[index - 1];
120export function getPreviousPost(slug, fields = [], key, sort = "date") {
121 let resultPost = null;
123 getAllPosts(fields, key, sort).forEach((post, index, array) => {
124 if (post.slug === slug) {
125 resultPost = array[index + 1];
131export function formatDate(d) {
132 // const wk = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
147 return mn[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
150export function capitalize(str) {
151 return str.charAt(0).toUpperCase() + str.slice(1);
154// export function generateCrumbs(path) {
155// const crumbs = path.split('/')
157// .filter(v => v !== '')
158// .map((crumb, index) => ({
159// name: capitalize(crumb),
161// path: crumbs.slice(0, index+2).join('/'),
166 * buildPageTree is written to provide a recursive index of posts
167 * for nested directories for creating sidebars
168 * @param {string} path Absolute path to the subdirectory.
169 * @returns {Object.<string,string|Object<string,string[]|number>>} Title of the directory, child pages, and child directories (recursive structure).
181// children: { [string]: PageTree },
184export function buildPageTree(path, ordering = "", content = false) {
185 const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
186 // 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
187 const children = fs.readdirSync(path, { withFileTypes: true });
188 // get a list of files
189 const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
190 // get a list of folders
191 const subdirs = children.filter((f) => f.isDirectory());
192 // return a pagetree datastructure
194 title: metadata.data.title,
197 // retrieve topmatter as JSON
198 const { content, data } = matter(
199 fs.readFileSync(join(path, page.name)),
206 slug: page.name.replace(/.md$/, ""),
207 ...(ordering === "weight" && { weight: data?.weight ?? 0 }),
208 ...(ordering === "date" && {
209 date: data?.date ?? "2000-01-01T00:00:00.000Z",
213 .sort((a, b) => a.weight - b.weight),
214 children: Object.fromEntries(
217 const aMetadata = matter(
218 fs.readFileSync(join(path, a.name, "_index.md")),
221 const bMetadata = matter(
222 fs.readFileSync(join(path, b.name, "_index.md")),
225 return aMetadata?.data?.weight - bMetadata?.data?.weight || 0;
229 buildPageTree(join(path, subdir.name), ordering, content),
235export const getPage = (path) => {
237 let fileContents = fs.readFileSync(`${path}.md`, "utf8");
239 const { data, content } = matter(fileContents, options);
240 return { data, content };
244 let fileContents = fs.readFileSync(`${path}/_index.md`, "utf8");
246 const { data, content } = matter(fileContents, options);
247 return { data, content };
250 console.error("no md file for slug");
255// Returns an array of unique grants categories
256export function getGrantsCategories() {
257 const paths = fs.readdirSync(directories("grants"));
258 const uniqueCategories = paths
259 // Get only markdown files
260 .filter((path) => parse(path).ext === ".md")
263 matter(fs.readFileSync(join(directories("grants"), path)), options)
265 // Each markdown file has an array of categories. Collapse these arrays into one array.
266 .reduce((acc, metadata) => {
267 return [...acc, ...metadata.data.taxonomies.grant_category];
269 // Deduplicate single array of categories such that the output is an array where each category is represented only once.
270 .reduce((acc, uniqueItem) => {
271 if (acc.includes(uniqueItem)) {
274 return [...acc, uniqueItem];
276 return uniqueCategories;
279// Returns an array of unique grants types
280export function getGrantsTypes() {
281 const paths = fs.readdirSync(directories("grants"));
282 const uniqueTypes = paths
283 // Get only markdown files
284 .filter((path) => parse(path).ext === ".md")
285 // Parse frontmatter to JSON
287 matter(fs.readFileSync(join(directories("grants"), path)), options)
289 // Each markdown file has an array of types. Collapse these arrays into one array.
290 .reduce((acc, metadata) => {
291 return [...acc, ...metadata.data.taxonomies.grant_type];
293 // Deduplicate single array of types such that the output is an array where each type is represented only once.
294 .reduce((acc, uniqueItem) => {
295 if (acc.includes(uniqueItem)) {
298 return [...acc, uniqueItem];
303// Returns a number which represents how many grants are open, or uncompleted
304export function getOpenGrantsCount() {
305 const paths = fs.readdirSync(directories("grants"));
307 // Get only markdown files
308 .filter((path) => parse(path).ext === ".md")
309 // Parse frontmatter to JSON
311 matter(fs.readFileSync(join(directories("grants"), path)), options)
313 // Count grants which are both uncompleted and not assigned
314 .reduce((acc, metadata) => {
316 !metadata.data.extra.completed &&
317 metadata.data.extra.assignee === ""
326export function getSimilarGrants(type, category, num) {
327 const bestMatches = getAllPosts(
328 ["title", "slug", "date", "description", "extra", "taxonomies"],
334 typeWeight: grant.taxonomies.grant_type.reduce((acc, _type) => {
335 if (type === _type) {
340 tagWeight: grant.taxonomies.grant_category.reduce((acc, _category) => {
341 if (category === _category) {
348 .sort((a, b) => a.typeWeight + a.tagWeight - b.typeWeight + b.tagWeight)
349 .map((wrappedGrant) => {
350 return wrappedGrant.grant;
353 return bestMatches.slice(0, num);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list