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

chore: backport retro compatible commits for the Docusaurus v2.4.2 release #9324

Merged
merged 6 commits into from
Sep 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ exports[`DefaultSidebarItemsGenerator generates simple flat sidebar 1`] = `
"type": "doc",
},
{
"customProps": {
"custom": "prop",
},
"id": "doc1",
"label": "doc1 sidebar label",
"type": "doc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('DefaultSidebarItemsGenerator', () => {
sidebarPosition: 2,
frontMatter: {
sidebar_label: 'doc1 sidebar label',
sidebar_custom_props: {custom: 'prop'},
},
title: '',
unversionedId: 'doc1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ Available doc IDs:
): WithPosition<SidebarItemDoc> {
const {
sidebarPosition: position,
frontMatter: {sidebar_label: label, sidebar_class_name: className},
frontMatter: {
sidebar_label: label,
sidebar_class_name: className,
sidebar_custom_props: customProps,
},
} = getDoc(id);
return {
type: 'doc',
Expand All @@ -149,6 +153,7 @@ Available doc IDs:
// sidebar
...(label !== undefined && {label}),
...(className !== undefined && {className}),
...(customProps !== undefined && {customProps}),
};
}
function createCategoryItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ export default function BlogPostItemContainer({
children,
className,
}: Props): JSX.Element {
const {frontMatter, assets} = useBlogPost();
const {
frontMatter,
assets,
metadata: {description},
} = useBlogPost();
const {withBaseUrl} = useBaseUrlUtils();
const image = assets.image ?? frontMatter.image;
const keywords = frontMatter.keywords ?? [];
return (
<article
className={className}
itemProp="blogPost"
itemScope
itemType="http://schema.org/BlogPosting">
{description && <meta itemProp="description" content={description} />}
{image && (
<meta itemProp="image" content={withBaseUrl(image, {absolute: true})} />
<link itemProp="image" href={withBaseUrl(image, {absolute: true})} />
)}
{keywords.length > 0 && (
<meta itemProp="keywords" content={keywords.join(',')} />
)}
{children}
</article>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export default function BlogPostItemHeaderAuthor({
<div className={clsx('avatar margin-bottom--sm', className)}>
{imageURL && (
<MaybeLink href={link} className="avatar__photo-link">
<img className="avatar__photo" src={imageURL} alt={name} />
<img
className="avatar__photo"
src={imageURL}
alt={name}
itemProp="image"
/>
</MaybeLink>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
display: flex;
flex: 1 0 auto;
}

/*
JS disabled??? Show light version by default => better than showing nothing
TODO bad, but we currently always show light mode when there's no data-theme
*/
html:not([data-theme]) .themedComponent--light {
display: initial;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {Props} from '@theme/MDXComponents/Ul';

import styles from './styles.module.css';

function transformUlClassName(className?: string): string {
function transformUlClassName(className?: string): string | undefined {
// Fix https://github.com/facebook/docusaurus/issues/9098
if (typeof className === 'undefined') {
return undefined;
}
return clsx(
className,
// This class is set globally by GitHub/MDX. We keep the global class, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
keyboardFocusedClassName,
} from '@docusaurus/theme-common/internal';
import {useLocation} from '@docusaurus/router';
import {applyTrailingSlash} from '@docusaurus/utils-common';
import SearchMetadata from '@theme/SearchMetadata';

// TODO move to SiteMetadataDefaults or theme-common ?
Expand Down Expand Up @@ -58,10 +59,19 @@ function AlternateLangHeaders(): JSX.Element {
// Default canonical url inferred from current page location pathname
function useDefaultCanonicalUrl() {
const {
siteConfig: {url: siteUrl},
siteConfig: {url: siteUrl, baseUrl, trailingSlash},
} = useDocusaurusContext();

// TODO using useLocation().pathname is not a super idea
// See https://github.com/facebook/docusaurus/issues/9170
const {pathname} = useLocation();
return siteUrl + useBaseUrl(pathname);

const canonicalPathname = applyTrailingSlash(useBaseUrl(pathname), {
trailingSlash,
baseUrl,
});

return siteUrl + canonicalPathname;
}

// TODO move to SiteMetadataDefaults or theme-common ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useLocation} from '@docusaurus/router';
import {applyTrailingSlash} from '@docusaurus/utils-common';

/**
* Permits to obtain the url of the current page in another locale, useful to
Expand Down Expand Up @@ -35,17 +36,25 @@ export function useAlternatePageUtils(): {
}) => string;
} {
const {
siteConfig: {baseUrl, url},
siteConfig: {baseUrl, url, trailingSlash},
i18n: {defaultLocale, currentLocale},
} = useDocusaurusContext();

// TODO using useLocation().pathname is not a super idea
// See https://github.com/facebook/docusaurus/issues/9170
const {pathname} = useLocation();

const canonicalPathname = applyTrailingSlash(pathname, {
trailingSlash,
baseUrl,
});

const baseUrlUnlocalized =
currentLocale === defaultLocale
? baseUrl
: baseUrl.replace(`/${currentLocale}/`, '/');

const pathnameSuffix = pathname.replace(baseUrl, '');
const pathnameSuffix = canonicalPathname.replace(baseUrl, '');

function getLocalizedBaseUrl(locale: string) {
return locale === defaultLocale
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import SiteMetadataDefaults from './SiteMetadataDefaults';
// TODO, quick fix for CSS insertion order
// eslint-disable-next-line import/order
import ErrorBoundary from '@docusaurus/ErrorBoundary';
import HasHydratedDataAttribute from './hasHydratedDataAttribute';

export default function App(): JSX.Element {
const routeElement = renderRoutes(routes);
Expand All @@ -39,6 +40,7 @@ export default function App(): JSX.Element {
{routeElement}
</PendingNavigation>
</Root>
<HasHydratedDataAttribute />
</BrowserContextProvider>
</DocusaurusContextProvider>
</ErrorBoundary>
Expand Down
21 changes: 21 additions & 0 deletions packages/docusaurus/src/client/hasHydratedDataAttribute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import Head from '@docusaurus/Head';
import useIsBrowser from '@docusaurus/useIsBrowser';

// See https://github.com/facebook/docusaurus/pull/9256
// Docusaurus adds a <html data-has-hydrated="true"> after hydration
export default function HasHydratedDataAttribute(): JSX.Element {
const isBrowser = useIsBrowser();
return (
<Head>
<html data-has-hydrated={isBrowser} />
</Head>
);
}
Loading