From a8a358723f6075fb447ed31d629cbe5629bc7185 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Fri, 15 Nov 2019 19:21:35 +0300 Subject: [PATCH 1/4] feat(v2): add meta RSS/Atom feed links to head --- .../src/theme/Layout/index.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/docusaurus-theme-classic/src/theme/Layout/index.js b/packages/docusaurus-theme-classic/src/theme/Layout/index.js index a73d95b592fa..295b555a3a30 100644 --- a/packages/docusaurus-theme-classic/src/theme/Layout/index.js +++ b/packages/docusaurus-theme-classic/src/theme/Layout/index.js @@ -22,6 +22,7 @@ function Layout(props) { title: defaultTitle, themeConfig: {image: defaultImage}, url: siteUrl, + presets, } = siteConfig; const { children, @@ -36,6 +37,10 @@ function Layout(props) { const metaImage = image || defaultImage; const metaImageUrl = siteUrl + useBaseUrl(metaImage); const faviconUrl = useBaseUrl(favicon); + const hasBlog = presets.some(preset => (preset[1] || {}).blog !== undefined); + const rssFeedLink = siteUrl + useBaseUrl('blog/feed.xml'); + const atomFeedLink = siteUrl + useBaseUrl('blog/atom.xml'); + return ( <> @@ -57,6 +62,22 @@ function Layout(props) { )} {permalink && } + {hasBlog && ( + + )} + {hasBlog && ( + + )}
{children}
From 0e22ad5e2f47b4729502a1b0688c83585e1299c0 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 30 Nov 2019 17:44:31 +0300 Subject: [PATCH 2/4] refactor(v2): use new API --- .../src/index.ts | 74 ++++++++++++++++--- .../src/theme/Layout/index.js | 21 ------ 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index 3e6f31ef48b9..c7aeb4848085 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -41,6 +41,18 @@ const DEFAULT_OPTIONS: PluginOptions = { truncateMarker: //, // string or regex }; +const getFeedTypes = ({type}: {type: string}) => { + let feedTypes = []; + + if (type === 'all') { + feedTypes = ['rss', 'atom']; + } else { + feedTypes.push(type); + } + + return feedTypes; +}; + export default function pluginContentBlog( context: LoadContext, opts: Partial, @@ -384,19 +396,13 @@ export default function pluginContentBlog( return; } - const { - feedOptions: {type: feedType}, - } = options; const feed = await generateBlogFeed(context, options); + if (!feed) { return; } - let feedTypes = []; - if (feedType === 'all') { - feedTypes = ['rss', 'atom']; - } else { - feedTypes.push(feedType); - } + + const feedTypes = getFeedTypes(options.feedOptions); await Promise.all( feedTypes.map(feedType => { @@ -414,5 +420,55 @@ export default function pluginContentBlog( }), ); }, + + injectHtmlTags() { + if (!options.feedOptions) { + return; + } + + const feedTypes = getFeedTypes(options.feedOptions); + const { + siteConfig: {baseUrl = '', title}, + } = context; + const feedsConfig: { + [key: string]: {type: string; path: string; title: string}; + } = { + rss: { + type: 'application/rss+xml', + path: 'blog/rss.xml', + title: `${title} Blog RSS Feed`, + }, + atom: { + type: 'application/atom+xml', + path: 'blog/atom.xml', + title: `${title} Blog Atom Feed`, + }, + }; + const headTags: object[] = []; + + feedTypes.map(feedType => { + const feedConfig = feedsConfig[feedType] || {}; + + if (!feedsConfig) { + return; + } + + const {type, path, title} = feedConfig; + + headTags.push({ + tagName: 'link', + attributes: { + rel: 'alternate', + type, + href: normalizeUrl([baseUrl, path]), + title, + }, + }); + }); + + return { + headTags, + }; + }, }; } diff --git a/packages/docusaurus-theme-classic/src/theme/Layout/index.js b/packages/docusaurus-theme-classic/src/theme/Layout/index.js index be05ba5f6883..526b33cd35e5 100644 --- a/packages/docusaurus-theme-classic/src/theme/Layout/index.js +++ b/packages/docusaurus-theme-classic/src/theme/Layout/index.js @@ -22,7 +22,6 @@ function Layout(props) { title: defaultTitle, themeConfig: {image: defaultImage}, url: siteUrl, - presets, } = siteConfig; const { children, @@ -38,10 +37,6 @@ function Layout(props) { const metaImage = image || defaultImage; const metaImageUrl = siteUrl + useBaseUrl(metaImage); const faviconUrl = useBaseUrl(favicon); - const hasBlog = presets.some(preset => (preset[1] || {}).blog !== undefined); - const rssFeedLink = siteUrl + useBaseUrl('blog/feed.xml'); - const atomFeedLink = siteUrl + useBaseUrl('blog/atom.xml'); - return ( <> @@ -64,22 +59,6 @@ function Layout(props) { )} {permalink && } - {hasBlog && ( - - )} - {hasBlog && ( - - )}
{children}
From 8c206028c04e562e0799b6d68bb3dca4aedd6a3e Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 30 Nov 2019 18:11:34 +0300 Subject: [PATCH 3/4] Better typing --- packages/docusaurus-plugin-content-blog/src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index c7aeb4848085..b4e20b943a9d 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -23,6 +23,7 @@ import { PluginContentLoadedActions, ConfigureWebpackUtils, Props, + HtmlTags, } from '@docusaurus/types'; import {Configuration} from 'webpack'; import {generateBlogFeed, generateBlogPosts} from './blogUtils'; @@ -41,7 +42,7 @@ const DEFAULT_OPTIONS: PluginOptions = { truncateMarker: //, // string or regex }; -const getFeedTypes = ({type}: {type: string}) => { +const getFeedTypes = ({type}: {type: 'rss' | 'atom' | 'all'}) => { let feedTypes = []; if (type === 'all') { @@ -444,7 +445,7 @@ export default function pluginContentBlog( title: `${title} Blog Atom Feed`, }, }; - const headTags: object[] = []; + const headTags: HtmlTags[] = []; feedTypes.map(feedType => { const feedConfig = feedsConfig[feedType] || {}; From 0bb95bda554a4afd5f969ffbde3fb640f62c1dcb Mon Sep 17 00:00:00 2001 From: endiliey Date: Sat, 30 Nov 2019 22:56:08 +0700 Subject: [PATCH 4/4] fix typing --- .../src/index.ts | 30 ++++++++++++------- .../src/types.ts | 4 ++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index 00181213cbb4..6371d3a1a914 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -17,6 +17,7 @@ import { BlogItemsToModules, TagsModule, BlogPaginated, + FeedType, } from './types'; import { LoadContext, @@ -43,15 +44,23 @@ const DEFAULT_OPTIONS: PluginOptions = { truncateMarker: //, // string or regex }; -const getFeedTypes = ({type}: {type: 'rss' | 'atom' | 'all'}) => { - let feedTypes = []; +function assertFeedTypes(val: any): asserts val is FeedType { + if (typeof val !== 'string' && !['rss', 'atom', 'all'].includes(val)) { + throw new Error( + `Invalid feedOptions type: ${val}. It must be either 'rss', 'atom', or 'all'`, + ); + } +} + +const getFeedTypes = (type?: FeedType) => { + assertFeedTypes(type); + let feedTypes: ('rss' | 'atom')[] = []; if (type === 'all') { feedTypes = ['rss', 'atom']; } else { feedTypes.push(type); } - return feedTypes; }; @@ -408,7 +417,7 @@ export default function pluginContentBlog( return; } - const feedTypes = getFeedTypes(options.feedOptions); + const feedTypes = getFeedTypes(options.feedOptions?.type); await Promise.all( feedTypes.map(feedType => { @@ -429,16 +438,15 @@ export default function pluginContentBlog( injectHtmlTags() { if (!options.feedOptions) { - return; + return {}; } - const feedTypes = getFeedTypes(options.feedOptions); + const feedTypes = getFeedTypes(options.feedOptions?.type); const { - siteConfig: {baseUrl = '', title}, + siteConfig: {title}, + baseUrl, } = context; - const feedsConfig: { - [key: string]: {type: string; path: string; title: string}; - } = { + const feedsConfig = { rss: { type: 'application/rss+xml', path: 'blog/rss.xml', @@ -450,7 +458,7 @@ export default function pluginContentBlog( title: `${title} Blog Atom Feed`, }, }; - const headTags: HtmlTags[] = []; + const headTags: HtmlTags = []; feedTypes.map(feedType => { const feedConfig = feedsConfig[feedType] || {}; diff --git a/packages/docusaurus-plugin-content-blog/src/types.ts b/packages/docusaurus-plugin-content-blog/src/types.ts index d6cb5c8de25c..2c5350b5fa05 100644 --- a/packages/docusaurus-plugin-content-blog/src/types.ts +++ b/packages/docusaurus-plugin-content-blog/src/types.ts @@ -17,6 +17,8 @@ export interface DateLink { link: string; } +export type FeedType = 'rss' | 'atom' | 'all'; + export interface PluginOptions { path: string; routeBasePath: string; @@ -30,7 +32,7 @@ export interface PluginOptions { rehypePlugins: string[]; truncateMarker: RegExp | string; feedOptions?: { - type: 'rss' | 'atom' | 'all'; + type: FeedType; title?: string; description?: string; copyright: string;