More fixes

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Josh-Cena 2021-06-17 17:47:03 +08:00
parent 6d3d416f58
commit c7c833fe77
No known key found for this signature in database
GPG key ID: C37145B818BDB68F
16 changed files with 59 additions and 32 deletions

View file

@ -32,7 +32,7 @@ import {LoadContext} from '@docusaurus/types';
import {validateBlogPostFrontMatter} from './blogFrontMatter'; import {validateBlogPostFrontMatter} from './blogFrontMatter';
export function truncate(fileString: string, truncateMarker: RegExp): string { export function truncate(fileString: string, truncateMarker: RegExp): string {
return fileString.split(truncateMarker, 1).shift()!; return fileString.split(truncateMarker, 1).shift() as string;
} }
export function getSourceToPermalink( export function getSourceToPermalink(

View file

@ -75,7 +75,7 @@ export type ActiveDocContext = {
}; };
export const getLatestVersion = (data: GlobalPluginData): Version => { export const getLatestVersion = (data: GlobalPluginData): Version => {
return data.versions.find((version) => version.isLast)!; return data.versions.find((version) => version.isLast) as GlobalVersion;
}; };
// Note: return undefined on doc-unrelated pages, // Note: return undefined on doc-unrelated pages,

View file

@ -130,6 +130,7 @@ function doProcessDocMetadata({
// Strip number prefixes by default (01-MyFolder/01-MyDoc.md => MyFolder/MyDoc) by default, // Strip number prefixes by default (01-MyFolder/01-MyDoc.md => MyFolder/MyDoc) by default,
// but allow to disable this behavior with frontmatterr // but allow to disable this behavior with frontmatterr
// eslint-disable-next-line camelcase
parse_number_prefixes = true, parse_number_prefixes = true,
} = frontMatter; } = frontMatter;
@ -144,6 +145,7 @@ function doProcessDocMetadata({
// ex: myDoc -> . // ex: myDoc -> .
const sourceDirName = path.dirname(source); const sourceDirName = path.dirname(source);
// eslint-disable-next-line camelcase
const {filename: unprefixedFileName, numberPrefix} = parse_number_prefixes const {filename: unprefixedFileName, numberPrefix} = parse_number_prefixes
? options.numberPrefixParser(sourceFileNameWithoutExtension) ? options.numberPrefixParser(sourceFileNameWithoutExtension)
: {filename: sourceFileNameWithoutExtension, numberPrefix: undefined}; : {filename: sourceFileNameWithoutExtension, numberPrefix: undefined};
@ -172,6 +174,7 @@ function doProcessDocMetadata({
return undefined; return undefined;
} }
// Eventually remove the number prefixes from intermediate directories // Eventually remove the number prefixes from intermediate directories
// eslint-disable-next-line camelcase
return parse_number_prefixes return parse_number_prefixes
? stripPathNumberPrefixes(sourceDirName, options.numberPrefixParser) ? stripPathNumberPrefixes(sourceDirName, options.numberPrefixParser)
: sourceDirName; : sourceDirName;

View file

@ -253,7 +253,9 @@ export default function pluginContentDocs(
if (versionHomeDoc) { if (versionHomeDoc) {
return versionHomeDoc; return versionHomeDoc;
} else if (firstDocIdOfFirstSidebar) { } else if (firstDocIdOfFirstSidebar) {
return docs.find((doc) => doc.id === firstDocIdOfFirstSidebar)!; return docs.find(
(doc) => doc.id === firstDocIdOfFirstSidebar,
) as DocMetadata;
} else { } else {
return docs[0]; return docs[0];
} }

View file

@ -97,7 +97,7 @@ function parseBreadcrumb(
): {parents: string[]; tail: string} { ): {parents: string[]; tail: string} {
return { return {
parents: take(breadcrumb, breadcrumb.length - 1), parents: take(breadcrumb, breadcrumb.length - 1),
tail: last(breadcrumb)!, tail: last(breadcrumb) as string,
}; };
} }

View file

@ -127,7 +127,10 @@ function assertIsCategory(
); );
} }
// "collapsed" is an optional property // "collapsed" is an optional property
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') { if (
Object.prototype.hasOwnProperty.call(item, 'collapsed') &&
typeof item.collapsed !== 'boolean'
) {
throw new Error( throw new Error(
`Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`, `Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`,
); );
@ -438,7 +441,20 @@ export function collectSidebarsDocIds(
}); });
} }
export function createSidebarsUtils(sidebars: Sidebars) { export function createSidebarsUtils(
sidebars: Sidebars,
): {
getFirstDocIdOfFirstSidebar: () => string | undefined;
getSidebarNameByDocId: (docId: string) => string | undefined;
getDocNavigation: (
docId: string,
) => {
sidebarName: string | undefined;
previousId: string | undefined;
nextId: string | undefined;
};
checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
} {
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars); const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
function getFirstDocIdOfFirstSidebar(): string | undefined { function getFirstDocIdOfFirstSidebar(): string | undefined {

View file

@ -288,11 +288,9 @@ function createVersionMetadata({
// retro-compatible values // retro-compatible values
const defaultVersionLabel = const defaultVersionLabel =
versionName === CURRENT_VERSION_NAME ? 'Next' : versionName; versionName === CURRENT_VERSION_NAME ? 'Next' : versionName;
const defaultVersionPathPart = isLast const versionNameNotLast =
? '' versionName === CURRENT_VERSION_NAME ? 'next' : versionName;
: versionName === CURRENT_VERSION_NAME const defaultVersionPathPart = isLast ? '' : versionNameNotLast;
? 'next'
: versionName;
const versionOptions: VersionOptions = options.versions[versionName] ?? {}; const versionOptions: VersionOptions = options.versions[versionName] ?? {};

View file

@ -21,10 +21,10 @@ const ThemedImage = (props: Props): JSX.Element => {
type SourceName = keyof Props['sources']; type SourceName = keyof Props['sources'];
const clientTheme: SourceName[] = isDarkTheme ? ['dark'] : ['light'];
const renderedSourceNames: SourceName[] = isClient const renderedSourceNames: SourceName[] = isClient
? isDarkTheme ? clientTheme
? ['dark']
: ['light']
: // We need to render both images on the server to avoid flash : // We need to render both images on the server to avoid flash
// See https://github.com/facebook/docusaurus/pull/3730 // See https://github.com/facebook/docusaurus/pull/3730
['light', 'dark']; ['light', 'dark'];

View file

@ -21,10 +21,10 @@ const ThemedImage = (props: Props): JSX.Element => {
type SourceName = keyof Props['sources']; type SourceName = keyof Props['sources'];
const clientTheme: SourceName[] = isDarkTheme ? ['dark'] : ['light'];
const renderedSourceNames: SourceName[] = isClient const renderedSourceNames: SourceName[] = isClient
? isDarkTheme ? clientTheme
? ['dark']
: ['light']
: // We need to render both images on the server to avoid flash : // We need to render both images on the server to avoid flash
// See https://github.com/facebook/docusaurus/pull/3730 // See https://github.com/facebook/docusaurus/pull/3730
['light', 'dark']; ['light', 'dark'];

View file

@ -35,7 +35,7 @@ function getNavbarTranslationFile(navbar: Navbar): TranslationFileContent {
) )
.keyBy((navbarItem) => `item.label.${navbarItem.label}`) .keyBy((navbarItem) => `item.label.${navbarItem.label}`)
.mapValues((navbarItem) => ({ .mapValues((navbarItem) => ({
message: navbarItem.label!, message: navbarItem.label as string,
description: `Navbar item with label ${navbarItem.label}`, description: `Navbar item with label ${navbarItem.label}`,
})) }))
.value(); .value();
@ -78,7 +78,7 @@ function getFooterTranslationFile(footer: Footer): TranslationFileContent {
) )
.keyBy((link) => `link.title.${link.title}`) .keyBy((link) => `link.title.${link.title}`)
.mapValues((link) => ({ .mapValues((link) => ({
message: link.title!, message: link.title as string,
description: `The title of the footer links column with title=${link.title} in the footer`, description: `The title of the footer links column with title=${link.title} in the footer`,
})) }))
.value(); .value();
@ -90,7 +90,7 @@ function getFooterTranslationFile(footer: Footer): TranslationFileContent {
) )
.keyBy((linkItem) => `link.item.label.${linkItem.label}`) .keyBy((linkItem) => `link.item.label.${linkItem.label}`)
.mapValues((linkItem) => ({ .mapValues((linkItem) => ({
message: linkItem.label!, message: linkItem.label as string,
description: `The label of footer link with label=${ description: `The label of footer link with label=${
linkItem.label linkItem.label
} linking to ${linkItem.to ?? linkItem.href}`, } linking to ${linkItem.to ?? linkItem.href}`,

View file

@ -10,6 +10,8 @@ const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const globby = require('globby'); const globby = require('globby');
const {mapValues, pickBy, difference, orderBy} = require('lodash'); const {mapValues, pickBy, difference, orderBy} = require('lodash');
// Unsafe import, should we create a package for the translationsExtractor ?
const translationsExtractor = require('@docusaurus/core/lib/server/translations/translationsExtractor');
const CodeDirPaths = [ const CodeDirPaths = [
path.join(__dirname, 'lib-next'), path.join(__dirname, 'lib-next'),
@ -49,11 +51,10 @@ function logKeys(keys) {
} }
async function extractThemeCodeMessages() { async function extractThemeCodeMessages() {
// Unsafe import, should we create a package for the translationsExtractor ?
const { const {
globSourceCodeFilePaths, globSourceCodeFilePaths,
extractAllSourceCodeFileTranslations, extractAllSourceCodeFileTranslations,
} = require('@docusaurus/core/lib/server/translations/translationsExtractor'); } = translationsExtractor;
const filePaths = ( const filePaths = (
await globSourceCodeFilePaths(CodeDirPaths) await globSourceCodeFilePaths(CodeDirPaths)

View file

@ -11,7 +11,15 @@ import {useLocation} from '@docusaurus/router';
// Permits to obtain the url of the current page in another locale // Permits to obtain the url of the current page in another locale
// Useful to generate hreflang meta headers etc... // Useful to generate hreflang meta headers etc...
// See https://developers.google.com/search/docs/advanced/crawling/localized-versions // See https://developers.google.com/search/docs/advanced/crawling/localized-versions
export function useAlternatePageUtils() { export function useAlternatePageUtils(): {
createUrl: ({
locale,
fullyQualified,
}: {
locale: string;
fullyQualified: boolean;
}) => string;
} {
const { const {
siteConfig: {baseUrl, url}, siteConfig: {baseUrl, url},
i18n: {defaultLocale, currentLocale}, i18n: {defaultLocale, currentLocale},

View file

@ -21,6 +21,9 @@ export default function applyTrailingSlash(
function removeTrailingSlash(str: string): string { function removeTrailingSlash(str: string): string {
return str.endsWith('/') ? str.slice(0, -1) : str; return str.endsWith('/') ? str.slice(0, -1) : str;
} }
function handleTrailingSlash(str: string, trailing: boolean): string {
return trailing ? addTrailingSlash(str) : removeTrailingSlash(str);
}
// undefined = legacy retrocompatible behavior // undefined = legacy retrocompatible behavior
if (typeof trailingSlash === 'undefined') { if (typeof trailingSlash === 'undefined') {
@ -32,10 +35,6 @@ export default function applyTrailingSlash(
// Never transform '/' to '' // Never transform '/' to ''
const newPathname = const newPathname =
pathname === '/' pathname === '/' ? '/' : handleTrailingSlash(pathname, trailingSlash);
? '/'
: trailingSlash
? addTrailingSlash(pathname)
: removeTrailingSlash(pathname);
return path.replace(pathname, newPathname); return path.replace(pathname, newPathname);
} }

View file

@ -12,7 +12,7 @@ export function buildUrl(
organizationName: string, organizationName: string,
projectName: string, projectName: string,
useSSH: boolean | undefined, useSSH: boolean | undefined,
) { ): string {
return useSSH return useSSH
? buildSshUrl(githubHost, organizationName, projectName, githubPort) ? buildSshUrl(githubHost, organizationName, projectName, githubPort)
: buildHttpsUrl( : buildHttpsUrl(

View file

@ -52,7 +52,7 @@ export async function loadSiteConfig({
}: { }: {
siteDir: string; siteDir: string;
customConfigFilePath?: string; customConfigFilePath?: string;
}) { }): Promise<{siteConfig: DocusaurusConfig; siteConfigPath: string}> {
const siteConfigPathUnresolved = const siteConfigPathUnresolved =
customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME; customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME;
@ -309,7 +309,7 @@ ${Object.keys(registry)
const siteMetadata: DocusaurusSiteMetadata = { const siteMetadata: DocusaurusSiteMetadata = {
docusaurusVersion: getPackageJsonVersion( docusaurusVersion: getPackageJsonVersion(
join(__dirname, '../../package.json'), join(__dirname, '../../package.json'),
)!, ) as string,
siteVersion: getPackageJsonVersion(join(siteDir, 'package.json')), siteVersion: getPackageJsonVersion(join(siteDir, 'package.json')),
pluginVersions: {}, pluginVersions: {},
}; };

View file

@ -11,7 +11,7 @@ import {applyTrailingSlash} from '@docusaurus/utils-common';
export default function applyRouteTrailingSlash( export default function applyRouteTrailingSlash(
route: RouteConfig, route: RouteConfig,
trailingSlash: boolean | undefined, trailingSlash: boolean | undefined,
) { ): RouteConfig {
return { return {
...route, ...route,
path: applyTrailingSlash(route.path, trailingSlash), path: applyTrailingSlash(route.path, trailingSlash),