From e348914bf5e511ff6862229d365ca5d2cece8e3b Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 22 Feb 2023 22:47:04 +1100 Subject: [PATCH] refactor: move json feed rendering to separated module (#11938) --- lib/utils/render.js | 48 ++------------------------------------------- lib/views/json.js | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 lib/views/json.js diff --git a/lib/utils/render.js b/lib/utils/render.js index 35d7159da..813f631c2 100644 --- a/lib/utils/render.js +++ b/lib/utils/render.js @@ -1,53 +1,9 @@ const art = require('art-template'); +const json = require('@/views/json'); // We may add more control over it later -/** - * This function should be used by RSSHub middleware only. - * @param {object} data ctx.state.data - * @returns `JSON.stringify`-ed [JSON Feed](https://www.jsonfeed.org/) - */ -const json = (data) => { - const jsonFeed = { - version: 'https://jsonfeed.org/version/1.1', - title: data.title || 'RSSHub', - home_page_url: data.link || 'https://docs.rsshub.app', - feed_url: data.feedLink, - description: `${data.description || data.title} - Made with love by RSSHub(https://github.com/DIYgod/RSSHub)`, - icon: data.image, - authors: typeof data.author === 'string' ? [{ name: data.author }] : data.author, - language: data.language || 'zh-cn', - items: data.item.map((item) => ({ - id: item.guid || item.id || item.link, - url: item.link, - title: item.title, - content_html: (item.content && item.content.html) || item.description || item.title, - content_text: item.content && item.content.text, - image: item.image, - banner_image: item.banner, - date_published: item.pubDate, - date_modified: item.updated, - authors: typeof item.author === 'string' ? [{ name: item.author }] : item.author, - tags: typeof item.category === 'string' ? [item.category] : item.category, - language: item.language, - attachments: item.enclosure_url - ? [ - { - url: item.enclosure_url, - mime_type: item.enclosure_type, - title: item.enclosure_title, - size_in_bytes: item.enclosure_length, - duration_in_seconds: item.itunes_duration, - }, - ] - : undefined, - _extra: item._extra || undefined, - })), - }; - return JSON.stringify(jsonFeed, null, 4); -}; - module.exports = { art, - json, + json, // This should be used by RSSHub middleware only. }; diff --git a/lib/views/json.js b/lib/views/json.js new file mode 100644 index 000000000..6b936a532 --- /dev/null +++ b/lib/views/json.js @@ -0,0 +1,46 @@ +/** + * This function should be used by RSSHub middleware only. + * @param {object} data ctx.state.data + * @returns `JSON.stringify`-ed [JSON Feed](https://www.jsonfeed.org/) + */ +const json = (data) => { + const jsonFeed = { + version: 'https://jsonfeed.org/version/1.1', + title: data.title || 'RSSHub', + home_page_url: data.link || 'https://docs.rsshub.app', + feed_url: data.feedLink, + description: `${data.description || data.title} - Made with love by RSSHub(https://github.com/DIYgod/RSSHub)`, + icon: data.image, + authors: typeof data.author === 'string' ? [{ name: data.author }] : data.author, + language: data.language || 'zh-cn', + items: data.item.map((item) => ({ + id: item.guid || item.id || item.link, + url: item.link, + title: item.title, + content_html: (item.content && item.content.html) || item.description || item.title, + content_text: item.content && item.content.text, + image: item.image, + banner_image: item.banner, + date_published: item.pubDate, + date_modified: item.updated, + authors: typeof item.author === 'string' ? [{ name: item.author }] : item.author, + tags: typeof item.category === 'string' ? [item.category] : item.category, + language: item.language, + attachments: item.enclosure_url + ? [ + { + url: item.enclosure_url, + mime_type: item.enclosure_type, + title: item.enclosure_title, + size_in_bytes: item.enclosure_length, + duration_in_seconds: item.itunes_duration, + }, + ] + : undefined, + _extra: item._extra || undefined, + })), + }; + return JSON.stringify(jsonFeed, null, 4); +}; + +module.exports = json;