Skip to content

Commit

Permalink
Move theme-classic/src/utils code to new @docusaurus/theme-common pac…
Browse files Browse the repository at this point in the history
…kage
  • Loading branch information
slorber committed Nov 18, 2020
1 parent 16f6943 commit d5aecf5
Show file tree
Hide file tree
Showing 31 changed files with 73 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import clsx from 'clsx';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';

import styles from './styles.module.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import usePrismTheme from '@theme/hooks/usePrismTheme';
import type {Props} from '@theme/CodeBlock';

import styles from './styles.module.css';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';

const highlightLinesRangeRegex = /{([\d,-]+)}/;
const getHighlightDirectiveRegex = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {matchPath} from '@docusaurus/router';

import clsx from 'clsx';
import styles from './styles.module.css';
import {docVersionSearchTag} from '../../utils/searchUtils';
import {docVersionSearchTag} from '@docusaurus/theme-common';

type DocPageContentProps = {
readonly currentDocRoute: DocumentRoute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import React, {useState, useCallback, useEffect, useRef} from 'react';
import clsx from 'clsx';
import useThemeConfig from '../../utils/useThemeConfig';
import {isSamePath} from '../../utils';
import {useThemeConfig, isSamePath} from '@docusaurus/theme-common';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
import useWindowSize, {windowSizes} from '@theme/hooks/useWindowSize';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useActiveVersion,
useDocVersionSuggestions,
} from '@theme/hooks/useDocs';
import useDocsPreferredVersion from '../../utils/docsPreferredVersion/useDocsPreferredVersion';
import {useDocsPreferredVersion} from '@docusaurus/theme-common';

const getVersionMainDoc = (version) =>
version.docs.find((doc) => doc.id === version.mainDocId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import clsx from 'clsx';

import Link from '@docusaurus/Link';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import useBaseUrl from '@docusaurus/useBaseUrl';
import styles from './styles.module.css';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import React from 'react';
import clsx from 'clsx';
import type {HeadingType, Props} from '@theme/Heading';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';

import './styles.css';
import styles from './styles.module.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';
import type {Props} from '@theme/Layout';
import SearchMetadatas from '@theme/SearchMetadatas';
import {DEFAULT_SEARCH_TAG} from '../../utils/searchUtils';
import {DEFAULT_SEARCH_TAG} from '@docusaurus/theme-common';

export default function LayoutHead(props: Props): JSX.Element {
const {siteConfig} = useDocusaurusContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import ThemeProvider from '@theme/ThemeProvider';
import UserPreferencesProvider from '@theme/UserPreferencesProvider';
import DocsPreferredVersionContextProvider from '../../utils/docsPreferredVersion/DocsPreferredVersionProvider';
import {DocsPreferredVersionContextProvider} from '@docusaurus/theme-common';

export default function LayoutProviders({children}) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-classic/src/theme/Logo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Link from '@docusaurus/Link';
import ThemedImage from '@theme/ThemedImage';
import useBaseUrl from '@docusaurus/useBaseUrl';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import isInternalUrl from '@docusaurus/isInternalUrl';

const Logo = (props: Props): JSX.Element => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import clsx from 'clsx';
import SearchBar from '@theme/SearchBar';
import Toggle from '@theme/Toggle';
import useThemeContext from '@theme/hooks/useThemeContext';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import useHideableNavbar from '@theme/hooks/useHideableNavbar';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
import useWindowSize, {windowSizes} from '@theme/hooks/useWindowSize';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import clsx from 'clsx';
import Link from '@docusaurus/Link';
import useBaseUrl from '@docusaurus/useBaseUrl';
import {useLocation} from '@docusaurus/router';
import {isSamePath} from '../../utils';
import {isSamePath} from '@docusaurus/theme-common';
import type {
NavLinkProps,
DesktopOrMobileNavBarItemProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import DefaultNavbarItem from './DefaultNavbarItem';
import {useLatestVersion, useActiveDocContext} from '@theme/hooks/useDocs';
import clsx from 'clsx';
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
import useDocsPreferredVersion from '../../utils/docsPreferredVersion/useDocsPreferredVersion';
import {useDocsPreferredVersion} from '@docusaurus/theme-common';

export default function DocNavbarItem({
docId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useActiveDocContext,
} from '@theme/hooks/useDocs';
import type {Props} from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
import useDocsPreferredVersion from '../../utils/docsPreferredVersion/useDocsPreferredVersion';
import {useDocsPreferredVersion} from '@docusaurus/theme-common';

const getVersionMainDoc = (version) =>
version.docs.find((doc) => doc.id === version.mainDocId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import DefaultNavbarItem from './DefaultNavbarItem';
import {useActiveVersion, useLatestVersion} from '@theme/hooks/useDocs';
import type {Props} from '@theme/NavbarItem/DocsVersionNavbarItem';
import useDocsPreferredVersion from '../../utils/docsPreferredVersion/useDocsPreferredVersion';
import {useDocsPreferredVersion} from '@docusaurus/theme-common';

const getVersionMainDoc = (version) =>
version.docs.find((doc) => doc.id === version.mainDocId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React, {ComponentProps} from 'react';
import Toggle from 'react-toggle';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

import clsx from 'clsx';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {useState, useEffect, useCallback} from 'react';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';
import type {useAnnouncementBarReturns} from '@theme/hooks/useAnnouncementBar';

const STORAGE_DISMISS_KEY = 'docusaurus.announcement.dismiss';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/
import {useAllDocsData, useActivePluginAndVersion} from '@theme/hooks/useDocs';
import {useDocsPreferredVersionByPluginId} from '../../utils/docsPreferredVersion/useDocsPreferredVersion';
import {DEFAULT_SEARCH_TAG, docVersionSearchTag} from '../../utils/searchUtils';
import {
useDocsPreferredVersionByPluginId,
DEFAULT_SEARCH_TAG,
docVersionSearchTag,
} from '@docusaurus/theme-common';

type ContextualSearchFilters = {
language: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import defaultTheme from 'prism-react-renderer/themes/palenight';
import useThemeContext from '@theme/hooks/useThemeContext';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';

const usePrismTheme = (): typeof defaultTheme => {
const {prism} = useThemeConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useState, useCallback, useEffect} from 'react';

import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import type {useThemeReturns} from '@theme/hooks/useTheme';
import useThemeConfig from '../../utils/useThemeConfig';
import {useThemeConfig} from '@docusaurus/theme-common';

const themes = {
light: 'light',
Expand Down
13 changes: 12 additions & 1 deletion packages/docusaurus-theme-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@
},
"license": "MIT",
"dependencies": {
"@docusaurus/types": "^2.0.0-alpha.66"
"@docusaurus/core": "2.0.0-alpha.66",
"@docusaurus/plugin-content-blog": "2.0.0-alpha.66",
"@docusaurus/plugin-content-docs": "2.0.0-alpha.66",
"@docusaurus/plugin-content-pages": "2.0.0-alpha.66",
"@docusaurus/types": "2.0.0-alpha.66"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "2.0.0-alpha.66"
},
"peerDependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4"
},
"engines": {
"node": ">=10.15.1"
Expand Down
13 changes: 12 additions & 1 deletion packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

export const Test = 42;
export {useThemeConfig, ThemeConfig} from './utils/useThemeConfig';
export {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './utils/searchUtils';
export {isDocsPluginEnabled} from './utils/docsUtils';

export {isSamePath} from './utils/pathUtils';

export {
useDocsPreferredVersion,
useDocsPreferredVersionByPluginId,
} from './utils/docsPreferredVersion/useDocsPreferredVersion';

export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
13 changes: 13 additions & 0 deletions packages/docusaurus-theme-common/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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.
*/

/* eslint-disable import/no-duplicates */
/* eslint-disable spaced-comment */
/// <reference types="@docusaurus/module-type-aliases" />
/// <reference types="@docusaurus/plugin-content-blog" />
/// <reference types="@docusaurus/plugin-content-docs" />
/// <reference types="@docusaurus/plugin-content-pages" />
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {isSamePath} from '../utils';
import {isSamePath} from '../pathUtils';

describe('isSamePath', () => {
test('should be true for compared path without trailing slash', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import React, {
useMemo,
useState,
} from 'react';
import useThemeConfig, {DocsVersionPersistence} from '../useThemeConfig';
import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
import {isDocsPluginEnabled} from '../docsUtils';

import {useAllDocsData} from '@theme/hooks/useDocs';
Expand Down Expand Up @@ -68,7 +68,7 @@ function readStorageState({
);
const pluginData = allDocsData[pluginId];
const versionExists = pluginData.versions.some(
(version) => version.name === preferredVersionNameUnsafe,
(version: any) => version.name === preferredVersionNameUnsafe,
);
if (versionExists) {
return {preferredVersionName: preferredVersionNameUnsafe};
Expand Down Expand Up @@ -129,7 +129,7 @@ type DocsPreferredVersionContextValue = ReturnType<typeof useContextValue>;

const Context = createContext<DocsPreferredVersionContextValue | null>(null);

export default function DocsPreferredVersionContextProvider({
export function DocsPreferredVersionContextProvider({
children,
}: {
children: ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';

import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';

// TODO improve typing

// Note, the preferredVersion attribute will always be null before mount
export default function useDocsPreferredVersion(
export function useDocsPreferredVersion(
pluginId: string | undefined = DEFAULT_PLUGIN_ID,
) {
const docsData = useDocsData(pluginId);
Expand All @@ -20,7 +22,9 @@ export default function useDocsPreferredVersion(
const {preferredVersionName} = state[pluginId];

const preferredVersion = preferredVersionName
? docsData.versions.find((version) => version.name === preferredVersionName)
? docsData.versions.find(
(version: any) => version.name === preferredVersionName,
)
: null;

const savePreferredVersionName = useCallback(
Expand All @@ -43,7 +47,7 @@ export function useDocsPreferredVersionByPluginId() {

return preferredVersionName
? docsData.versions.find(
(version) => version.name === preferredVersionName,
(version: any) => version.name === preferredVersionName,
)
: null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export type ThemeConfig = {
hideableSidebar: any;
};

export default function useThemeConfig(): ThemeConfig {
export function useThemeConfig(): ThemeConfig {
return useDocusaurusContext().siteConfig.themeConfig as ThemeConfig;
}

0 comments on commit d5aecf5

Please sign in to comment.