summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 17:53:55 +0100
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-01-28 17:53:55 +0100
commit1a5e99c7972968cc384c8dd94bbb92f9c9beac3b (patch)
treeeeb47ac09b408e386b13cfc93ec9da582d17fbaa
parent74ee8edb555ef128112378f828c636bc04533b31 (diff)
downloadkronikarz-1a5e99c7972968cc384c8dd94bbb92f9c9beac3b.tar.gz
kronikarz-1a5e99c7972968cc384c8dd94bbb92f9c9beac3b.zip
Add categories
-rw-r--r--lib/generateApi.ts28
-rw-r--r--lib/interfaces.ts4
-rw-r--r--test/generateApi.spec.ts11
-rw-r--r--test/index.spec.ts2
-rw-r--r--test/samples/2019/12/12/category.md13
5 files changed, 53 insertions, 5 deletions
diff --git a/lib/generateApi.ts b/lib/generateApi.ts
index cade7e4..0024d9f 100644
--- a/lib/generateApi.ts
+++ b/lib/generateApi.ts
@@ -2,6 +2,7 @@ import fs from 'fs';
 import path from 'path';
 import Post from './Post';
 import { dateToPath } from './utils';
+import { Object as PlainObject } from './interfaces';
 
 function mkDirByPathSync(targetDir: string) {
   const sep = path.sep;
@@ -49,7 +50,7 @@ export function saveApiEntry(post: Post, path: string) {
 }
 
 /*
- * This function generates paged api index on path `/api/paged-posts`
+ * This function generates paged api index on path `/api/posts`
  */
 function generateApiIndex(posts: Array<Post>, path: string) {
   const parsedPosts = posts.map((post) => {
@@ -63,9 +64,34 @@ function generateApiIndex(posts: Array<Post>, path: string) {
   );
 }
 
+function generateCategories(posts: Array<Post>, path: string) {
+  const categories: PlainObject = {};
+  posts.forEach((post) => {
+    const apiEntry = post.toApi();
+    delete apiEntry.content;
+    if (apiEntry.meta['category']) {
+      apiEntry.meta['category'].forEach((category: string) =>
+        categories[category]
+          ? categories[category].push(apiEntry)
+          : (categories[category] = [apiEntry])
+      );
+    }
+  });
+
+  const dir = `${path}/api/category`;
+  mkDirByPathSync(dir);
+  Object.keys(categories).forEach((category) => {
+    const filePath = `${dir}/${category}.json`;
+    fs.writeFile(filePath, JSON.stringify(categories[category]), (err) =>
+      err ? console.log(err) : null
+    );
+  });
+}
+
 export function generateApi(posts: Array<Post>, path: string) {
   try {
     generateApiIndex(posts, path);
+    generateCategories(posts, path);
     posts.forEach((post) => saveApiEntry(post, path));
   } catch (err) {
     console.log(err);
diff --git a/lib/interfaces.ts b/lib/interfaces.ts
index 158ac1d..1fbb226 100644
--- a/lib/interfaces.ts
+++ b/lib/interfaces.ts
@@ -11,7 +11,7 @@ export interface Object {
 export interface Meta {
   title: string;
   author: string;
-  additionalMeta: object;
+  additionalMeta: Object;
 }
 
 export interface FrontMatterObject {
@@ -25,7 +25,7 @@ export interface PostApiListEntry {
   title: string;
   path: string;
   description: string;
-  meta: object;
+  meta: Object;
 }
 
 export interface PostApiEntry extends PostApiListEntry {
diff --git a/test/generateApi.spec.ts b/test/generateApi.spec.ts
index bf63b6e..f22c8e4 100644
--- a/test/generateApi.spec.ts
+++ b/test/generateApi.spec.ts
@@ -2,12 +2,21 @@ import * as fs from "fs";
 import Kronikarz from "../dist";
 
 const k = new Kronikarz(__dirname + "/samples");
+const dir = "./tmp";
 
 test("generate single file", () => {
-  const dir = "./tmp";
   k.generateApi(dir);
   fs.readFile("./tmp/api/posts/2019/12/12/test.json", "utf-8", (err, data) => {
     const parsedPost = JSON.parse(data);
     expect(parsedPost.title).toBe("Test");
   });
 });
+
+test("categories", () => {
+    k.generateApi(dir)
+    fs.readFile(`${dir}/api/category/test-category.json`, "utf-8", (err, data) => {
+        const posts = JSON.parse(data)
+        expect(posts.length).toBe(1)
+        expect(posts[0].content).toBe(undefined)
+    })
+})
diff --git a/test/index.spec.ts b/test/index.spec.ts
index 4fd65e1..4622724 100644
--- a/test/index.spec.ts
+++ b/test/index.spec.ts
@@ -2,5 +2,5 @@ import Kronikarz from "../dist";
 
 test("basic", () => {
   const k = new Kronikarz(__dirname + "/samples");
-  expect(k.getPosts().length).toBe(1);
+  expect(k.getPosts().length).toBe(2);
 });
diff --git a/test/samples/2019/12/12/category.md b/test/samples/2019/12/12/category.md
new file mode 100644
index 0000000..e7f04e7
--- /dev/null
+++ b/test/samples/2019/12/12/category.md
@@ -0,0 +1,13 @@
+---
+title: Category
+author: Tester
+category:
+    - test-category
+---
+
+# Category
+
+01-01-2020 | Tester
+
+This file is for testing categories
+