From 88c1e8f2621612e146740536b7d82c697b5251a1 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Sun, 10 Jan 2021 08:59:29 +0100 Subject: Rename to shell.nix --- default.nix | 9 --------- shell.nix | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) delete mode 100644 default.nix create mode 100644 shell.nix diff --git a/default.nix b/default.nix deleted file mode 100644 index 65e3443..0000000 --- a/default.nix +++ /dev/null @@ -1,9 +0,0 @@ -let - pkgs = import { }; -in - with pkgs; - mkShell { - buildInputs = [ - nodejs - ]; - } diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..65e3443 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +let + pkgs = import { }; +in + with pkgs; + mkShell { + buildInputs = [ + nodejs + ]; + } -- cgit 1.4.1 From 61a831565a49be3daafd4cb523caa1d2823a2b59 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Sun, 10 Jan 2021 09:02:27 +0100 Subject: Add generatePages --- lib/generateApi.ts | 44 ++++++++++++++++++++++++++++++++++++++------ test/generatePages.spec.ts | 13 +++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 test/generatePages.spec.ts diff --git a/lib/generateApi.ts b/lib/generateApi.ts index 0024d9f..c8b20ce 100644 --- a/lib/generateApi.ts +++ b/lib/generateApi.ts @@ -49,21 +49,52 @@ export function saveApiEntry(post: Post, path: string) { ); } +//#Source https://bit.ly/2neWfJ2 +function chunk (arr: Array, size: number): Array> { + return Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => + arr.slice(i * size, i * size + size) + ); +} + +function parsePosts(post: Post) { + const apiEntry = post.toApi(); + delete apiEntry.content; + return apiEntry; +} + /* - * This function generates paged api index on path `/api/posts` + * This function generates api index on path `/api/posts` */ function generateApiIndex(posts: Array, path: string) { - const parsedPosts = posts.map((post) => { - const apiEntry = post.toApi(); - delete apiEntry.content; - return apiEntry; - }); + const parsedPosts = posts.map(parsePosts); const filePath = `${path}/api/posts.json`; fs.writeFile(filePath, JSON.stringify(parsedPosts), (err) => err ? console.log(err) : null ); } +/* + * This function generates pages on path `/api/page/` + */ +function generatePages(posts: Array, path: string, len: number = 8) { + const parsedPosts = posts.map(parsePosts); + const pages = chunk(posts, len); + mkDirByPathSync(`${path}/api/page`); + pages.forEach((posts: Array, index: number) => { + const pageNumber = index + 1; + const page = { posts, next : pageNumber + 1 }; + if (pageNumber == pages.length) { + delete page.next; + } + fs.writeFile(`${path}/api/page/${pageNumber}.json`, JSON.stringify(page), (err) => + err ? console.log(err) : null + ); + }) +} + + + + function generateCategories(posts: Array, path: string) { const categories: PlainObject = {}; posts.forEach((post) => { @@ -92,6 +123,7 @@ export function generateApi(posts: Array, path: string) { try { generateApiIndex(posts, path); generateCategories(posts, path); + generatePages(posts, path); posts.forEach((post) => saveApiEntry(post, path)); } catch (err) { console.log(err); diff --git a/test/generatePages.spec.ts b/test/generatePages.spec.ts new file mode 100644 index 0000000..2312e48 --- /dev/null +++ b/test/generatePages.spec.ts @@ -0,0 +1,13 @@ +import * as fs from "fs"; +import Kronikarz from "../dist"; + +const k = new Kronikarz(__dirname + "/samples"); +const dir = "./tmp"; + +test("generate pages", () => { + k.generateApi(dir); + fs.readFile("./tmp/api/page/1.json", "utf-8", (err, data) => { + const page = JSON.parse(data); + expect(page.posts.length).toBe(2); + }); +}) -- cgit 1.4.1 From 3bbeb33e6df1aac21ec2e92931f4628b992f4013 Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Sun, 10 Jan 2021 09:03:01 +0100 Subject: Version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1a12e34..09d3705 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kronikarz", - "version": "2.2.0", + "version": "2.3.0", "description": "System elektronicznej kroniki", "main": "dist/index.js", "types": "dist/index.d.ts", -- cgit 1.4.1