Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,7 @@ exports[`getContentTranslationFiles returns translation files matching snapshot

exports[`translateContent falls back when translation is incomplete 1`] = `
{
"blogListPaginated": [
{
"items": [
"hello",
],
"metadata": {
"blogDescription": "Someone's random blog",
"blogTitle": "My blog",
"nextPage": undefined,
"page": 1,
"permalink": "/",
"postsPerPage": 10,
"previousPage": undefined,
"totalCount": 1,
"totalPages": 1,
},
},
],
"blogDescription": "Someone's random blog",
"blogPosts": [
{
"content": "",
Expand All @@ -63,29 +46,13 @@ exports[`translateContent falls back when translation is incomplete 1`] = `
"blogSidebarTitle": "All my posts",
"blogTags": {},
"blogTagsListPath": "/tags",
"blogTitle": "My blog",
}
`;

exports[`translateContent returns translated loaded 1`] = `
{
"blogListPaginated": [
{
"items": [
"hello",
],
"metadata": {
"blogDescription": "Someone's random blog (translated)",
"blogTitle": "My blog (translated)",
"nextPage": undefined,
"page": 1,
"permalink": "/",
"postsPerPage": 10,
"previousPage": undefined,
"totalCount": 1,
"totalPages": 1,
},
},
],
"blogDescription": "Someone's random blog (translated)",
"blogPosts": [
{
"content": "",
Expand All @@ -107,5 +74,6 @@ exports[`translateContent returns translated loaded 1`] = `
"blogSidebarTitle": "All my posts (translated)",
"blogTags": {},
"blogTagsListPath": "/tags",
"blogTitle": "My blog (translated)",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,7 @@ describe('blog plugin', () => {
},
DefaultI18N,
);
const {blogPosts, blogTags, blogListPaginated} =
(await plugin.loadContent!())!;

expect(blogListPaginated).toHaveLength(3);
const {blogPosts, blogTags} = (await plugin.loadContent!())!;

expect(Object.keys(blogTags)).toHaveLength(2);
expect(blogTags).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {updateTranslationFileMessages} from '@docusaurus/utils';
import {fromPartial} from '@total-typescript/shoehorn';
import {getTranslationFiles, translateContent} from '../translations';
import {DEFAULT_OPTIONS} from '../options';
import type {
Expand All @@ -16,13 +17,13 @@ import type {

const sampleBlogOptions: PluginOptions = {
...DEFAULT_OPTIONS,
blogSidebarTitle: 'All my posts',
blogTitle: 'My blog',
blogDescription: "Someone's random blog",
blogSidebarTitle: 'All my posts',
};

const sampleBlogPosts: BlogPost[] = [
{
fromPartial({
id: 'hello',
metadata: {
permalink: '/blog/2021/06/19/hello',
Expand All @@ -37,27 +38,13 @@ const sampleBlogPosts: BlogPost[] = [
unlisted: false,
},
content: '',
},
}),
];

const sampleBlogContent: BlogContent = {
blogTitle: sampleBlogOptions.blogTitle,
blogDescription: sampleBlogOptions.blogDescription,
blogSidebarTitle: sampleBlogOptions.blogSidebarTitle,
blogListPaginated: [
{
items: ['hello'],
metadata: {
permalink: '/',
page: 1,
postsPerPage: 10,
totalPages: 1,
totalCount: 1,
previousPage: undefined,
nextPage: undefined,
blogTitle: sampleBlogOptions.blogTitle,
blogDescription: sampleBlogOptions.blogDescription,
},
},
],
blogPosts: sampleBlogPosts,
blogTags: {},
blogTagsListPath: '/tags',
Expand Down
20 changes: 5 additions & 15 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import {
createAbsoluteFilePathMatcher,
getContentPathList,
getDataFilePath,
DEFAULT_PLUGIN_ID,
resolveMarkdownLinkPathname,
getLocaleConfig,
} from '@docusaurus/utils';
import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
import {
getBlogTags,
paginateBlogPosts,
shouldBeListed,
applyProcessBlogPosts,
generateBlogPosts,
Expand All @@ -45,7 +43,6 @@ import type {
Assets,
BlogTags,
BlogContent,
BlogPaginated,
} from '@docusaurus/plugin-content-blog';
import type {RuleSetRule, RuleSetUseItem} from 'webpack';

Expand Down Expand Up @@ -85,7 +82,7 @@ export default async function pluginContentBlog(
})
: undefined,
};
const pluginId = options.id ?? DEFAULT_PLUGIN_ID;
const pluginId = options.id;

const pluginDataDirRoot = path.join(generatedFilesDir, PluginName);
const dataDir = path.join(pluginDataDirRoot, pluginId);
Expand Down Expand Up @@ -260,9 +257,10 @@ export default async function pluginContentBlog(

if (!blogPosts.length) {
return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts: [],
blogListPaginated: [],
blogTags: {},
blogTagsListPath,
authorsMap,
Expand Down Expand Up @@ -291,15 +289,6 @@ export default async function pluginContentBlog(
}
});

const blogListPaginated: BlogPaginated[] = paginateBlogPosts({
blogPosts: listedBlogPosts,
blogTitle,
blogDescription,
postsPerPageOption,
basePageUrl: baseBlogUrl,
pageBasePath,
});

const blogTags: BlogTags = getBlogTags({
blogPosts,
postsPerPageOption,
Expand All @@ -309,9 +298,10 @@ export default async function pluginContentBlog(
});

return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
authorsMap,
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
RouteBasePathSchema,
URISchema,
} from '@docusaurus/utils-validation';
import {GlobExcludeDefault} from '@docusaurus/utils';
import {DEFAULT_PLUGIN_ID, GlobExcludeDefault} from '@docusaurus/utils';
import type {
PluginOptions,
Options,
Expand All @@ -25,6 +25,7 @@ import type {
import type {OptionValidationContext} from '@docusaurus/types';

export const DEFAULT_OPTIONS: PluginOptions = {
id: DEFAULT_PLUGIN_ID,
feedOptions: {
type: ['rss', 'atom'],
copyright: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ declare module '@docusaurus/plugin-content-blog' {
export type PluginOptions = MDXOptions &
TagsPluginOptions & {
/** Plugin ID. */
id?: string;
id: string;
/**
* Path to the blog content directory on the file system, relative to site
* directory.
Expand Down Expand Up @@ -583,9 +583,10 @@ declare module '@docusaurus/plugin-content-blog' {
export type AuthorsMap = {[authorKey: string]: AuthorWithKey};

export type BlogContent = {
blogSidebarTitle: string;
blogTitle: string; // for translation purposes
blogDescription: string; // for translation purposes
blogSidebarTitle: string; // for translation purposes
blogPosts: BlogPost[];
blogListPaginated: BlogPaginated[];
blogTags: BlogTags;
blogTagsListPath: string;
authorsMap?: AuthorsMap;
Expand Down
48 changes: 30 additions & 18 deletions packages/docusaurus-plugin-content-blog/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,24 @@ export async function buildAllRoutes({
blogArchiveComponent,
routeBasePath,
archiveBasePath,
blogTitle,
authorsBasePath,
postsPerPage,
blogDescription,
pageBasePath,
} = options;
const pluginId = options.id!;
const pluginId = options.id;
const {createData} = actions;
const {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
authorsMap,
} = content;

const authorsListPath = normalizeUrl([
baseUrl,
routeBasePath,
authorsBasePath,
]);
const blogBasePath = normalizeUrl([baseUrl, routeBasePath]);
const authorsListPath = normalizeUrl([blogBasePath, authorsBasePath]);

const listedBlogPosts = blogPosts.filter(shouldBeListed);

Expand Down Expand Up @@ -119,7 +116,7 @@ export async function buildAllRoutes({

async function createBlogMetadataModule() {
const blogMetadata: BlogMetadata = {
blogBasePath: normalizeUrl([baseUrl, routeBasePath]),
blogBasePath,
blogTitle,
authorsListPath,
};
Expand Down Expand Up @@ -156,7 +153,7 @@ export async function buildAllRoutes({
if (archiveBasePath && listedBlogPosts.length) {
return [
{
path: normalizeUrl([baseUrl, routeBasePath, archiveBasePath]),
path: normalizeUrl([blogBasePath, archiveBasePath]),
component: blogArchiveComponent,
exact: true,
props: {
Expand Down Expand Up @@ -210,6 +207,15 @@ export async function buildAllRoutes({
}

function createBlogPostsPaginatedRoutes(): RouteConfig[] {
const blogListPaginated = paginateBlogPosts({
blogPosts: listedBlogPosts,
blogTitle,
blogDescription,
postsPerPageOption: postsPerPage,
basePageUrl: blogBasePath,
pageBasePath,
});

return blogListPaginated.map((paginated) => {
return {
path: paginated.metadata.permalink,
Expand Down Expand Up @@ -294,12 +300,14 @@ export async function buildAllRoutes({
sidebar: sidebarModulePath,
},
props: {
authors: authors.map((author) =>
toAuthorItemProp({
authors: authors.map((author) => {
const authorPosts = blogPostsByAuthorKey[author.key] ?? [];
const listedAuthorPosts = authorPosts.filter(shouldBeListed);
return toAuthorItemProp({
author,
count: blogPostsByAuthorKey[author.key]?.length ?? 0,
}),
),
count: listedAuthorPosts.length,
});
}),
},
context: {
blogMetadata: blogMetadataModulePath,
Expand All @@ -309,12 +317,13 @@ export async function buildAllRoutes({

function createAuthorPaginatedRoute(author: AuthorWithKey): RouteConfig[] {
const authorBlogPosts = blogPostsByAuthorKey[author.key] ?? [];
const listedAuthorBlogPosts = authorBlogPosts.filter(shouldBeListed);
if (!author.page) {
return [];
}

const pages = paginateBlogPosts({
blogPosts: authorBlogPosts,
blogPosts: listedAuthorBlogPosts,
basePageUrl: author.page.permalink,
blogDescription,
blogTitle,
Expand All @@ -332,7 +341,10 @@ export async function buildAllRoutes({
sidebar: sidebarModulePath,
},
props: {
author: toAuthorItemProp({author, count: authorBlogPosts.length}),
author: toAuthorItemProp({
author,
count: listedAuthorBlogPosts.length,
}),
listMetadata: metadata,
},
context: {
Expand Down
Loading
Loading