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

fix: search page for non latest doc versions #205

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions docusaurus-search-local/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ All notable changes to this project will be documented in this file. See [standa

## [0.27.1](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.27.0...v0.27.1) (2022-06-13)


### Bug Fixes

* properly support no trailing slashes after fixing indexing top level pages ([999bd8b](https://github.com/easyops-cn/docusaurus-search-local/commit/999bd8bec8abe73f12abe20f58f8030be46c9ecd)), closes [#212](https://github.com/easyops-cn/docusaurus-search-local/issues/212)
- properly support no trailing slashes after fixing indexing top level pages ([999bd8b](https://github.com/easyops-cn/docusaurus-search-local/commit/999bd8bec8abe73f12abe20f58f8030be46c9ecd)), closes [#212](https://github.com/easyops-cn/docusaurus-search-local/issues/212)

## [0.27.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.26.1...v0.27.0) (2022-05-29)


### Features

* versioned search ([6865e7c](https://github.com/easyops-cn/docusaurus-search-local/commit/6865e7c3eee416e53648877359cdf206119be043)), closes [#66](https://github.com/easyops-cn/docusaurus-search-local/issues/66)
- versioned search ([6865e7c](https://github.com/easyops-cn/docusaurus-search-local/commit/6865e7c3eee416e53648877359cdf206119be043)), closes [#66](https://github.com/easyops-cn/docusaurus-search-local/issues/66)

### [0.26.1](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.26.0...v0.26.1) (2022-05-25)


### Bug Fixes

* fix i18n id of `theme.SearchPage.noResultsText` ([fe530f3](https://github.com/easyops-cn/docusaurus-search-local/commit/fe530f35da0b11c78c9d68cb6a7fea5871919e00))
- fix i18n id of `theme.SearchPage.noResultsText` ([fe530f3](https://github.com/easyops-cn/docusaurus-search-local/commit/fe530f35da0b11c78c9d68cb6a7fea5871919e00))

## [0.26.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.26.0...v0.26.0) (2022-05-24)

### Features

* add a clear button to the search bar [#170](https://github.com/easyops-cn/docusaurus-search-local/issues/170) ([f516123](https://github.com/easyops-cn/docusaurus-search-local/commit/f5161238cb7466fe7309e9166037d681e647f910))
- add a clear button to the search bar [#170](https://github.com/easyops-cn/docusaurus-search-local/issues/170) ([f516123](https://github.com/easyops-cn/docusaurus-search-local/commit/f5161238cb7466fe7309e9166037d681e647f910))

## [0.25.0](https://github.com/easyops-cn/docusaurus-search-local/compare/v0.24.1...v0.25.0) (2022-05-19)

Expand Down
6 changes: 4 additions & 2 deletions docusaurus-search-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"license": "MIT",
"dependencies": {
"@docusaurus/plugin-content-docs": "^2.0.0-beta.20",
"@docusaurus/theme-common": "^2.0.0-beta.20",
"@docusaurus/theme-translations": "^2.0.0-beta.20",
"@docusaurus/utils": "^2.0.0-beta.20",
"@docusaurus/utils-common": "^2.0.0-beta.20",
Expand All @@ -43,6 +42,7 @@
},
"devDependencies": {
"@docusaurus/module-type-aliases": "^2.0.0-beta.20",
"@docusaurus/theme-common": "^2.0.0-beta.20",
"@docusaurus/types": "^2.0.0-beta.20",
"@tsconfig/docusaurus": "^1.0.2",
"@types/cheerio": "^0.22.31",
Expand All @@ -61,6 +61,8 @@
"typescript": "^4.6.4"
},
"peerDependencies": {
"react": "^16.14.0 || 17 || 18"
"@docusaurus/theme-common": "^2.0.0-beta.20",
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
}
}
33 changes: 23 additions & 10 deletions docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
import { useHistory, useLocation } from "@docusaurus/router";
import { translate } from "@docusaurus/Translate";
import { useActiveVersion } from '@docusaurus/plugin-content-docs/client';
import { useDocsPreferredVersion } from "@docusaurus/theme-common";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have moved this export to an internal entrypoint

import {
  useDocsPreferredVersion,
} from '@docusaurus/theme-common/internal';

This also means that technically we are not 100% ready to consider this API to be part of our public API surface and make no breaking changes on it 😅

But this can be discussed if you don't have a better choice than using it, and in practice we may not break it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any other way to do it so it is either that or if you know of any other, supported, way of getting the selected version?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will move useDocsPreferredVersion to our public api

Even though we may do a breaking change later, it will be clearly highlighted in a major release blog post

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";

import { fetchIndexes } from "./fetchIndexes";
import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
Expand Down Expand Up @@ -45,12 +46,21 @@ interface SearchBarProps {
export default function SearchBar({
handleSearchBarToggle,
}: SearchBarProps): ReactElement {
let {
const {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const activeVersion = useActiveVersion();
if (activeVersion && !activeVersion.isLast) {
baseUrl = activeVersion.path + "/";

// It returns undefined for non-docs pages
const activePlugin = useActivePlugin();
let versionUrl = baseUrl;

// For non-docs pages while using plugin-content-docs with custom ids,
// this will throw an error of:
// > Docusaurus plugin global data not found for "docusaurus-plugin-content-docs" plugin with id "default".
// It seems that we can not get the correct id for non-docs pages.
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
if (preferredVersion && !preferredVersion.isLast) {
versionUrl = preferredVersion.path + "/";
}
const history = useHistory();
const location = useLocation();
Expand All @@ -72,7 +82,7 @@ export default function SearchBar({
setLoading(true);

const [{ wrappedIndexes, zhDictionary }, autoComplete] = await Promise.all([
fetchIndexes(baseUrl),
fetchIndexes(versionUrl),
fetchAutoCompleteJS(),
]);

Expand Down Expand Up @@ -165,7 +175,7 @@ export default function SearchBar({
}
input.focus();
}
}, [baseUrl, history]);
}, [baseUrl, versionUrl, history]);

useEffect(() => {
if (!Mark) {
Expand Down Expand Up @@ -245,16 +255,19 @@ export default function SearchBar({
const onClearSearch = useCallback(() => {
const params = new URLSearchParams(location.search);
params.delete(SEARCH_PARAM_HIGHLIGHT);
let paramsStr = params.toString();
let searchUrl = location.pathname + (paramsStr != "" ? `?${paramsStr}` : "") + location.hash;
const paramsStr = params.toString();
const searchUrl =
location.pathname +
(paramsStr != "" ? `?${paramsStr}` : "") +
location.hash;
if (searchUrl != location.pathname + location.search + location.hash) {
history.push(searchUrl);
}

// We always clear these here because in case no match was selected the above history push wont happen
setInputValue("");
search.current?.autocomplete.setVal("");
}, [location.pathname, location.search, location.hash]);
}, [location.pathname, location.search, location.hash, history]);

return (
<div
Expand Down
32 changes: 27 additions & 5 deletions docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import Layout from "@theme/Layout";
import Head from "@docusaurus/Head";
import Link from "@docusaurus/Link";
import { translate } from "@docusaurus/Translate";
import { usePluralForm } from "@docusaurus/theme-common";
import {
usePluralForm,
useDocsPreferredVersion,
} from "@docusaurus/theme-common";
import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";

import useSearchQuery from "../hooks/useSearchQuery";
import { fetchIndexes } from "../SearchBar/fetchIndexes";
Expand All @@ -19,9 +23,27 @@ import styles from "./SearchPage.module.css";
import { concatDocumentPath } from "../../utils/concatDocumentPath";

export default function SearchPage(): React.ReactElement {
return (
<Layout>
<SearchPageContent />
</Layout>
);
}

function SearchPageContent(): React.ReactElement {
const {
siteConfig: { baseUrl },
} = useDocusaurusContext();

// It returns undefined for non-docs pages.
const activePlugin = useActivePlugin();
let versionUrl = baseUrl;

// There is an issue, see `SearchBar.tsx`.
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
if (preferredVersion && !preferredVersion.isLast) {
versionUrl = preferredVersion.path + "/";
}
const { selectMessage } = usePluralForm();
const { searchValue, updateSearchPath } = useSearchQuery();
const [searchQuery, setSearchQuery] = useState(searchValue);
Expand Down Expand Up @@ -84,16 +106,16 @@ export default function SearchPage(): React.ReactElement {

useEffect(() => {
async function doFetchIndexes() {
const { wrappedIndexes, zhDictionary } = await fetchIndexes(baseUrl);
const { wrappedIndexes, zhDictionary } = await fetchIndexes(versionUrl);
setSearchSource(() =>
SearchSourceFactory(wrappedIndexes, zhDictionary, 100)
);
}
doFetchIndexes();
}, [baseUrl]);
}, [versionUrl]);

return (
<Layout>
<React.Fragment>
<Head>
{/*
We should not index search pages
Expand Down Expand Up @@ -161,7 +183,7 @@ export default function SearchPage(): React.ReactElement {
))}
</section>
</div>
</Layout>
</React.Fragment>
);
}

Expand Down
30 changes: 21 additions & 9 deletions docusaurus-search-local/src/server/utils/processDocInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ProcessedPluginOptions,
PostBuildData,
} from "../../shared/interfaces";
import { LoadedContent, LoadedVersion } from "@docusaurus/plugin-content-docs"
import { LoadedContent, LoadedVersion } from "@docusaurus/plugin-content-docs";

export function processDocInfos(
{ routesPaths, outDir, baseUrl, siteConfig, plugins }: PostBuildData,
Expand All @@ -22,28 +22,35 @@ export function processDocInfos(
const emptySet = new Set();
let versionData: any = [{ versionOutDir: outDir, docs: emptySet }];
if (plugins) {
const docsPluginData = plugins.find(element => element.name === "docusaurus-plugin-content-docs");
const docsPluginData = plugins.find(
(element) => element.name === "docusaurus-plugin-content-docs"
);
if (docsPluginData) {
versionData = [];
const loadedVersions:LoadedVersion[] = (docsPluginData.content as LoadedContent).loadedVersions;
const loadedVersions: LoadedVersion[] = (
docsPluginData.content as LoadedContent
).loadedVersions;
for (const loadedVersion of loadedVersions) {
let docs = new Set();
const docs = new Set();
for (const doc of loadedVersion.docs) {
docs.add(doc.permalink);
}
const route = loadedVersion.path.substr(baseUrl.length);
let versionOutDir = outDir;
// The last versions search-index should always be placed in the root since it is the one used from non-docs pages
if (!loadedVersion.isLast) {
versionOutDir = path.join(outDir, ...route.split("/").filter((i: string) => i));
versionOutDir = path.join(
outDir,
...route.split("/").filter((i: string) => i)
);
}
versionData.push({ versionOutDir, docs });
}
}
}

// Create a list of files to index per document version. This will always include all pages and blogs.
let result = [];
const result = [];
for (const { versionOutDir, docs } of versionData) {
const versionPaths = routesPaths
.map<DocInfoWithRoute | undefined>((url: string) => {
Expand All @@ -57,7 +64,8 @@ export function processDocInfos(

// Do not index homepage, error page and search page.
if (
((!docsRouteBasePath || docsRouteBasePath[0] !== "") && route === "") ||
((!docsRouteBasePath || docsRouteBasePath[0] !== "") &&
route === "") ||
route === "404.html" ||
route === "search"
) {
Expand All @@ -78,7 +86,9 @@ export function processDocInfos(

if (
indexBlog &&
blogRouteBasePath.some((basePath) => isSameOrSubRoute(route, basePath))
blogRouteBasePath.some((basePath) =>
isSameOrSubRoute(route, basePath)
)
) {
if (
blogRouteBasePath.some(
Expand All @@ -94,7 +104,9 @@ export function processDocInfos(
}
if (
indexDocs &&
docsRouteBasePath.some((basePath) => isSameOrSubRoute(route, basePath))
docsRouteBasePath.some((basePath) =>
isSameOrSubRoute(route, basePath)
)
) {
if (docs.size === 0 || docs.has(url)) {
return { route, url, type: "docs" };
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@
"docusaurus-search-local",
"website"
],
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"resolutions": {
"@docusaurus/core": "2.0.0-beta.20",
"@docusaurus/preset-classic": "2.0.0-beta.20",
"@docusaurus/theme-classic": "2.0.0-beta.20",
"@docusaurus/theme-common": "2.0.0-beta.20"
}
}
6 changes: 4 additions & 2 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"dependencies": {
"@docusaurus/core": "^2.0.0-beta.20",
"@docusaurus/preset-classic": "^2.0.0-beta.20",
"@docusaurus/theme-classic": "^2.0.0-beta.20",
"@docusaurus/theme-common": "^2.0.0-beta.20",
"@easyops-cn/docusaurus-search-local": "workspace:*",
"@mdx-js/react": "^1.6.22",
"clsx": "^1.1.1",
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"browserslist": {
"production": [
Expand Down
Loading