From 9f75ff1d2d5ebf34c040474e836e125f80253365 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Tue, 28 Jan 2020 11:55:03 +0100 Subject: Add Post class --- lib/interfaces.ts | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'lib/interfaces.ts') diff --git a/lib/interfaces.ts b/lib/interfaces.ts index 1d598c6..974765f 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -1,24 +1,33 @@ -interface Date { +export interface Date { year: string; month: string; day: string; } -interface Meta { - [key: string]: string; +/*export interface Meta { + [key: string]: object; +}*/ + +export interface Meta { + title: string; + author: string; + additionalMeta: object; } -export interface PostContent { - html: string; - markdown: string; - description: string; - meta: Meta; +export interface FrontMatterObject { + body: string; + attributes: Meta; } -export interface Post { - date: Date; +export interface PostApiListEntry { + date: string; + author: string; title: string; - content: PostContent; - filePath: string; - route: string; + path: string; + description: string; + meta: object; +} + +export interface PostApiEntry extends PostApiListEntry { + content: string; } -- cgit 1.4.1 From 74ee8edb555ef128112378f828c636bc04533b31 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Tue, 28 Jan 2020 12:18:47 +0100 Subject: Fix remove attributes --- lib/Post.ts | 9 ++++++--- lib/interfaces.ts | 10 +++++----- test/generateApi.spec.ts | 2 +- test/getPost.spec.ts | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) (limited to 'lib/interfaces.ts') diff --git a/lib/Post.ts b/lib/Post.ts index 27a6cb9..f99ef83 100644 --- a/lib/Post.ts +++ b/lib/Post.ts @@ -38,13 +38,16 @@ export default class Post { getMeta(): Meta { const { attributes } = this.post; const author: string = attributes.author; - delete attributes.author; + if (!author) throw 'Error while parsing the author'; const title: string = attributes.title; - delete attributes.title; + if (!title) throw 'Error while parsing the title'; + const additionalMeta = Object.keys(attributes) + .filter((key) => key != 'title' && key != 'author') + .reduce((acc, key) => ({ ...acc, [key]: attributes[key] }), {}); return { author, title, - additionalMeta: attributes, + additionalMeta, }; } diff --git a/lib/interfaces.ts b/lib/interfaces.ts index 974765f..158ac1d 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -4,19 +4,19 @@ export interface Date { day: string; } -/*export interface Meta { - [key: string]: object; -}*/ +export interface Object { + [key: string]: any; +} export interface Meta { title: string; author: string; - additionalMeta: object; + additionalMeta: object; } export interface FrontMatterObject { body: string; - attributes: Meta; + attributes: Object; } export interface PostApiListEntry { diff --git a/test/generateApi.spec.ts b/test/generateApi.spec.ts index c2a96e3..bf63b6e 100644 --- a/test/generateApi.spec.ts +++ b/test/generateApi.spec.ts @@ -8,6 +8,6 @@ test("generate single file", () => { 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"); + expect(parsedPost.title).toBe("Test"); }); }); diff --git a/test/getPost.spec.ts b/test/getPost.spec.ts index 72d7b5e..080bfbd 100644 --- a/test/getPost.spec.ts +++ b/test/getPost.spec.ts @@ -3,6 +3,7 @@ import Kronikarz from "../dist"; const k = new Kronikarz(__dirname + "/samples"); test("simple get", () => { - const post = k.getPost("2019", "12", "12", "test"); + const p = k.getPost("2019", "12", "12", "test"); + const post = p.toApi(); expect(post.title).toBe("Test"); }); -- cgit 1.4.1 From 1a5e99c7972968cc384c8dd94bbb92f9c9beac3b Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Tue, 28 Jan 2020 17:53:55 +0100 Subject: Add categories --- lib/generateApi.ts | 28 +++++++++++++++++++++++++++- lib/interfaces.ts | 4 ++-- test/generateApi.spec.ts | 11 ++++++++++- test/index.spec.ts | 2 +- test/samples/2019/12/12/category.md | 13 +++++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test/samples/2019/12/12/category.md (limited to 'lib/interfaces.ts') 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, path: string) { const parsedPosts = posts.map((post) => { @@ -63,9 +64,34 @@ function generateApiIndex(posts: Array, path: string) { ); } +function generateCategories(posts: Array, 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, 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 + -- cgit 1.4.1