-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#365 created page with xml(rss) #432
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
|
||
#RSS | ||
/public/feed.xml |
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'; | ||||||
|
||||||
|
@@ -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", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Такие параметры лучше брать из конфига |
||||||
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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не используемый код лучше удалить There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Еще кажется, что дата публикации все же должна быть:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пост не содержит информацию о дате публикации. В каждом файле есть ключи title, subtitle, description, image и author. Или дату можно получить как-то ещё? |
||||||
// date: new Date(post.date), | ||||||
}); | ||||||
}); | ||||||
|
||||||
fs.writeFileSync("./public/feed.xml", feed.rss2()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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'; | ||
|
||
|
@@ -18,6 +18,7 @@ const Home = ({ posts }) => { | |
export const getStaticProps = async ({ locale }) => ({ | ||
props: { | ||
posts: await getPostsList(locale), | ||
...await generateRssFeed(locale), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть ощущение, что генерация должна выполняться не здесь, а в отдельной команде перед деплоем (см. как сделан sitemap) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seth2810 Привет, я попытался сделать пребилд скрипт (“prebuild”: “node <путь к скрипту>”), но так как в package.json не указан “type”: ”module”, команда падает с ошибкой. Так как в скрипте используется код, который выполняется в рамках рантайма next.js, все функции (с зависимостями) переписать на CommonJS модули тоже не вышло. Попытки использовать расширение .mjs и флаги типа —experimental-modules так же не привели к успеху. Пожалуйста, подскажи, как можно решить эту проблему There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Aleksandr-Bondarev предлагаю тебе запушить свой код, пусть и не рабочий, чтобы я мог смотреть предметно 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Aleksandr-Bondarev учитывая, что будет необходимо генерировать фиды под разные локали, нашел другой способ позволяющий это сделать более привычным для next.js путем. Еще там выяснилось, что обычный |
||
...(await serverSideTranslations(locale, ['common'])), | ||
}, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Кажется так быть не должно - https://github.com/Hexlet/hexletguides.github.io/blob/gh-pages/feed.xml#L8: