diff options
Diffstat (limited to 'src/getPosts.ts')
-rw-r--r-- | src/getPosts.ts | 44 |
1 files changed, 9 insertions, 35 deletions
diff --git a/src/getPosts.ts b/src/getPosts.ts index 893946d..4170292 100644 --- a/src/getPosts.ts +++ b/src/getPosts.ts @@ -1,48 +1,22 @@ import * as fs from "fs"; -import { JSDOM } from "jsdom"; import { Post } from "./interfaces"; +import parsePost from "./parsePost"; +import { readDir } from "./utils"; -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) { +function getPosts(path: string): Array<Post> { let routesArray: Post[] = []; try { - const years = fs.readdirSync(`${path}`); - console.log(years); + const years = readDir(`${path}`); years.forEach((year: string) => { - const months = fs.readdirSync(`${path}/${year}`); + const months = readDir(`${path}/${year}`); months.forEach((month: string) => { - const days = fs.readdirSync(`${path}/${year}/${month}`); + const days = readDir(`${path}/${year}/${month}`); days.forEach((day: string) => { - const files = fs.readdirSync(`${path}/${year}/${month}/${day}`); + const files = readDir(`${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 - }; + const post = parsePost(fsRoute); routesArray.push(post); }); @@ -59,4 +33,4 @@ function createRoutesArray() { // return posts.map(post => post.route); } -export { getPosts, createRoutesArray, getPostAttributes }; +export { getPosts, createRoutesArray }; |