Skip to content

Commit

Permalink
Merge pull request #11 from bci-oss/update-to-antora-3
Browse files Browse the repository at this point in the history
Update to Antora 3
  • Loading branch information
atextor authored Oct 25, 2023
2 parents 2cc98ce + 6f943b9 commit 655a191
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
}],
"max-len": [1, 120, 2],
"spaced-comment": "off"
},
"globals": {
"globalThis": "writable"
}
}
37 changes: 21 additions & 16 deletions src/js/vendor/search.js → src/js/vendor/search-ui.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-env browser */
window.antoraLunr = (function (lunr) {
const scriptAttrs = document.getElementById('search-script').dataset
const basePath = scriptAttrs.basePath
const pagePath = scriptAttrs.pagePath
;(globalThis || window).lunrSiteSearch = (function () {
/* eslint-disable no-var */
const config = document.getElementById('search-script').dataset
const siteRootPath = config.siteRootPath || ''
var searchInput = document.getElementById('search-input')
var searchResult = document.createElement('div')
searchResult.classList.add('search-result-dropdown-menu')
Expand All @@ -20,7 +19,7 @@ window.antoraLunr = (function (lunr) {

var end = start + length
var textEnd = text.length - 1
var contextOffset = 15
var contextOffset = 100
var contextAfter = end + contextOffset > textEnd ? textEnd : end + contextOffset
var contextBefore = start - contextOffset < 0 ? 0 : start - contextOffset
if (start === 0 && end === textEnd) {
Expand Down Expand Up @@ -93,7 +92,7 @@ window.antoraLunr = (function (lunr) {
return hits
}

function createSearchResult(result, store, searchResultDataset) {
function createSearchResult (result, store, searchResultDataset) {
result.forEach(function (item) {
var url = item.ref
var hash
Expand All @@ -115,8 +114,7 @@ window.antoraLunr = (function (lunr) {
var documentHit = document.createElement('div')
documentHit.classList.add('search-result-document-hit')
var documentHitLink = document.createElement('a')
var rootPath = basePath
documentHitLink.href = rootPath + item.ref
documentHitLink.href = siteRootPath + item.ref
documentHit.appendChild(documentHitLink)
hits.forEach(function (hit) {
documentHitLink.appendChild(hit)
Expand Down Expand Up @@ -194,13 +192,22 @@ window.antoraLunr = (function (lunr) {
}
}

function init (data) {
var index = Object.assign({index: lunr.Index.load(data.index), store: data.store})
function init (lunr, data) {
var index = Object.assign({ index: lunr.Index.load(data.index), store: data.store })
var debug = 'URLSearchParams' in window && new URLSearchParams(window.location.search).has('lunr-debug')
var search = debounce(function () {
searchIndex(index.index, index.store, searchInput.value)
try {
searchIndex(index.index, index.store, searchInput.value)
} catch (err) {
if (debug) console.debug('Invalid search query: ' + searchInput.value + ' (' + err.message + ')')
}
}, 100)
searchInput.addEventListener('keydown', search)

searchInput.addEventListener('keydown', function (event) {
if (event.key === 'Escape') searchInput.value = ''
})

// this is prevented in case of mousedown attached to SearchResultItem
searchInput.addEventListener('blur', function (e) {
while (searchResult.firstChild) {
Expand All @@ -209,7 +216,5 @@ window.antoraLunr = (function (lunr) {
})
}

return {
init: init,
}
})(window.lunr)
return { init: init }
})()
4 changes: 2 additions & 2 deletions src/partials/footer-scripts.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script src="{{uiRootPath}}/js/site.js"></script>
<script src="{{uiRootPath}}/js/vendor/lunr.min.js"></script>
<script src="{{uiRootPath}}/js/vendor/search.js" id="search-script" data-base-path="{{or siteRootPath (or site.url siteRootUrl)}}" data-page-path="{{@root.page.url}}"></script>
<script async src="{{uiRootPath}}/../search-index.js"></script>
<script src="{{uiRootPath}}/js/vendor/search-ui.js" id="search-script" data-site-root-path="{{siteRootPath}}" data-stylesheet="{{uiRootPath}}/css/search.css"></script>
<script async src="{{siteRootPath}}/search-index.js"></script>
<script async src="{{uiRootPath}}/js/vendor/highlight.js"></script>

0 comments on commit 655a191

Please sign in to comment.