summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-08-07 21:55:16 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2020-08-07 21:55:16 +0200
commit6116769757ddd88781129e7339fc7ff60a259337 (patch)
tree362f6512b72b38d1d7a796783b60fe5f24fadb4a /lib
parentc941b9a7066f6fe5cfebf159feb7d00cd316f64d (diff)
downloadkronikarz-6116769757ddd88781129e7339fc7ff60a259337.tar.gz
kronikarz-6116769757ddd88781129e7339fc7ff60a259337.zip
Add rss feed generation
Diffstat (limited to 'lib')
-rw-r--r--lib/generateRss.ts26
-rw-r--r--lib/index.ts17
-rw-r--r--lib/interfaces.ts6
3 files changed, 45 insertions, 4 deletions
diff --git a/lib/generateRss.ts b/lib/generateRss.ts
new file mode 100644
index 0000000..2f341c0
--- /dev/null
+++ b/lib/generateRss.ts
@@ -0,0 +1,26 @@
+import * as fs from "fs"
+import RSS from "rss";
+
+import { FeedOptions } from "./interfaces";
+import Post from "./Post";
+
+export function generateRss(posts: Array<Post>, feed_path: string, feedOptions: FeedOptions) {
+  const feed = new RSS(feedOptions);
+  const parsedPosts = posts.forEach((post) => {
+    const { title, content, path, date, author } = post.toApi();
+    feed.item({
+      title,
+      description: content,
+      url: feedOptions.site_url + path,
+      guid: path,
+      author,
+      date,
+    })
+  })
+  const xml = feed.xml();
+  fs.writeFile(feed_path, xml, (err) => {
+    if (err) {
+      console.log(err);
+    }
+  })
+}
diff --git a/lib/index.ts b/lib/index.ts
index 4bf4dd3..3eddf7a 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -1,6 +1,8 @@
+import { FeedOptions } from "./interfaces";
 import { getPost as apiGetPost } from "./getPost";
 import { getPosts as apiGetPosts } from "./getPosts";
-import { generateApi as apiGenerateApi } from "./generateApi"
+import { generateApi as apiGenerateApi } from "./generateApi";
+import { generateRss as apiGenerateRss } from "./generateRss";
 import Post from "./Post";
 
 export default class Kronikarz {
@@ -13,11 +15,18 @@ 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);
   }
-    generateApi(path: string) {
-      const posts = this.getPosts();
-      apiGenerateApi(posts, path)
+
+  generateApi(path: string) {
+    const posts = this.getPosts();
+    apiGenerateApi(posts, path)
+  }
+
+  generateRss(path: string, feedOptions: FeedOptions) {
+    const posts = this.getPosts();
+    apiGenerateRss(posts, path, feedOptions);
   }
 }
diff --git a/lib/interfaces.ts b/lib/interfaces.ts
index 1fbb226..931f712 100644
--- a/lib/interfaces.ts
+++ b/lib/interfaces.ts
@@ -31,3 +31,9 @@ export interface PostApiListEntry {
 export interface PostApiEntry extends PostApiListEntry {
   content: string;
 }
+
+export interface FeedOptions {
+  title: string;
+  feed_url: string;
+  site_url: string;
+}