Skip to content

Commit

Permalink
feat: indexBaseUrl option added
Browse files Browse the repository at this point in the history
  • Loading branch information
lelouch77 committed Jul 15, 2020
1 parent 0318ee9 commit 301de53
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ Offline Search for Docusaurus V2

[![npm version](https://badge.fury.io/js/docusaurus-lunr-search.svg)](https://www.npmjs.com/package/docusaurus-lunr-search)

## Prerequisites
`worker_thread` is needed, suggested node version > 12.X
For older version of node use `docusaurus-lunr-search` version `2.1.0`
(`npm i [email protected]`)

## Sample
<p align="center">
<img width="460" height="300" src="https://raw.githubusercontent.com/lelouch77/docusaurus-lunr-search/master/assets/search-offline.png">
Expand Down Expand Up @@ -71,7 +66,16 @@ module.exports = {
}],
}
```

### indexBaseUrl
Base url will not indexed by default, if you want to index the base url set this option to `true`
```
module.exports = {
// ...
plugins: [[ require.resolve('docusaurus-lunr-search'), {
indexBaseUrl: true
}],
}
```
Thanks to [`algolia/docsearch.js`](https://github.com/algolia/docsearch), I modified it to create this search component

And thanks [cmfcmf](https://github.com/cmfcmf), I used the code from his library [docusaurus-search-local](https://github.com/cmfcmf/docusaurus-search-local) for multi-language support.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docusaurus-lunr-search",
"version": "2.1.4",
"version": "2.1.6",
"description": "Offline search component for Docusaurus V2",
"main": "src/index.js",
"publishConfig": {
Expand Down
10 changes: 5 additions & 5 deletions src/html-to-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const toText = require('hast-util-to-text')
const is = require('unist-util-is')
const toVfile = require('to-vfile')

const sectionHeaderTypes = ['h2', 'h3']
const sectionHeaderTest = ({ tagName }) => ['h2', 'h3'].includes(tagName)

// Build search data for a html
function * scanDocuments({ path, url }) {
function* scanDocuments({ path, url }) {
let vfile
try {
vfile = toVfile.readSync(path)
Expand Down Expand Up @@ -61,7 +61,7 @@ function * scanDocuments({ path, url }) {
keywords,
}

for (const sectonDesc of sectionHeaders) {
for (const sectionDesc of sectionHeaders) {
const { title, content, ref } = sectionDesc;
yield {
title,
Expand All @@ -84,7 +84,7 @@ function getSectionHeaders(markdown) {

const emitCurrent = () => {
result.push({
title: toText(currentSection).replace(/^#+/, ''),
title: toText(currentSection).replace(/^#+/, '').replace(/#$/, ''),
ref: select('.anchor', currentSection).properties.id,
content: contentsAcc,
})
Expand All @@ -93,7 +93,7 @@ function getSectionHeaders(markdown) {
}

for (const node of markdown.children) {
if (is(node, sectionHeaderTypes)) {
if (is(node, sectionHeaderTest)) {
if (currentSection) {
emitCurrent()
}
Expand Down
13 changes: 9 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,27 @@ function generateLunrClientJS(outDir, language = "en") {

function getFilePaths(routesPaths, outDir, baseUrl, options = {}) {
const files = []
const { excludeRoutes = [] } = options
const addedFiles = new Set();
const { excludeRoutes = [], indexBaseUrl = false } = options
const meta = {
excludedCount: 0,
}

routesPaths.forEach((route) => {
if (route === baseUrl || route === `${baseUrl}404.html`) return
if ((!indexBaseUrl && route === baseUrl) || route === `${baseUrl}404.html`) return
route = route.substr(baseUrl.length)
const filePath = path.join(outDir, route, "index.html")
// In case docs only mode routesPaths has baseUrl twice
if(addedFiles.has(filePath)) return
if (excludeRoutes.some((excludePattern) => minimatch(route, excludePattern))) {
meta.excludedCount++
return
}
files.push({
path: path.join(outDir, route, "index.html"),
path: filePath,
url: route,
})
});
addedFiles.add(filePath);
})
return [files, meta]
}
Expand Down

0 comments on commit 301de53

Please sign in to comment.