@@ -3,6 +3,16 @@ import { join, parse } from "path";
3import matter from "gray-matter";
3import matter from "gray-matter";
4import toml from "@iarna/toml";
4import toml from "@iarna/toml";
5import { DateTime } from "luxon";
5import { DateTime } from "luxon";
6+import { getPostBySlug, getPostSlugs } from "@urbit/foundation-design-system";
8+// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
11+ toml: toml.parse.bind(toml),
7const directories = (dir) => {
17const directories = (dir) => {
8 switch (dir) {
18 switch (dir) {
@@ -25,49 +35,6 @@ const directories = (dir) => {
28−export function getPostSlugs(key) {
29− const dir = fs.existsSync(directories(key))
30− ? fs.readdirSync(directories(key), { withFileTypes: true })
33− .filter((f) => f.isFile() && f.name !== "_index.md")
37−// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
40− toml: toml.parse.bind(toml),
46−export function getPostBySlug(slug, fields = [], key) {
47− const realSlug = slug.replace(/\.md$/, "");
48− const fullPath = join(directories(key), `${realSlug}.md`);
49− const fileContents = fs.readFileSync(fullPath, "utf8");
50− const { data, content } = matter(fileContents, options);
53− // Ensure only the minimal needed data is exposed
54− fields.forEach((field) => {
55− if (field === "slug") {
56− items[field] = realSlug;
59− if (field === "content") {
60− items[field] = content;
64− items[field] = data[field];
71export function getAllPosts(fields = [], key, sort = "") {
38export function getAllPosts(fields = [], key, sort = "") {
72 const slugs = getPostSlugs(key);
39 const slugs = getPostSlugs(key);
73 const posts = slugs
40 const posts = slugs
@@ -116,32 +83,6 @@ export function getDocsPaths() {
116 return getAllPaths(directories("docs"), []);
83 return getAllPaths(directories("docs"), []);
119−export function getNextPost(slug, fields = [], key, sort = "date") {
120− let resultPost = null;
122− getAllPosts(fields, key, sort).forEach((post, index, array) => {
123− if (post.slug === slug) {
124− if (typeof array[index - 1] !== "undefined") {
125− resultPost = array[index - 1];
132−export function getPreviousPost(slug, fields = [], key, sort = "date") {
133− let resultPost = null;
135− getAllPosts(fields, key, sort).forEach((post, index, array) => {
136− if (post.slug === slug) {
137− if (typeof array[index + 1] !== "undefined") {
138− resultPost = array[index + 1];
145export function formatDateWithTime(d) {
86export function formatDateWithTime(d) {
146 const date = DateTime.fromISO(d, {
87 const date = DateTime.fromISO(d, {
147 setZone: true,
88 setZone: true,
@@ -149,73 +90,11 @@ export function formatDateWithTime(d) {
149 return date;
90 return date;
152−export function capitalize(str) {
153− return str.charAt(0).toUpperCase() + str.slice(1);
156−// Takes an ISO 8601 date string
157−// Returns a time relative to the IANA timezone and iso8601 date
158−export function generateDisplayDate(iso8601, zone = "America/Los_Angeles") {
159− return DateTime.fromISO(iso8601, { zone });
162−// Takes an ISO 8601 date string
163−// Returns a time relative to user's current timezone
164−export function generateRealtimeDate(iso8601) {
165− return DateTime.fromISO(iso8601).setZone("system");
168// Formats a DateTime object to something like "July 15, 2021, 5:00 PM"
93// Formats a DateTime object to something like "July 15, 2021, 5:00 PM"
169export function formatDateAndTime(dateTimeObject) {
94export function formatDateAndTime(dateTimeObject) {
170 return dateTimeObject.toLocaleString(DateTime.DATETIME_HUGE);
95 return dateTimeObject.toLocaleString(DateTime.DATETIME_HUGE);
173−// Formats a DateTime object to something like "5:00 PM"
174−export function formatTime(dateTimeObject) {
175− const dt = dateTimeObject.toLocaleString(DateTime.TIME_SIMPLE);
176− return dt.replace(/:00/g, "");
179−export function formatTimeZone(dateTimeObject) {
180− return dateTimeObject.offsetNameShort;
183−// Formats a DateTime object to something like "Jul 15, 2021"
184−export function formatDate(dateTimeObject) {
185− return dateTimeObject.toLocaleString(DateTime.DATE_FULL);
188−// export function generateCrumbs(path) {
189−// const crumbs = path.split('/')
191−// .filter(v => v !== '')
192−// .map((crumb, index) => ({
193−// name: capitalize(crumb),
195−// path: crumbs.slice(0, index+2).join('/'),
199−export const getPage = (path) => {
201− let fileContents = fs.readFileSync(`${path}.md`, "utf8");
203− const { data, content } = matter(fileContents, options);
204− return { data, content };
208− let fileContents = fs.readFileSync(`${path}/_index.md`, "utf8");
210− const { data, content } = matter(fileContents, options);
211− return { data, content };
214− console.error("no md file for slug");
219// Returns an array of unique grants categories
98// Returns an array of unique grants categories
220export function getGrantsCategories() {
99export function getGrantsCategories() {
221 const paths = fs.readdirSync(directories("grants"));
100 const paths = fs.readdirSync(directories("grants"));