summary refs log tree commit diff
path: root/src/getPosts.ts
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 22:47:37 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 22:47:37 +0200
commit241ceeba4ad203b1e0711490344e970a3a452d07 (patch)
treeeab68a6b859c8e8804ddf5d989f9582c3724153f /src/getPosts.ts
parent590a6776063807be20f8cfaf2de9059bbbc559c1 (diff)
downloadkronikarz-241ceeba4ad203b1e0711490344e970a3a452d07.tar.gz
kronikarz-241ceeba4ad203b1e0711490344e970a3a452d07.zip
Add simple parsing
Diffstat (limited to 'src/getPosts.ts')
-rw-r--r--src/getPosts.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/getPosts.ts b/src/getPosts.ts
new file mode 100644
index 0000000..893946d
--- /dev/null
+++ b/src/getPosts.ts
@@ -0,0 +1,62 @@
+import * as fs from "fs";
+import { JSDOM } from "jsdom";
+import { Post } from "./interfaces";
+
+const frontmatter = require("front-matter");
+const md = require("markdown-it")();
+
+function getPostAttributes(fileContent: string) {
+  const post = frontmatter(fileContent);
+
+  const { document } = new JSDOM(`<body>${md.render(post.body)}</body>`).window;
+  const element = document.getElementsByTagName("p");
+
+  post.body = `<div>${md.render(post.body)}</div>`;
+  post.description = element[1].textContent;
+
+  return post;
+}
+
+function getPosts(path: string) {
+  let routesArray: Post[] = [];
+  try {
+    const years = fs.readdirSync(`${path}`);
+    console.log(years);
+    years.forEach((year: string) => {
+      const months = fs.readdirSync(`${path}/${year}`);
+      months.forEach((month: string) => {
+        const days = fs.readdirSync(`${path}/${year}/${month}`);
+        days.forEach((day: string) => {
+          const files = fs.readdirSync(`${path}/${year}/${month}/${day}`);
+          files.forEach((file: string) => {
+            const title = file.substr(0, file.lastIndexOf("."));
+            const route = `/kronika/${year}/${month}/${day}/${title}/`;
+            const fsRoute = `${path}/${year}/${month}/${day}/${file}`;
+
+            const data = getPostAttributes(fs.readFileSync(fsRoute, "utf-8"));
+
+            const post = {
+              date: { year, month, day },
+              title,
+              data,
+              file,
+              fsRoute,
+              route
+            };
+
+            routesArray.push(post);
+          });
+        });
+      });
+    });
+  } finally {
+    return routesArray;
+  }
+}
+
+function createRoutesArray() {
+  // let posts = getPosts();
+  // return posts.map(post => post.route);
+}
+
+export { getPosts, createRoutesArray, getPostAttributes };