From 8ef7c24583a25a7124b8d62891324a4a9c28fe1e Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Thu, 12 Nov 2020 19:01:46 +0300 Subject: [PATCH] fix(v2): use swizzled SearchPage component if any (#3721) * fix(v2): use swizzled SearchPage component if any * Fix dependency cycles warning --- .../docusaurus-theme-search-algolia/src/index.js | 7 +++++-- packages/docusaurus-utils/src/index.ts | 14 ++++++++++++++ website/docs/using-themes.md | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-theme-search-algolia/src/index.js b/packages/docusaurus-theme-search-algolia/src/index.js index 04a019b753..186857bd60 100644 --- a/packages/docusaurus-theme-search-algolia/src/index.js +++ b/packages/docusaurus-theme-search-algolia/src/index.js @@ -8,7 +8,7 @@ const path = require('path'); const fs = require('fs'); const eta = require('eta'); -const {normalizeUrl} = require('@docusaurus/utils'); +const {normalizeUrl, getSwizzledComponent} = require('@docusaurus/utils'); const openSearchTemplate = require('./templates/opensearch'); const {validateThemeConfig} = require('./validateThemeConfig'); const {memoize} = require('lodash'); @@ -29,7 +29,10 @@ function theme(context) { baseUrl, siteConfig: {title, url, favicon}, } = context; - const pagePath = path.resolve(__dirname, './theme/SearchPage/index.js'); + const pageComponent = './theme/SearchPage/index.js'; + const pagePath = + getSwizzledComponent(pageComponent) || + path.resolve(__dirname, pageComponent); return { name: 'docusaurus-theme-search-algolia', diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 6de875d4c3..5eea3925b8 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -463,3 +463,17 @@ export function reportMessage( ); } } + +export function getSwizzledComponent( + componentPath: string, +): string | undefined { + const swizzledComponentPath = path.resolve( + process.cwd(), + 'src', + componentPath, + ); + + return fs.existsSync(swizzledComponentPath) + ? swizzledComponentPath + : undefined; +} diff --git a/website/docs/using-themes.md b/website/docs/using-themes.md index af2aa521a3..8f388dd8ad 100644 --- a/website/docs/using-themes.md +++ b/website/docs/using-themes.md @@ -197,7 +197,7 @@ This theme provides a `@theme/SearchBar` component that integrates with Algolia npm install --save @docusaurus/theme-search-algolia ``` -This theme also adds search page available at `/search` path with OpenSearch support. +This theme also adds search page available at `/search` (as swizzleable `SearchPage` component) path with OpenSearch support. :::tip