diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-14 09:47:28 +0200 |
---|---|---|
committer | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-14 09:47:51 +0200 |
commit | 49e82e812812ba75175d98173379d2e416e17b8f (patch) | |
tree | f67bdcffce9aca8a76b534124a0eabbe85658768 | |
parent | 262ebdc07892f3c892d4831c276444df6f6e22f1 (diff) | |
download | kronikarz-49e82e812812ba75175d98173379d2e416e17b8f.tar.gz kronikarz-49e82e812812ba75175d98173379d2e416e17b8f.zip |
Add getPost
-rw-r--r-- | docs/README.md | 5 | ||||
-rw-r--r-- | jest.config.js | 2 | ||||
-rw-r--r-- | lib/getPost.ts | 19 | ||||
-rw-r--r-- | lib/getPosts.ts | 2 | ||||
-rw-r--r-- | lib/index.ts | 4 | ||||
-rw-r--r-- | lib/interfaces.ts | 6 | ||||
-rw-r--r-- | lib/parsePost.ts | 2 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/getPost.spec.ts | 8 | ||||
-rw-r--r-- | test/index.spec.ts (renamed from lib/__tests__/index.spec.ts) | 3 | ||||
-rw-r--r-- | test/samples/2019/12/12/test.md (renamed from lib/__tests__/samples/2019/12/12/test.md) | 0 | ||||
-rw-r--r-- | tsconfig.json | 3 |
12 files changed, 45 insertions, 11 deletions
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: ["<rootDir>/src"], transform: { "^.+\\.tsx?$": "ts-jest" } - // testMatch: ["**/test/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"] }; 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<Post> { 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<Post> { 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/lib/__tests__/index.spec.ts b/test/index.spec.ts index 52e28ba..4fd65e1 100644 --- a/lib/__tests__/index.spec.ts +++ b/test/index.spec.ts @@ -1,7 +1,6 @@ -import Kronikarz from ".."; +import Kronikarz from "../dist"; 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/test/samples/2019/12/12/test.md index 5fc0158..5fc0158 100644 --- a/lib/__tests__/samples/2019/12/12/test.md +++ b/test/samples/2019/12/12/test.md 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"] } |