on master help sign in
lib: first pass at flattened file tree of relative paths and metadata
commit793af6acea45ca6cccee67e7560af90cdfea819e authorMatilde Park <matilde@tlon.io> dateWed 16 Jun 2021, 18:26 (5 years ago) parent7480a24
1 file changed+118−65
lib/lib.js+118−65
lib/lib.js+118−65
@@ -1,9 +1,9 @@
1−import remark from 'remark'
2−import html from 'remark-html'
3−import fs from 'fs'
4−import { join } from 'path'
5−import matter from 'gray-matter'
6−import toml from '@iarna/toml'
1+import remark from "remark";
2+import html from "remark-html";
3+import fs from "fs";
4+import { join, parse } from "path";
5+import matter from "gray-matter";
6+import toml from "@iarna/toml";
77
88// export async function markdownToHtml(markdown) {
99// const result = await remark().use(html).process(markdown)
@@ -11,13 +11,13 @@ import toml from '@iarna/toml'
1111// }
1212
1313const directories = {
14− blog: join(process.cwd(), 'content/blog'),
15− events: join(process.cwd(), 'content/events'),
16− docs: join(process.cwd(), 'content/docs'),
17−}
14+ blog: join(process.cwd(), "content/blog"),
15+ events: join(process.cwd(), "content/events"),
16+ docs: join(process.cwd(), "content/docs"),
17+};
1818
1919export function getPostSlugs(key) {
20− return fs.readdirSync(directories[key])
20+ return fs.readdirSync(directories[key]);
2121}
2222
2323// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
@@ -25,79 +25,87 @@ const options = {
2525 engines: {
2626 toml: toml.parse.bind(toml),
2727 },
28− language: 'toml',
29− delimiters: '+++'
30−}
28+ language: "toml",
29+ delimiters: "+++",
30+};
3131
3232export function getPostBySlug(slug, fields = [], key) {
33− const realSlug = slug.replace(/\.md$/, '')
34− const fullPath = join(directories[key], `${realSlug}.md`)
35− const fileContents = fs.readFileSync(fullPath, 'utf8')
36− const { data, content } = matter(fileContents, options)
37− const items = {}
38−
33+ const realSlug = slug.replace(/\.md$/, "");
34+ const fullPath = join(directories[key], `${realSlug}.md`);
35+ const fileContents = fs.readFileSync(fullPath, "utf8");
36+ const { data, content } = matter(fileContents, options);
37+ const items = {};
38+
3939 // Ensure only the minimal needed data is exposed
4040 fields.forEach((field) => {
41−
42− if (field === 'slug') {
43− items[field] = realSlug
44− }
45−
46− if (field === 'content') {
47− items[field] = content
48− }
49−
50− if (data[field]) {
51− items[field] = data[field]
52− }
53−
54− })
55−
41+ if (field === "slug") {
42+ items[field] = realSlug;
43+ }
44+
45+ if (field === "content") {
46+ items[field] = content;
47+ }
48+
49+ if (data[field]) {
50+ items[field] = data[field];
51+ }
52+ });
53+
5654 // 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.
57− items.date = JSON.stringify(items.date).replace(/\"/g, '');
58−
55+ items.date = JSON.stringify(items.date).replace(/\"/g, "");
5956
60− return items
57+ return items;
6158}
6259
6360export function getAllPosts(fields = [], key) {
64− const slugs = getPostSlugs(key)
61+ const slugs = getPostSlugs(key);
6562 const posts = slugs
66− .map((slug) => getPostBySlug(slug, fields, key))
67− // sort posts by date in descending order
68− .sort((post1, post2) => (post1.date > post2.date ? -1 : 1))
69−
70− return posts
63+ .map((slug) => getPostBySlug(slug, fields, key))
64+ // sort posts by date in descending order
65+ .sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
66+
67+ return posts;
7168}
7269
7370export function getNextPost(slug, fields = [], key) {
74− let resultPost = null
75−
76− getAllPosts(fields, key)
77− .forEach((post, index, array) => {
78− if (post.slug === slug) {
79− resultPost = array[index - 1]
80− }
81− })
82− return resultPost
71+ let resultPost = null;
72+
73+ getAllPosts(fields, key).forEach((post, index, array) => {
74+ if (post.slug === slug) {
75+ resultPost = array[index - 1];
76+ }
77+ });
78+ return resultPost;
8379}
8480
8581export function getPreviousPost(slug, fields = [], key) {
86− let resultPost = null
87−
88− getAllPosts(fields, key)
89− .forEach((post, index, array) => {
90− if (post.slug === slug) {
91− resultPost = array[index + 1]
92− }
93− })
94− return resultPost
82+ let resultPost = null;
83+
84+ getAllPosts(fields, key).forEach((post, index, array) => {
85+ if (post.slug === slug) {
86+ resultPost = array[index + 1];
87+ }
88+ });
89+ return resultPost;
9590}
9691
9792export function formatDate(d) {
9893 // const wk = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
99− const mn =["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
100− return mn[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear()
94+ const mn = [
95+ "January",
96+ "February",
97+ "March",
98+ "April",
99+ "May",
100+ "June",
101+ "July",
102+ "August",
103+ "September",
104+ "October",
105+ "November",
106+ "December",
107+ ];
108+ return mn[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
101109}
102110
103111// export function capitalize(str) {
@@ -113,4 +121,49 @@ export function formatDate(d) {
113121// key: crumb,
114122// path: crumbs.slice(0, index+2).join('/'),
115123// }))
116−// }
124+// }
125+
126+export const getAllFiles = function (dirPath, arrayOfFiles) {
127+ let files = fs.readdirSync(dirPath);
128+
129+ arrayOfFiles = arrayOfFiles || [];
130+
131+ files.forEach(function (file) {
132+ if (fs.statSync(dirPath + "/" + file).isDirectory()) {
133+ arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
134+ } else {
135+ arrayOfFiles.push(join(__dirname, dirPath, "/", file));
136+ }
137+ });
138+
139+ return arrayOfFiles;
140+};
141+
142+export function getLinksinSubdirs(path) {
143+ const allFiles = getAllFiles(path);
144+ const fileTree = {};
145+ allFiles.forEach((e) => {
146+ const parsed = parse(e.substr(e.indexOf("content/") + 7));
147+
148+ if (!(parsed.dir in fileTree)) {
149+ const fullPathofDir = join(
150+ process.cwd(),
151+ "/content",
152+ parsed.dir,
153+ "_index.md"
154+ );
155+ const dirContents = fs.readFileSync(fullPathofDir, "utf8");
156+ const { data: dirData } = matter(dirContents, options);
157+ fileTree[parsed.dir] = { name: dirData.title, pages: [] };
158+ }
159+
160+ const fileContents = fs.readFileSync(e, "utf8");
161+ const { data } = matter(fileContents, options);
162+ fileTree[parsed.dir].pages.push({
163+ name: data.title,
164+ base: parsed.base,
165+ slug: parsed.name,
166+ });
167+ });
168+ return fileTree;
169+}