diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-13 22:47:37 +0200 |
---|---|---|
committer | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-10-13 22:47:37 +0200 |
commit | 241ceeba4ad203b1e0711490344e970a3a452d07 (patch) | |
tree | eab68a6b859c8e8804ddf5d989f9582c3724153f | |
parent | 590a6776063807be20f8cfaf2de9059bbbc559c1 (diff) | |
download | kronikarz-241ceeba4ad203b1e0711490344e970a3a452d07.tar.gz kronikarz-241ceeba4ad203b1e0711490344e970a3a452d07.zip |
Add simple parsing
-rw-r--r-- | docs/README.md | 18 | ||||
-rw-r--r-- | jest.config.js | 1 | ||||
-rw-r--r-- | src/__tests__/index.spec.ts | 6 | ||||
-rw-r--r-- | src/__tests__/samples/2019/12/12/test.md | 11 | ||||
-rw-r--r-- | src/getPosts.ts | 62 | ||||
-rw-r--r-- | src/index.ts | 81 | ||||
-rw-r--r-- | src/interfaces.ts | 16 | ||||
-rw-r--r-- | test/index.spec.js | 0 |
8 files changed, 123 insertions, 72 deletions
diff --git a/docs/README.md b/docs/README.md index 76d000e..616fbb7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1 +1,19 @@ # Kronikarz + +Required folder structure: + +``` +year +└── month + └── title + └── title.md +``` + +To parse all posts run: + +```js +import Kronikarz from "kronikarz"; + +const k = new Kronikarz("path/to/posts"); +k.getPosts(); +``` diff --git a/jest.config.js b/jest.config.js index 4f26d08..8effa4a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,4 +3,5 @@ module.exports = { transform: { "^.+\\.tsx?$": "ts-jest" } + // testMatch: ["**/test/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"] }; diff --git a/src/__tests__/index.spec.ts b/src/__tests__/index.spec.ts new file mode 100644 index 0000000..64ce82d --- /dev/null +++ b/src/__tests__/index.spec.ts @@ -0,0 +1,6 @@ +import Kronikarz from ".."; + +test("basic", () => { + const k = new Kronikarz("./src/__tests__/samples"); + console.log(k.getPosts()); +}); diff --git a/src/__tests__/samples/2019/12/12/test.md b/src/__tests__/samples/2019/12/12/test.md new file mode 100644 index 0000000..5fc0158 --- /dev/null +++ b/src/__tests__/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/src/getPosts.ts b/src/getPosts.ts new file mode 100644 index 0000000..893946d --- /dev/null +++ b/src/getPosts.ts @@ -0,0 +1,62 @@ +import * as fs from "fs"; +import { JSDOM } from "jsdom"; +import { Post } from "./interfaces"; + +const frontmatter = require("front-matter"); +const md = require("markdown-it")(); + +function getPostAttributes(fileContent: string) { + const post = frontmatter(fileContent); + + const { document } = new JSDOM(`<body>${md.render(post.body)}</body>`).window; + const element = document.getElementsByTagName("p"); + + post.body = `<div>${md.render(post.body)}</div>`; + post.description = element[1].textContent; + + return post; +} + +function getPosts(path: string) { + let routesArray: Post[] = []; + try { + const years = fs.readdirSync(`${path}`); + console.log(years); + years.forEach((year: string) => { + const months = fs.readdirSync(`${path}/${year}`); + months.forEach((month: string) => { + const days = fs.readdirSync(`${path}/${year}/${month}`); + days.forEach((day: string) => { + const files = fs.readdirSync(`${path}/${year}/${month}/${day}`); + files.forEach((file: string) => { + const title = file.substr(0, file.lastIndexOf(".")); + const route = `/kronika/${year}/${month}/${day}/${title}/`; + const fsRoute = `${path}/${year}/${month}/${day}/${file}`; + + const data = getPostAttributes(fs.readFileSync(fsRoute, "utf-8")); + + const post = { + date: { year, month, day }, + title, + data, + file, + fsRoute, + route + }; + + routesArray.push(post); + }); + }); + }); + }); + } finally { + return routesArray; + } +} + +function createRoutesArray() { + // let posts = getPosts(); + // return posts.map(post => post.route); +} + +export { getPosts, createRoutesArray, getPostAttributes }; diff --git a/src/index.ts b/src/index.ts index b6a2b78..61e38c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,77 +1,14 @@ -import * as fs from "fs"; -import { JSDOM } from "jsdom"; +import { getPosts as apiGetPosts } from "./getPosts"; +import { Post } from "./interfaces"; -const frontmatter = require("front-matter"); -const md = require("markdown-it")(); +export default class Kronikarz { + postPath: string; -const POSTS_PATH = "./content/wpisy"; - -interface Date { - year: string; - month: string; - day: string; -} - -interface Post { - date: Date; - title: string; - data: Object; - file: string; - fsRoute: string; - route: string; -} - -function getPostAttributes(fileContent: string) { - const post = frontmatter(fileContent); - - const { document } = new JSDOM(`<body>${md.render(post.body)}</body>`).window; - const element = document.getElementsByTagName("p"); - - post.body = `<div>${md.render(post.body)}</div>`; - post.description = element[1].textContent; - - return post; -} - -function getPosts() { - let routesArray: Post[] = []; - try { - const years = fs.readdirSync(`${POSTS_PATH}`); - years.forEach((year: string) => { - const months = fs.readdirSync(`${POSTS_PATH}/${year}`); - months.forEach((month: string) => { - const days = fs.readdirSync(`${POSTS_PATH}/${year}/${month}`); - days.forEach((day: string) => { - const files = fs.readdirSync(`${POSTS_PATH}/${year}/${month}/${day}`); - files.forEach((file: string) => { - const title = file.substr(0, file.lastIndexOf(".")); - const route = `/kronika/${year}/${month}/${day}/${title}/`; - const fsRoute = `${POSTS_PATH}/${year}/${month}/${day}/${file}`; - - const data = getPostAttributes(fs.readFileSync(fsRoute, "utf-8")); - - const post = { - date: { year, month, day }, - title, - data, - file, - fsRoute, - route - }; - - routesArray.push(post); - }); - }); - }); - }); - } finally { - return routesArray; + constructor(postPath: string) { + this.postPath = postPath; } -} -function createRoutesArray() { - let posts = getPosts(); - return posts.map(post => post.route); + getPosts(): Array<Post> { + return apiGetPosts(this.postPath); + } } - -export { getPosts, createRoutesArray, getPostAttributes }; diff --git a/src/interfaces.ts b/src/interfaces.ts new file mode 100644 index 0000000..b6c8d73 --- /dev/null +++ b/src/interfaces.ts @@ -0,0 +1,16 @@ +interface Date { + year: string; + month: string; + day: string; +} + +interface Post { + date: Date; + title: string; + data: Object; + file: string; + fsRoute: string; + route: string; +} + +export { Post }; diff --git a/test/index.spec.js b/test/index.spec.js deleted file mode 100644 index e69de29..0000000 --- a/test/index.spec.js +++ /dev/null |