on master help sign in
lib: add getGrantYear, getGrantByShortcode
commit79e8835b6a854b4fda9682ac6243b9eacfcb8239 authorMatilde Park <matilde.park@urbit.org> dateTue 1 Nov 2022, 14:01 (4 years ago) parentf8dd399
1 file changed+28
lib/lib.js+28
lib/lib.js+28
@@ -126,6 +126,34 @@ export function getGrantsTypes() {
126126 return uniqueTypes;
127127}
128128
129+// Grab which directory the grant post is in
130+
131+export function getGrantYear(slug) {
132+ const basePath = join(process.cwd(), 'content/grants')
133+ const years = fs.readdirSync(basePath, { 'withFileTypes': true }).filter((f) => f.isDirectory());
134+ const dir = years.find((year) => {
135+ const yearDir = fs.readdirSync(join(basePath, year.name), { 'withFileTypes': true });
136+ return yearDir.some((file) => file.name === `${slug}.md`)
137+ });
138+ return dir;
139+}
140+
141+export function getGrantByShortcode(shortcode) {
142+ const basePath = join(process.cwd(), 'content/grants')
143+ const years = fs.readdirSync(basePath, { 'withFileTypes': true }).filter((f) => f.isDirectory());
144+ let found = null
145+ const dir = years.find((year) => {
146+ const yearDir = fs.readdirSync(join(basePath, year.name), { 'withFileTypes': true });
147+ const foundFile = yearDir.find((file) => {
148+ const { data } = matter(fs.readFileSync(join(basePath, year.name, file.name)), options);
149+ return data?.extra?.deliverable === shortcode.join("/")
150+ })
151+ found = foundFile;
152+ return foundFile
153+ });
154+ return found?.name?.replace(".md", "");
155+}
156+
129157// Groups arrays into an array of arrays with 2 members
130158export const pair = (arr) =>
131159 arr.reduce(function (rows, key, index) {