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

feat(i18n): support translating through locale extensions myDoc.fr.md #9700

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
69 changes: 47 additions & 22 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
getContentPathList,
isUnlisted,
isDraft,
filterFilesWithLocaleExtension,
getLocalizedSource,
} from '@docusaurus/utils';
import {validateBlogPostFrontMatter} from './frontMatter';
import {type AuthorsMap, getAuthorsMap, getBlogPostAuthors} from './authors';
Expand Down Expand Up @@ -208,13 +210,19 @@ async function parseBlogPostMarkdownFile({
const defaultReadingTime: ReadingTimeFunction = ({content, options}) =>
readingTime(content, options).minutes;

async function processBlogSourceFile(
blogSourceRelative: string,
contentPaths: BlogContentPaths,
context: LoadContext,
options: PluginOptions,
authorsMap?: AuthorsMap,
): Promise<BlogPost | undefined> {
async function processBlogSourceFile({
blogSourceRelative,
contentPaths,
context,
options,
authorsMap,
}: {
blogSourceRelative: string;
contentPaths: BlogContentPaths;
context: LoadContext;
options: PluginOptions;
authorsMap?: AuthorsMap;
}): Promise<BlogPost | undefined> {
const {
siteConfig: {
baseUrl,
Expand All @@ -231,21 +239,30 @@ async function processBlogSourceFile(
editUrl,
} = options;

// TODO remove this in favor of getLocalizedSource
// Lookup in localized folder in priority
const blogDirPath = await getFolderContainingFile(
getContentPathList(contentPaths),
blogSourceRelative,
);

const blogSourceAbsolute = path.join(blogDirPath, blogSourceRelative);
const {
source: blogSource,

// contentPath: blogDirPath
} = await getLocalizedSource({
relativeSource: blogSourceRelative,
contentPaths,
locale: context.i18n.currentLocale,
});

const {frontMatter, content, contentTitle, excerpt} =
await parseBlogPostMarkdownFile({
filePath: blogSourceAbsolute,
filePath: blogSource,
parseFrontMatter,
});

const aliasedSource = aliasedSitePath(blogSourceAbsolute, siteDir);
const aliasedSource = aliasedSitePath(blogSource, siteDir);

const draft = isDraft({frontMatter});
const unlisted = isUnlisted({frontMatter});
Expand Down Expand Up @@ -274,14 +291,14 @@ async function processBlogSourceFile(
}

try {
const result = getFileCommitDate(blogSourceAbsolute, {
const result = getFileCommitDate(blogSource, {
age: 'oldest',
includeAuthor: false,
});
return result.date;
} catch (err) {
logger.warn(err);
return (await fs.stat(blogSourceAbsolute)).birthtime;
return (await fs.stat(blogSource)).birthtime;
}
}

Expand All @@ -302,7 +319,7 @@ async function processBlogSourceFile(
function getBlogEditUrl() {
const blogPathRelative = path.relative(
blogDirPath,
path.resolve(blogSourceAbsolute),
path.resolve(blogSource),
);

if (typeof editUrl === 'function') {
Expand Down Expand Up @@ -374,28 +391,36 @@ export async function generateBlogPosts(
return [];
}

const blogSourceFiles = await Globby(include, {
cwd: contentPaths.contentPath,
ignore: exclude,
});
async function getBlogSourceFiles() {
const files = await Globby(include, {
cwd: contentPaths.contentPath,
ignore: exclude,
});
return filterFilesWithLocaleExtension({
files,
locales: context.i18n.locales,
});
}

const blogSourceFiles = await getBlogSourceFiles();

const authorsMap = await getAuthorsMap({
contentPaths,
authorsMapPath: options.authorsMapPath,
});

async function doProcessBlogSourceFile(blogSourceFile: string) {
async function doProcessBlogSourceFile(blogSourceRelative: string) {
try {
return await processBlogSourceFile(
blogSourceFile,
return await processBlogSourceFile({
blogSourceRelative,
contentPaths,
context,
options,
authorsMap,
);
});
} catch (err) {
throw new Error(
`Processing of blog source file path=${blogSourceFile} failed.`,
`Processing of blog source file path=${blogSourceRelative} failed.`,
{cause: err as Error},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the
* preserved as-is. Default values will be applied when generating metadata
*/
export type BlogPostFrontMatter = {
// TODO Docusaurus v4: remove
/**
* @deprecated Use `slug` instead.
*/
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports[`docusaurus-plugin-content-pages loads simple pages with french translat
},
{
"permalink": "/fr/typescript",
"source": "@site/src/pages/typescript.tsx",
"source": "@site/src/pages/typescript.fr.tsx",
"type": "jsx",
},
{
Expand Down
31 changes: 18 additions & 13 deletions packages/docusaurus-plugin-content-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
aliasedSitePath,
docuHash,
getPluginI18nPath,
getFolderContainingFile,
addTrailingPathSeparator,
Globby,
createAbsoluteFilePathMatcher,
Expand All @@ -22,6 +21,8 @@ import {
parseMarkdownFile,
isUnlisted,
isDraft,
getLocalizedSource,
filterFilesWithLocaleExtension,
} from '@docusaurus/utils';
import {validatePageFrontMatter} from './frontMatter';

Expand Down Expand Up @@ -62,6 +63,17 @@ export default function pluginContentPages(
);
const dataDir = path.join(pluginDataDirRoot, options.id ?? DEFAULT_PLUGIN_ID);

async function getPageFiles() {
const files = await Globby(options.include, {
cwd: contentPaths.contentPath,
ignore: options.exclude,
});
return filterFilesWithLocaleExtension({
files,
locales: context.i18n.locales,
});
}

return {
name: 'docusaurus-plugin-content-pages',

Expand All @@ -73,28 +85,21 @@ export default function pluginContentPages(
},

async loadContent() {
const {include} = options;

if (!(await fs.pathExists(contentPaths.contentPath))) {
return null;
}

const {baseUrl} = siteConfig;
const pagesFiles = await Globby(include, {
cwd: contentPaths.contentPath,
ignore: options.exclude,
});
const pagesFiles = await getPageFiles();

async function processPageSourceFile(
relativeSource: string,
): Promise<Metadata | undefined> {
// Lookup in localized folder in priority
const contentPath = await getFolderContainingFile(
getContentPathList(contentPaths),
const {source} = await getLocalizedSource({
relativeSource,
);

const source = path.join(contentPath, relativeSource);
contentPaths,
locale: context.i18n.currentLocale,
});
const aliasedSourcePath = aliasedSitePath(source, siteDir);
const permalink = normalizeUrl([
baseUrl,
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus-plugin-content-pages/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

export type PagesContentPaths = {
contentPath: string;
contentPathLocalized: string;
};
import type {ContentPaths} from '@docusaurus/utils';

export type PagesContentPaths = ContentPaths;
Loading
Loading