summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 23:48:12 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 23:48:12 +0200
commit4274595b251632ef004d5f1429d7fbdde1cd4a5a (patch)
treec8a5ac120ddd03e161ecac522b1180f3a815cf70 /lib
parent0ccea881106946b66d167eee0ec8116ae4e3019e (diff)
downloadkronikarz-4274595b251632ef004d5f1429d7fbdde1cd4a5a.tar.gz
kronikarz-4274595b251632ef004d5f1429d7fbdde1cd4a5a.zip
Add publish options
Diffstat (limited to 'lib')
-rw-r--r--lib/__tests__/index.spec.ts7
-rw-r--r--lib/__tests__/samples/2019/12/12/test.md11
-rw-r--r--lib/getPosts.ts36
-rw-r--r--lib/index.ts14
-rw-r--r--lib/interfaces.ts20
-rw-r--r--lib/parsePost.ts45
-rw-r--r--lib/utils.ts9
7 files changed, 142 insertions, 0 deletions
diff --git a/lib/__tests__/index.spec.ts b/lib/__tests__/index.spec.ts
new file mode 100644
index 0000000..52e28ba
--- /dev/null
+++ b/lib/__tests__/index.spec.ts
@@ -0,0 +1,7 @@
+import Kronikarz from "..";
+
+test("basic", () => {
+  const k = new Kronikarz(__dirname + "/samples");
+  console.log(k.getPosts());
+  expect(k.getPosts().length).toBe(1);
+});
diff --git a/lib/__tests__/samples/2019/12/12/test.md b/lib/__tests__/samples/2019/12/12/test.md
new file mode 100644
index 0000000..5fc0158
--- /dev/null
+++ b/lib/__tests__/samples/2019/12/12/test.md
@@ -0,0 +1,11 @@
+---
+title: Test
+description: test test
+author: Tester
+---
+
+# Test
+
+12-12-2019 | Tester
+
+test test test
diff --git a/lib/getPosts.ts b/lib/getPosts.ts
new file mode 100644
index 0000000..4170292
--- /dev/null
+++ b/lib/getPosts.ts
@@ -0,0 +1,36 @@
+import * as fs from "fs";
+import { Post } from "./interfaces";
+import parsePost from "./parsePost";
+import { readDir } from "./utils";
+
+function getPosts(path: string): Array<Post> {
+  let routesArray: Post[] = [];
+  try {
+    const years = readDir(`${path}`);
+    years.forEach((year: string) => {
+      const months = readDir(`${path}/${year}`);
+      months.forEach((month: string) => {
+        const days = readDir(`${path}/${year}/${month}`);
+        days.forEach((day: string) => {
+          const files = readDir(`${path}/${year}/${month}/${day}`);
+          files.forEach((file: string) => {
+            const fsRoute = `${path}/${year}/${month}/${day}/${file}`;
+
+            const post = parsePost(fsRoute);
+
+            routesArray.push(post);
+          });
+        });
+      });
+    });
+  } finally {
+    return routesArray;
+  }
+}
+
+function createRoutesArray() {
+  // let posts = getPosts();
+  // return posts.map(post => post.route);
+}
+
+export { getPosts, createRoutesArray };
diff --git a/lib/index.ts b/lib/index.ts
new file mode 100644
index 0000000..61e38c8
--- /dev/null
+++ b/lib/index.ts
@@ -0,0 +1,14 @@
+import { getPosts as apiGetPosts } from "./getPosts";
+import { Post } from "./interfaces";
+
+export default class Kronikarz {
+  postPath: string;
+
+  constructor(postPath: string) {
+    this.postPath = postPath;
+  }
+
+  getPosts(): Array<Post> {
+    return apiGetPosts(this.postPath);
+  }
+}
diff --git a/lib/interfaces.ts b/lib/interfaces.ts
new file mode 100644
index 0000000..8c3493d
--- /dev/null
+++ b/lib/interfaces.ts
@@ -0,0 +1,20 @@
+interface Date {
+  year: string;
+  month: string;
+  day: string;
+}
+
+export interface PostContent {
+  html: string;
+  markdown: string;
+  description: string;
+  meta: Object;
+}
+
+export interface Post {
+  date: Date;
+  title: string;
+  content: PostContent;
+  filePath: string;
+  route: string;
+}
diff --git a/lib/parsePost.ts b/lib/parsePost.ts
new file mode 100644
index 0000000..01e8d1c
--- /dev/null
+++ b/lib/parsePost.ts
@@ -0,0 +1,45 @@
+import * as fs from "fs";
+import { JSDOM } from "jsdom";
+import { Post, PostContent } from "./interfaces";
+
+const frontmatter = require("front-matter");
+const md = require("markdown-it")();
+
+function getDescription(html: string): string {
+  const { document } = new JSDOM(`<div>${html}</div>`).window;
+  const elements = document.getElementsByTagName("p");
+
+  const description = elements[1].textContent;
+
+  return description || "";
+}
+
+function getPostContent(fileContent: string): PostContent {
+  const post = frontmatter(fileContent);
+
+  const markdown = post.body;
+  const html = `<div>${md.render(markdown)}</div>`;
+  const description = getDescription(html);
+
+  return {
+    html,
+    markdown,
+    description,
+    meta: post.attributes
+  };
+}
+
+export default function parsePost(filePath: string): Post {
+  const [year, month, day, title] = filePath.split("/").splice(-4, 4);
+  const date = { year, month, day };
+  const fileContent = fs.readFileSync(filePath, "utf-8");
+  const content = getPostContent(fileContent);
+
+  return {
+    date,
+    title: title.substr(0, title.lastIndexOf(".")),
+    content,
+    filePath,
+    route: `/kronika/${year}/${month}/${day}/${title}`
+  };
+}
diff --git a/lib/utils.ts b/lib/utils.ts
new file mode 100644
index 0000000..23a3947
--- /dev/null
+++ b/lib/utils.ts
@@ -0,0 +1,9 @@
+import * as fs from "fs";
+
+export function readDir(path: string): Array<string> {
+  if (fs.existsSync(path)) {
+    return fs.readdirSync(path);
+  } else {
+    throw `Path "${path}" doesn't exist`;
+  }
+}