Skip to content

Commit

Permalink
Merge pull request #104 from lxix/feature/display-version-numbers
Browse files Browse the repository at this point in the history
Add version display feature
  • Loading branch information
praveenn77 committed May 4, 2023
2 parents c45117f + a692a85 commit a26e57a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ module.exports = {
],
}
```

### disableVersioning
Docs versions are displayed by default. If you want to hide it, set this plugin option to `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
12 changes: 12 additions & 0 deletions src/html-to-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function* scanDocuments({ path, url }) {
return acc
}, []).join(' ')

let version = null;
if (workerData.loadedVersions) {
const docsearchVersionElement = select('meta[name="docsearch:version"]', hast);

version = docsearchVersionElement
? workerData.loadedVersions[docsearchVersionElement.properties.content]
: null;
}


yield {
title: pageTitle,
type: 0,
Expand All @@ -59,6 +69,7 @@ function* scanDocuments({ path, url }) {
// If there is no sections then push the complete content under page title
content: sectionHeaders.length === 0 ? getContent(markdown) : '',
keywords,
version,
}

for (const sectionDesc of sectionHeaders) {
Expand All @@ -69,6 +80,7 @@ function* scanDocuments({ path, url }) {
pageTitle,
url: `${url}#${ref}`,
content,
version,
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ module.exports = function (context, options) {
async contentLoaded({actions}) {
actions.setGlobalData({"fileNames": fileNames})
},
async postBuild({ routesPaths = [], outDir, baseUrl }) {
async postBuild({ routesPaths = [], outDir, baseUrl, plugins }) {
console.log('docusaurus-lunr-search:: Building search docs and lunr index file')
console.time('docusaurus-lunr-search:: Indexing time')

const docsPlugin = plugins.find((plugin) => plugin.name == 'docusaurus-plugin-content-docs');

const [files, meta] = utils.getFilePaths(routesPaths, outDir, baseUrl, options)
if (meta.excludedCount) {
console.log(`docusaurus-lunr-search:: ${meta.excludedCount} documents were excluded from the search by excludeRoutes config`)
Expand All @@ -58,6 +60,13 @@ module.exports = function (context, options) {
}
})

const loadedVersions = docsPlugin && !docsPlugin.options.disableVersioning && !(options.disableVersioning ?? false)
? docsPlugin.content.loadedVersions.reduce(function (accum, currentVal) {
accum[currentVal.versionName] = currentVal.label;
return accum;
}, {})
: null;

const addToSearchData = (d) => {
lunrBuilder.add({
id: searchDocuments.length,
Expand All @@ -68,7 +77,7 @@ module.exports = function (context, options) {
searchDocuments.push(d);
}

const indexedDocuments = await buildSearchData(files, addToSearchData)
const indexedDocuments = await buildSearchData(files, addToSearchData, loadedVersions)
const lunrIndex = lunrBuilder.build()
console.timeEnd('docusaurus-lunr-search:: Indexing time')
console.log(`docusaurus-lunr-search:: indexed ${indexedDocuments} documents out of ${files.length}`)
Expand Down Expand Up @@ -103,7 +112,7 @@ module.exports = function (context, options) {
};
};

function buildSearchData(files, addToSearchData) {
function buildSearchData(files, addToSearchData, loadedVersions) {
if (!files.length) {
return Promise.resolve()
}
Expand Down Expand Up @@ -138,7 +147,11 @@ function buildSearchData(files, addToSearchData) {
if (nextIndex >= files.length) {
break
}
const worker = new Worker(path.join(__dirname, 'html-to-doc.js'))
const worker = new Worker(path.join(__dirname, 'html-to-doc.js'), {
workerData: {
loadedVersions: loadedVersions
},
})
worker.on('error', reject)
worker.on('message', (message) => {
handleMessage(message, worker)
Expand Down
4 changes: 3 additions & 1 deletion src/theme/SearchBar/DocSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class DocSearch {
!isLvl2 &&
(subcategory && subcategory !== "" && subcategory !== category);
const isLvl0 = !isLvl1 && !isLvl2;
const version = hit.version;

return {
isLvl0,
Expand All @@ -234,7 +235,8 @@ class DocSearch {
subcategory,
title: displayTitle,
text,
url
url,
version
};
});
}
Expand Down
8 changes: 8 additions & 0 deletions src/theme/SearchBar/algolia.css
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@
padding-right: 2px;
}

.algolia-autocomplete .algolia-docsearch-suggestion--version {
display: block;
font-size: 0.65em;
color: #a6aab1;
padding-top: 2px;
padding-right: 2px;
}

.algolia-autocomplete .algolia-docsearch-suggestion--no-results {
width: 100%;
padding: 8px 0;
Expand Down
1 change: 1 addition & 0 deletions src/theme/SearchBar/lunar-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LunrSearchAdapter {
lvl1: doc.type === 0 ? null : doc.title
},
url: doc.url,
version: doc.version,
_snippetResult: formattedContent ? {
content: {
value: formattedContent,
Expand Down
1 change: 1 addition & 0 deletions src/theme/SearchBar/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const templates = {
<div class="${suggestionPrefix}--subcategory-inline">{{{subcategory}}}</div>
<div class="${suggestionPrefix}--title">{{{title}}}</div>
{{#text}}<div class="${suggestionPrefix}--text">{{{text}}}</div>{{/text}}
{{#version}}<div class="${suggestionPrefix}--version">{{version}}</div>{{/version}}
</div>
{{/isTextOrSubcategoryNonEmpty}}
</div>
Expand Down

0 comments on commit a26e57a

Please sign in to comment.