summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 23:31:53 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2019-10-13 23:31:53 +0200
commit0ccea881106946b66d167eee0ec8116ae4e3019e (patch)
treef5bdf7501d97cf31a767ed91335145530db4f49d
parent241ceeba4ad203b1e0711490344e970a3a452d07 (diff)
downloadkronikarz-0ccea881106946b66d167eee0ec8116ae4e3019e.tar.gz
kronikarz-0ccea881106946b66d167eee0ec8116ae4e3019e.zip
Add parsePost
-rw-r--r--src/__tests__/index.spec.ts3
-rw-r--r--src/getPosts.ts44
-rw-r--r--src/interfaces.ts16
-rw-r--r--src/parsePost.ts45
-rw-r--r--src/utils.ts9
5 files changed, 75 insertions, 42 deletions
diff --git a/src/__tests__/index.spec.ts b/src/__tests__/index.spec.ts
index 64ce82d..52e28ba 100644
--- a/src/__tests__/index.spec.ts
+++ b/src/__tests__/index.spec.ts
@@ -1,6 +1,7 @@
 import Kronikarz from "..";
 
 test("basic", () => {
-  const k = new Kronikarz("./src/__tests__/samples");
+  const k = new Kronikarz(__dirname + "/samples");
   console.log(k.getPosts());
+  expect(k.getPosts().length).toBe(1);
 });
diff --git a/src/getPosts.ts b/src/getPosts.ts
index 893946d..4170292 100644
--- a/src/getPosts.ts
+++ b/src/getPosts.ts
@@ -1,48 +1,22 @@
 import * as fs from "fs";
-import { JSDOM } from "jsdom";
 import { Post } from "./interfaces";
+import parsePost from "./parsePost";
+import { readDir } from "./utils";
 
-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) {
+function getPosts(path: string): Array<Post> {
   let routesArray: Post[] = [];
   try {
-    const years = fs.readdirSync(`${path}`);
-    console.log(years);
+    const years = readDir(`${path}`);
     years.forEach((year: string) => {
-      const months = fs.readdirSync(`${path}/${year}`);
+      const months = readDir(`${path}/${year}`);
       months.forEach((month: string) => {
-        const days = fs.readdirSync(`${path}/${year}/${month}`);
+        const days = readDir(`${path}/${year}/${month}`);
         days.forEach((day: string) => {
-          const files = fs.readdirSync(`${path}/${year}/${month}/${day}`);
+          const files = readDir(`${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
-            };
+            const post = parsePost(fsRoute);
 
             routesArray.push(post);
           });
@@ -59,4 +33,4 @@ function createRoutesArray() {
   // return posts.map(post => post.route);
 }
 
-export { getPosts, createRoutesArray, getPostAttributes };
+export { getPosts, createRoutesArray };
diff --git a/src/interfaces.ts b/src/interfaces.ts
index b6c8d73..8c3493d 100644
--- a/src/interfaces.ts
+++ b/src/interfaces.ts
@@ -4,13 +4,17 @@ interface Date {
   day: string;
 }
 
-interface Post {
+export interface PostContent {
+  html: string;
+  markdown: string;
+  description: string;
+  meta: Object;
+}
+
+export interface Post {
   date: Date;
   title: string;
-  data: Object;
-  file: string;
-  fsRoute: string;
+  content: PostContent;
+  filePath: string;
   route: string;
 }
-
-export { Post };
diff --git a/src/parsePost.ts b/src/parsePost.ts
new file mode 100644
index 0000000..01e8d1c
--- /dev/null
+++ b/src/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/src/utils.ts b/src/utils.ts
new file mode 100644
index 0000000..23a3947
--- /dev/null
+++ b/src/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`;
+  }
+}