diff --git a/README.md b/README.md index a47fe81..cd555c1 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,17 @@ npm i docusaurus-lunr-search --save ``` npm install ``` -3. Then run docusaurus swizzle -``` -npm run swizzle docusaurus-lunr-search SearchBar -- --danger -``` -4. Add the docusaurus-lunr-search plugin to your `docusaurus.config.js` +3. Add the docusaurus-lunr-search plugin to your `docusaurus.config.js` ``` module.exports = { // ... plugins: [require.resolve('docusaurus-lunr-search')], } ``` +4. Then run docusaurus swizzle +``` +npm run swizzle docusaurus-lunr-search SearchBar -- --eject --danger +``` 5. Then build your Docusaurus project ``` npm run build diff --git a/src/theme/SearchBar/lib/DocSearch.js b/src/theme/SearchBar/DocSearch.js similarity index 100% rename from src/theme/SearchBar/lib/DocSearch.js rename to src/theme/SearchBar/DocSearch.js diff --git a/src/theme/SearchBar/index.js b/src/theme/SearchBar/index.js index 2ebc647..3ccdb4b 100644 --- a/src/theme/SearchBar/index.js +++ b/src/theme/SearchBar/index.js @@ -57,7 +57,7 @@ const Search = props => { Promise.all([ getSearchDoc(), getLunrIndex(), - import("./lib/DocSearch"), + import("./DocSearch"), import("./algolia.css") ]).then(([searchDocs, searchIndex, { default: DocSearch }]) => { if (searchDocs.length === 0) { diff --git a/src/theme/SearchBar/lib/lunar-search.js b/src/theme/SearchBar/lunar-search.js similarity index 98% rename from src/theme/SearchBar/lib/lunar-search.js rename to src/theme/SearchBar/lunar-search.js index a72ea5b..31728f3 100644 --- a/src/theme/SearchBar/lib/lunar-search.js +++ b/src/theme/SearchBar/lunar-search.js @@ -26,7 +26,7 @@ class LunrSearchAdapter { lvl0: doc.pageTitle || doc.title, lvl1: doc.type === 0 ? null : doc.title }, - url: this.baseUrl + doc.url, + url: this.baseUrl !== '/' || doc.url.charAt(0) !== '/' ? this.baseUrl + doc.url : doc.url, _snippetResult: formattedContent ? { content: { value: formattedContent, diff --git a/src/theme/SearchBar/lib/templates.js b/src/theme/SearchBar/templates.js similarity index 100% rename from src/theme/SearchBar/lib/templates.js rename to src/theme/SearchBar/templates.js diff --git a/src/theme/SearchBar/lib/utils.js b/src/theme/SearchBar/utils.js similarity index 100% rename from src/theme/SearchBar/lib/utils.js rename to src/theme/SearchBar/utils.js diff --git a/src/utils.js b/src/utils.js index fa10db4..60b19d8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -64,17 +64,21 @@ function getFilePaths(routesPaths, outDir, baseUrl, options = {}) { if (route === `${baseUrl}404.html`) return const isBaseUrl = route === baseUrl - let filePath; if (isBaseUrl && !indexBaseUrl) { return; } - route = route.substring(baseUrl.length) - if (isBaseUrl || route.endsWith(path.sep)) { - filePath = path.join(outDir, route, "index.html") - } else { - filePath = path.join(outDir, `${route}.html`) + const candidatePaths = [ + path.join(outDir, `${route}.html`), + path.join(outDir, route, "index.html") + ] + + const filePath = candidatePaths.find(fs.existsSync); + if(!fs.existsSync(filePath)) { + // if this error occurs, likely docusaurus changed some file generation aspects + // and we need to update the candidates above + console.warn(`docusaurus-lunr-search: could not resolve file for route '${route}', it will be missing in the search index`); } // In case docs only mode routesPaths has baseUrl twice