Skip to content

Commit

Permalink
feat: versioned search
Browse files Browse the repository at this point in the history
Adds support for versioned documentations by building separate search index files per version.
Fixes #66
  • Loading branch information
Cyriuz committed May 26, 2022
1 parent 8438463 commit 6865e7c
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 147 deletions.
1 change: 1 addition & 0 deletions docusaurus-search-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 { fetchIndexes } from "./fetchIndexes";
import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
Expand Down Expand Up @@ -44,9 +45,13 @@ interface SearchBarProps {
export default function SearchBar({
handleSearchBarToggle,
}: SearchBarProps): ReactElement {
const {
let {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const activeVersion = useActiveVersion();
if (activeVersion && !activeVersion.isLast) {
baseUrl = activeVersion.path + "/";
}
const history = useHistory();
const location = useLocation();
const searchBarRef = useRef<HTMLInputElement>(null);
Expand Down
24 changes: 13 additions & 11 deletions docusaurus-search-local/src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ export function postBuildFactory(config: ProcessedPluginOptions) {

debugInfo("parsing documents");

// Give every index entry a unique id so that the index does not need to store long URLs.
const allDocuments = await scanDocuments(data);
for (let versionData of data) {
// Give every index entry a unique id so that the index does not need to store long URLs.
const allDocuments = await scanDocuments(versionData.paths);

debugInfo("building index");
debugInfo("building index");

const searchIndex = buildIndex(allDocuments, config);
const searchIndex = buildIndex(allDocuments, config);

debugInfo("writing index to disk");
debugInfo("writing index to disk");

await writeFileAsync(
path.join(buildData.outDir, "search-index.json"),
JSON.stringify(searchIndex),
{ encoding: "utf8" }
);
await writeFileAsync(
path.join(versionData.outDir, "search-index.json"),
JSON.stringify(searchIndex),
{ encoding: "utf8" }
);

debugInfo("index written to disk successfully!");
debugInfo("index written to disk successfully!");
}
};
}
165 changes: 95 additions & 70 deletions docusaurus-search-local/src/server/utils/processDocInfos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,24 @@ describe("processDocInfos", () => {
},
[
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page",
outDir: "/build",
paths: [
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page",
},
],
},
],
],
Expand Down Expand Up @@ -112,19 +117,24 @@ describe("processDocInfos", () => {
},
[
{
filePath: "/build/docs/a.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page.html",
type: "page",
url: "/base/page",
outDir: "/build",
paths: [
{
filePath: "/build/docs/a.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page.html",
type: "page",
url: "/base/page",
},
],
},
],
],
Expand Down Expand Up @@ -177,19 +187,24 @@ describe("processDocInfos", () => {
},
[
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a/",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b/",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page/",
outDir: "/build",
paths: [
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a/",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b/",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page/",
},
],
},
],
],
Expand Down Expand Up @@ -226,24 +241,29 @@ describe("processDocInfos", () => {
},
[
{
filePath: "/build/index.html",
type: "docs",
url: "/base/",
},
{
filePath: "/build/docs-a/index.html",
type: "docs",
url: "/base/docs-a",
},
{
filePath: "/build/docs-b/c/index.html",
type: "docs",
url: "/base/docs-b/c",
},
{
filePath: "/build/page/index.html",
type: "docs",
url: "/base/page",
outDir: "/build",
paths: [
{
filePath: "/build/index.html",
type: "docs",
url: "/base/",
},
{
filePath: "/build/docs-a/index.html",
type: "docs",
url: "/base/docs-a",
},
{
filePath: "/build/docs-b/c/index.html",
type: "docs",
url: "/base/docs-b/c",
},
{
filePath: "/build/page/index.html",
type: "docs",
url: "/base/page",
},
],
},
],
],
Expand Down Expand Up @@ -280,19 +300,24 @@ describe("processDocInfos", () => {
},
[
{
filePath: "/build/blog-a/index.html",
type: "blog",
url: "/base/blog-a",
},
{
filePath: "/build/blog-b/c/index.html",
type: "blog",
url: "/base/blog-b/c",
},
{
filePath: "/build/page/index.html",
type: "blog",
url: "/base/page",
outDir: "/build",
paths: [
{
filePath: "/build/blog-a/index.html",
type: "blog",
url: "/base/blog-a",
},
{
filePath: "/build/blog-b/c/index.html",
type: "blog",
url: "/base/blog-b/c",
},
{
filePath: "/build/page/index.html",
type: "blog",
url: "/base/page",
},
],
},
],
],
Expand Down
Loading

0 comments on commit 6865e7c

Please sign in to comment.