summary refs log tree commit diff
path: root/src/index.ts
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 21:40:57 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 21:40:57 +0200
commita6d36484b73aaf45daf0aa8c371d651962141af3 (patch)
tree1477de1cc41da9cf5bba40203af54a492d90eaa6 /src/index.ts
downloadkronikarz-a6d36484b73aaf45daf0aa8c371d651962141af3.tar.gz
kronikarz-a6d36484b73aaf45daf0aa8c371d651962141af3.zip
Init
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..b6a2b78
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,77 @@
+import * as fs from "fs";
+import { JSDOM } from "jsdom";
+
+const frontmatter = require("front-matter");
+const md = require("markdown-it")();
+
+const POSTS_PATH = "./content/wpisy";
+
+interface Date {
+  year: string;
+  month: string;
+  day: string;
+}
+
+interface Post {
+  date: Date;
+  title: string;
+  data: Object;
+  file: string;
+  fsRoute: string;
+  route: string;
+}
+
+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() {
+  let routesArray: Post[] = [];
+  try {
+    const years = fs.readdirSync(`${POSTS_PATH}`);
+    years.forEach((year: string) => {
+      const months = fs.readdirSync(`${POSTS_PATH}/${year}`);
+      months.forEach((month: string) => {
+        const days = fs.readdirSync(`${POSTS_PATH}/${year}/${month}`);
+        days.forEach((day: string) => {
+          const files = fs.readdirSync(`${POSTS_PATH}/${year}/${month}/${day}`);
+          files.forEach((file: string) => {
+            const title = file.substr(0, file.lastIndexOf("."));
+            const route = `/kronika/${year}/${month}/${day}/${title}/`;
+            const fsRoute = `${POSTS_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 };