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
17 changes: 4 additions & 13 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
import {
getBlogTags,
paginateBlogPosts,
shouldBeListed,
applyProcessBlogPosts,
generateBlogPosts,
Expand All @@ -45,7 +44,6 @@ import type {
Assets,
BlogTags,
BlogContent,
BlogPaginated,
} from '@docusaurus/plugin-content-blog';
import type {RuleSetRule, RuleSetUseItem} from 'webpack';

Expand Down Expand Up @@ -260,9 +258,10 @@ export default async function pluginContentBlog(

if (!blogPosts.length) {
return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts: [],
blogListPaginated: [],
blogTags: {},
blogTagsListPath,
authorsMap,
Expand Down Expand Up @@ -291,15 +290,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 +299,10 @@ export default async function pluginContentBlog(
});

return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
authorsMap,
Expand Down
Original file line number Diff line number Diff line change
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
26 changes: 16 additions & 10 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 {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
37 changes: 7 additions & 30 deletions packages/docusaurus-plugin-content-blog/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import type {TranslationFileContent, TranslationFile} from '@docusaurus/types';
import type {
PluginOptions,
BlogContent,
BlogPaginated,
} from '@docusaurus/plugin-content-blog';

function translateListPage(
blogListPaginated: BlogPaginated[],
translations: TranslationFileContent,
) {
return blogListPaginated.map((page) => {
const {items, metadata} = page;
return {
items,
metadata: {
...metadata,
blogTitle: translations.title?.message ?? page.metadata.blogTitle,
blogDescription:
translations.description?.message ?? page.metadata.blogDescription,
},
};
});
}
import type {TranslationFile} from '@docusaurus/types';
import type {PluginOptions, BlogContent} from '@docusaurus/plugin-content-blog';

export function getTranslationFiles(options: PluginOptions): TranslationFile[] {
return [
Expand Down Expand Up @@ -56,14 +34,13 @@ export function translateContent(
content: BlogContent,
translationFiles: TranslationFile[],
): BlogContent {
const {content: optionsTranslations} = translationFiles[0]!;
const {content: translations} = translationFiles[0]!;
return {
...content,
blogTitle: translations.title?.message ?? content.blogTitle,
blogDescription:
translations.description?.message ?? content.blogDescription,
blogSidebarTitle:
optionsTranslations['sidebar.title']?.message ?? content.blogSidebarTitle,
blogListPaginated: translateListPage(
content.blogListPaginated,
optionsTranslations,
),
translations['sidebar.title']?.message ?? content.blogSidebarTitle,
};
}