mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
extract code to docusaurus utils
This commit is contained in:
parent
90b1895156
commit
b49b462389
3 changed files with 91 additions and 45 deletions
|
@ -21,7 +21,8 @@ import {
|
||||||
parseMarkdownFile,
|
parseMarkdownFile,
|
||||||
isUnlisted,
|
isUnlisted,
|
||||||
isDraft,
|
isDraft,
|
||||||
findAsyncSequential,
|
getLocalizedSourcePath,
|
||||||
|
filterFilesWithLocaleExtension,
|
||||||
} from '@docusaurus/utils';
|
} from '@docusaurus/utils';
|
||||||
import {validatePageFrontMatter} from './frontMatter';
|
import {validatePageFrontMatter} from './frontMatter';
|
||||||
|
|
||||||
|
@ -38,48 +39,6 @@ export function getContentPathList(contentPaths: PagesContentPaths): string[] {
|
||||||
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLocalizedSource({
|
|
||||||
relativeSource,
|
|
||||||
contentPaths,
|
|
||||||
locale,
|
|
||||||
}: {
|
|
||||||
relativeSource: string;
|
|
||||||
contentPaths: PagesContentPaths;
|
|
||||||
locale: string;
|
|
||||||
}) {
|
|
||||||
const {name, dir, ext} = path.parse(relativeSource);
|
|
||||||
|
|
||||||
// Lookup in localized folder in priority
|
|
||||||
const possibleSources = getContentPathList(contentPaths).flatMap(
|
|
||||||
(contentPath) => [
|
|
||||||
path.join(contentPath, dir, `${name}.${locale}${ext}`),
|
|
||||||
path.join(contentPath, relativeSource),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const localizedSource = await findAsyncSequential(
|
|
||||||
possibleSources,
|
|
||||||
fs.pathExists,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!localizedSource) {
|
|
||||||
throw new Error('unexpected');
|
|
||||||
}
|
|
||||||
|
|
||||||
return localizedSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterLocaleExtensionFiles(
|
|
||||||
files: string[],
|
|
||||||
locales: string[],
|
|
||||||
): string[] {
|
|
||||||
const localeExtensions = locales.map((locale) => `.${locale}`);
|
|
||||||
return files.filter((file) => {
|
|
||||||
const {name} = path.parse(file);
|
|
||||||
return !localeExtensions.includes(path.extname(name));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const isMarkdownSource = (source: string) =>
|
const isMarkdownSource = (source: string) =>
|
||||||
source.endsWith('.md') || source.endsWith('.mdx');
|
source.endsWith('.md') || source.endsWith('.mdx');
|
||||||
|
|
||||||
|
@ -109,7 +68,10 @@ export default function pluginContentPages(
|
||||||
cwd: contentPaths.contentPath,
|
cwd: contentPaths.contentPath,
|
||||||
ignore: options.exclude,
|
ignore: options.exclude,
|
||||||
});
|
});
|
||||||
return filterLocaleExtensionFiles(files, context.i18n.locales);
|
return filterFilesWithLocaleExtension({
|
||||||
|
files,
|
||||||
|
locales: context.i18n.locales,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -133,7 +95,7 @@ export default function pluginContentPages(
|
||||||
async function processPageSourceFile(
|
async function processPageSourceFile(
|
||||||
relativeSource: string,
|
relativeSource: string,
|
||||||
): Promise<Metadata | undefined> {
|
): Promise<Metadata | undefined> {
|
||||||
const source = await getLocalizedSource({
|
const source = await getLocalizedSourcePath({
|
||||||
relativeSource,
|
relativeSource,
|
||||||
contentPaths,
|
contentPaths,
|
||||||
locale: context.i18n.currentLocale,
|
locale: context.i18n.currentLocale,
|
||||||
|
|
|
@ -6,14 +6,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs-extra';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import {DEFAULT_PLUGIN_ID} from './constants';
|
import {DEFAULT_PLUGIN_ID} from './constants';
|
||||||
import {normalizeUrl} from './urlUtils';
|
import {normalizeUrl} from './urlUtils';
|
||||||
|
import {findAsyncSequential} from './jsUtils';
|
||||||
import type {
|
import type {
|
||||||
TranslationFileContent,
|
TranslationFileContent,
|
||||||
TranslationFile,
|
TranslationFile,
|
||||||
I18n,
|
I18n,
|
||||||
} from '@docusaurus/types';
|
} from '@docusaurus/types';
|
||||||
|
import type {ContentPaths} from './markdownLinks';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a list of translation file contents, and shallow-merges them into one.
|
* Takes a list of translation file contents, and shallow-merges them into one.
|
||||||
|
@ -112,3 +115,82 @@ export function localizePath({
|
||||||
// Url paths; add a trailing slash so it's a valid base URL
|
// Url paths; add a trailing slash so it's a valid base URL
|
||||||
return normalizeUrl([originalPath, i18n.currentLocale, '/']);
|
return normalizeUrl([originalPath, i18n.currentLocale, '/']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Localize a content file path
|
||||||
|
* ./dir/myDoc.md => ./dir/myDoc.fr.md
|
||||||
|
* @param filePath
|
||||||
|
* @param locale
|
||||||
|
*/
|
||||||
|
function addLocaleExtension(filePath: string, locale: string) {
|
||||||
|
const {name, dir, ext} = path.parse(filePath);
|
||||||
|
return path.join(dir, `${name}.${locale}${ext}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first existing localized path of a content file
|
||||||
|
* @param relativeSource
|
||||||
|
* @param contentPaths
|
||||||
|
* @param locale
|
||||||
|
*/
|
||||||
|
export async function getLocalizedSourcePath({
|
||||||
|
relativeSource,
|
||||||
|
contentPaths,
|
||||||
|
locale,
|
||||||
|
}: {
|
||||||
|
relativeSource: string;
|
||||||
|
contentPaths: ContentPaths;
|
||||||
|
locale: string;
|
||||||
|
}): Promise<string> {
|
||||||
|
// docs/myDoc.fr.md
|
||||||
|
const localeExtensionSource = path.join(
|
||||||
|
contentPaths.contentPath,
|
||||||
|
addLocaleExtension(relativeSource, locale),
|
||||||
|
);
|
||||||
|
|
||||||
|
// i18n/fr/docs/current/myDoc.md
|
||||||
|
const i18nFolderSource = path.join(
|
||||||
|
contentPaths.contentPathLocalized,
|
||||||
|
relativeSource,
|
||||||
|
);
|
||||||
|
|
||||||
|
// docs/myDoc.md
|
||||||
|
const originalSource = path.join(contentPaths.contentPath, relativeSource);
|
||||||
|
|
||||||
|
// Order matters
|
||||||
|
const possibleSources = [
|
||||||
|
localeExtensionSource,
|
||||||
|
i18nFolderSource,
|
||||||
|
originalSource,
|
||||||
|
];
|
||||||
|
|
||||||
|
// TODO can we avoid/optimize this by passing all the files we know as param?
|
||||||
|
const localizedSource = await findAsyncSequential(
|
||||||
|
possibleSources,
|
||||||
|
fs.pathExists,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!localizedSource) {
|
||||||
|
throw new Error(
|
||||||
|
`Unexpected error, couldn't find any existing file at ${originalSource}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return localizedSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterFilesWithLocaleExtension({
|
||||||
|
files,
|
||||||
|
locales,
|
||||||
|
}: {
|
||||||
|
files: string[];
|
||||||
|
locales: string[];
|
||||||
|
}): string[] {
|
||||||
|
const possibleLocaleExtensions = new Set(
|
||||||
|
locales.map((locale) => `.${locale}`),
|
||||||
|
);
|
||||||
|
return files.filter((file) => {
|
||||||
|
const {name} = path.parse(file);
|
||||||
|
return !possibleLocaleExtensions.has(path.extname(name));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -34,6 +34,8 @@ export {
|
||||||
updateTranslationFileMessages,
|
updateTranslationFileMessages,
|
||||||
getPluginI18nPath,
|
getPluginI18nPath,
|
||||||
localizePath,
|
localizePath,
|
||||||
|
getLocalizedSourcePath,
|
||||||
|
filterFilesWithLocaleExtension,
|
||||||
} from './i18nUtils';
|
} from './i18nUtils';
|
||||||
export {
|
export {
|
||||||
removeSuffix,
|
removeSuffix,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue