Skip to content

Commit

Permalink
added RSS generating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr-Bondarev committed Sep 7, 2022
1 parent 45ad383 commit bc7f8ff
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 54 deletions.
3 changes: 3 additions & 0 deletions next-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ yarn-error.log*

# local env files
.env*.local

#RSS
/public/feed.xml
42 changes: 42 additions & 0 deletions next-app/api/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'path';
import fsp from 'fs/promises';
import fs from 'fs';

import matter from 'gray-matter';
import remarkGfm from 'remark-gfm';
import rehypePrism from '@mapbox/rehype-prism';
import { serialize } from 'next-mdx-remote/serialize';
import { Feed } from 'feed';
import findLastIndex from 'lodash.findlastindex';
import capitalize from 'lodash.capitalize';

Expand Down Expand Up @@ -93,3 +95,43 @@ export const findPost = async (name, locale) => {
content: compiledSource,
};
};

export const generateRssFeed = async (locale) => {
const posts = await getPublishedPosts(locale);
const postsToShow = posts.filter(({ hidden = false }) => !hidden);

const siteURL = 'http://localhost:3000';
const date = new Date();

const feed = new Feed({
title: "Hexlet Guides",
description: "Полезные статьи и гайды для разработчиков",
id: siteURL,
link: siteURL,
language: locale,
image: `${siteURL}/favicon.ico`,
favicon: `${siteURL}/favicon.ico`,
updated: date, // today's date
feedLinks: {
rss2: `${siteURL}/feed.xml`,
},
author: 'Kirill Mokevnin',
});

postsToShow.forEach((post) => {
feed.addItem({
title: post.header,
id: post.sourceUrl,
link: post.sourceUrl,
description: post.summary,
content: post.content,
author: {
name: post.author,
},
// image: post.image,
// date: new Date(post.date),
});
});

fs.writeFileSync("./public/feed.xml", feed.rss2());
};
49 changes: 49 additions & 0 deletions next-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mapbox/rehype-prism": "^0.8.0",
"bootstrap": "^5.1.3",
"disqus-react": "^1.1.3",
"feed": "^4.2.2",
"file-loader": "^6.2.0",
"gray-matter": "^4.0.3",
"lodash.capitalize": "^4.2.1",
Expand Down
53 changes: 0 additions & 53 deletions next-app/pages/feed.xml.js

This file was deleted.

3 changes: 2 additions & 1 deletion next-app/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

import { getPostsList } from '../api/index.js';
import { getPostsList, generateRssFeed } from '../api/index.js';
import DefaultLayout from '../components/DefaultLayout.jsx';
import HomePageInfo from '../components/HomePageInfo.jsx';

Expand All @@ -18,6 +18,7 @@ const Home = ({ posts }) => {
export const getStaticProps = async ({ locale }) => ({
props: {
posts: await getPostsList(locale),
...await generateRssFeed(locale),
...(await serverSideTranslations(locale, ['common'])),
},
});
Expand Down

0 comments on commit bc7f8ff

Please sign in to comment.