From 49e82e812812ba75175d98173379d2e416e17b8f Mon Sep 17 00:00:00 2001 From: Patryk Niedźwiedziński Date: Mon, 14 Oct 2019 09:47:28 +0200 Subject: Add getPost --- docs/README.md | 5 +++-- jest.config.js | 2 -- lib/__tests__/index.spec.ts | 7 ------- lib/__tests__/samples/2019/12/12/test.md | 11 ----------- lib/getPost.ts | 19 +++++++++++++++++++ lib/getPosts.ts | 2 +- lib/index.ts | 4 ++++ lib/interfaces.ts | 6 +++++- lib/parsePost.ts | 2 +- package.json | 2 +- test/getPost.spec.ts | 8 ++++++++ test/index.spec.ts | 6 ++++++ test/samples/2019/12/12/test.md | 11 +++++++++++ tsconfig.json | 3 ++- 14 files changed, 61 insertions(+), 27 deletions(-) delete mode 100644 lib/__tests__/index.spec.ts delete mode 100644 lib/__tests__/samples/2019/12/12/test.md create mode 100644 lib/getPost.ts create mode 100644 test/getPost.spec.ts create mode 100644 test/index.spec.ts create mode 100644 test/samples/2019/12/12/test.md diff --git a/docs/README.md b/docs/README.md index 4b82b43..ebfdaa0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,8 +28,9 @@ import Kronikarz from "kronikarz"; const k = new Kronikarz("path/to/posts"); ``` -At this point only one method is available. +At this point only two methods are available. ```js -k.getPosts(); +k.getPosts(); // returns array of all posts +k.getPost("2019", "11", "20", "title"); // returns post from path `2019/11/20/title.md` ``` diff --git a/jest.config.js b/jest.config.js index 8effa4a..1e9ddba 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,7 +1,5 @@ module.exports = { - roots: ["/src"], transform: { "^.+\\.tsx?$": "ts-jest" } - // testMatch: ["**/test/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"] }; diff --git a/lib/__tests__/index.spec.ts b/lib/__tests__/index.spec.ts deleted file mode 100644 index 52e28ba..0000000 --- a/lib/__tests__/index.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 5fc0158..0000000 --- a/lib/__tests__/samples/2019/12/12/test.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -title: Test -description: test test -author: Tester ---- - -# Test - -12-12-2019 | Tester - -test test test diff --git a/lib/getPost.ts b/lib/getPost.ts new file mode 100644 index 0000000..2555215 --- /dev/null +++ b/lib/getPost.ts @@ -0,0 +1,19 @@ +import { parsePost } from "./parsePost"; +import { Post } from "./interfaces"; + +interface getPostArgument { + year: string; + month: string; + day: string; + title: string; +} + +export function getPost( + { year, month, day, title }: getPostArgument, + path: string +): Post { + const filePath = `${path}/${year}/${month}/${day}/${title}.md`; + + const post = parsePost(filePath); + return post; +} diff --git a/lib/getPosts.ts b/lib/getPosts.ts index 69a353f..1821f12 100644 --- a/lib/getPosts.ts +++ b/lib/getPosts.ts @@ -1,5 +1,5 @@ import { Post } from "./interfaces"; -import parsePost from "./parsePost"; +import { parsePost } from "./parsePost"; import { readDir } from "./utils"; function getPosts(path: string): Array { diff --git a/lib/index.ts b/lib/index.ts index 61e38c8..ade7180 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,3 +1,4 @@ +import { getPost as apiGetPost } from "./getPost"; import { getPosts as apiGetPosts } from "./getPosts"; import { Post } from "./interfaces"; @@ -11,4 +12,7 @@ export default class Kronikarz { getPosts(): Array { return apiGetPosts(this.postPath); } + getPost(year: string, month: string, day: string, title: string): Post { + return apiGetPost({ year, month, day, title }, this.postPath); + } } diff --git a/lib/interfaces.ts b/lib/interfaces.ts index 8c3493d..1d598c6 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -4,11 +4,15 @@ interface Date { day: string; } +interface Meta { + [key: string]: string; +} + export interface PostContent { html: string; markdown: string; description: string; - meta: Object; + meta: Meta; } export interface Post { diff --git a/lib/parsePost.ts b/lib/parsePost.ts index 01e8d1c..416dd16 100644 --- a/lib/parsePost.ts +++ b/lib/parsePost.ts @@ -29,7 +29,7 @@ function getPostContent(fileContent: string): PostContent { }; } -export default function parsePost(filePath: string): Post { +export 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"); diff --git a/package.json b/package.json index c301df4..7a67237 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kronikarz", - "version": "0.1.1", + "version": "0.1.2", "description": "System elektronicznej kroniki", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/test/getPost.spec.ts b/test/getPost.spec.ts new file mode 100644 index 0000000..6a5e4e0 --- /dev/null +++ b/test/getPost.spec.ts @@ -0,0 +1,8 @@ +import Kronikarz from "../dist"; + +const k = new Kronikarz(__dirname + "/samples"); + +test("simple get", () => { + const post = k.getPost("2019", "12", "12", "test"); + expect(post.content.meta["title"]).toBe("Test"); +}); diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000..4fd65e1 --- /dev/null +++ b/test/index.spec.ts @@ -0,0 +1,6 @@ +import Kronikarz from "../dist"; + +test("basic", () => { + const k = new Kronikarz(__dirname + "/samples"); + expect(k.getPosts().length).toBe(1); +}); diff --git a/test/samples/2019/12/12/test.md b/test/samples/2019/12/12/test.md new file mode 100644 index 0000000..5fc0158 --- /dev/null +++ b/test/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/tsconfig.json b/tsconfig.json index cc0e6fa..31f9494 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -59,5 +59,6 @@ /* Experimental Options */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - } + }, + "exclude": ["test", "dist"] } -- cgit 1.4.1