Skip to content
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

fix(blog): apply trailing slash to blog feed #9920

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import logger from '@docusaurus/logger';
import {Feed, type Author as FeedAuthor} from 'feed';
import * as srcset from 'srcset';
import {normalizeUrl, readOutputHTMLFile} from '@docusaurus/utils';
import {blogPostContainerID} from '@docusaurus/utils-common';
import {
blogPostContainerID,
applyTrailingSlash,
} from '@docusaurus/utils-common';
import {load as cheerioLoad} from 'cheerio';
import type {DocusaurusConfig} from '@docusaurus/types';
import type {
Expand Down Expand Up @@ -40,8 +43,14 @@ async function generateBlogFeed({
}

const {feedOptions, routeBasePath} = options;
const {url: siteUrl, baseUrl, title, favicon} = siteConfig;
const blogBaseUrl = normalizeUrl([siteUrl, baseUrl, routeBasePath]);
const {url: siteUrl, baseUrl, title, favicon, trailingSlash} = siteConfig;
const blogBaseUrl = applyTrailingSlash(
normalizeUrl([siteUrl, baseUrl, routeBasePath]),
{
trailingSlash,
baseUrl,
},
);

const blogPostsForFeed =
feedOptions.limit === false || feedOptions.limit === null
Expand Down Expand Up @@ -85,7 +94,7 @@ async function defaultCreateFeedItems({
siteConfig: DocusaurusConfig;
outDir: string;
}): Promise<BlogFeedItem[]> {
const {url: siteUrl} = siteConfig;
const {url: siteUrl, baseUrl, trailingSlash} = siteConfig;

function toFeedAuthor(author: Author): FeedAuthor {
return {name: author.name, link: author.url, email: author.email};
Expand All @@ -105,13 +114,19 @@ async function defaultCreateFeedItems({
} = post;

const content = await readOutputHTMLFile(
permalink.replace(siteConfig.baseUrl, ''),
permalink.replace(baseUrl, ''),
outDir,
siteConfig.trailingSlash,
trailingSlash,
);
const $ = cheerioLoad(content);

const blogPostAbsoluteUrl = normalizeUrl([siteUrl, permalink]);
const blogPostAbsoluteUrl = applyTrailingSlash(
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
normalizeUrl([siteUrl, permalink]),
{
trailingSlash,
baseUrl,
},
);

const toAbsoluteUrl = (src: string) =>
String(new URL(src, blogPostAbsoluteUrl));
Expand Down
Loading