');
- const a = li.querySelector('a'), small = li.querySelector('small');
+ function searchValue(fuzzy) {
+ // Operators:
+ // +: means AND. i.e +sdk +metaverse will found words that contains sdk & metaverse
+ // ~n: looks for N fuzzy words. i.e. metaverse~1 => metavese ✅
+ return value.split(' ').map(val => {
+ // Avoid blankspaces
+ if (!val) return
+ // if its a short word or fuzzy option is turned off, then return only the value with the +operator
+ if (val.length <= 4 || !fuzzy) return `+${val}`
- a.href = page.href;
- a.textContent = page.title;
- small.textContent = page.section;
+ return `+${val}~1`
+ }).filter(a => !!a).join(' ')
+ }
- results.appendChild(li);
+ function getSearchHits() {
+ // First search for the words without fuzzy, so we can have a more accurate result.
+ const hits = window.lunrIdx.search(searchValue()).slice(0, LIMIT_RESULTS);
+ if (hits.length) return hits
+ return window.lunrIdx.search(searchValue(true)).slice(0, LIMIT_RESULTS);
+ }
+ const searchHits = getSearchHits()
+ showSearchBox()
+ if (!searchHits.length) {
+ resultCard(`Not Found`, `Sorry, we couldn't find any matches. Try searching for a different keyword`)
+ return
+ }
+ searchHits.forEach((hit) => {
+ const document = documents.get(Number(hit.ref))
+ if (!document) return
+ const highlightedContent = highlightContent(document.content, hit)
+ resultCard(document.title, highlightedContent, document.href)
});
}
+ function resultCard(title, content, href) {
+ const li = element('
');
+ if (href) li.querySelector('a').href = href;
+ li.querySelector('h4').textContent = title;
+ li.querySelector('span').innerHTML = content
+ results.appendChild(li);
+ }
+
+ function highlightContent(content, hit) {
+ const amountLetters = 60
+ const { metadata } = hit.matchData
+ let from = 0
+ let to = 100
+ const keys = Object.keys(metadata).sort()
+ for (const key of keys) {
+ const positions = metadata[key]?.content?.position
+ if (!positions) {
+ continue
+ }
+ for (const position of positions) {
+ const positionStart = position[0]
+ from = Math.max(0, content.length - positionStart <= amountLetters
+ ? positionStart - amountLetters * 2
+ : positionStart - amountLetters)
+ to = positionStart + position[1] + amountLetters
+ }
+ break
+ }
+ let value = content.slice(from, to)
+ if (from !== 0) {
+ value = `...${value}`
+ }
+ for (const key of keys) {
+ value = value.replace(new RegExp(key, 'gi'), '$&')
+ }
+
+ return value + '...'
+ }
+
+ // HELPERS
/**
* @param {String} content
* @returns {Node}
@@ -101,4 +176,26 @@
div.innerHTML = content;
return div.firstChild;
}
+
+ function hide(element) {
+ element.classList.add('hidden')
+ }
+
+ function show(element) {
+ element.classList.remove('hidden')
+ }
+
+ function showSearchBox() {
+ if (!resultsContainer.classList.contains('hidden')) {
+ return
+ }
+ resultsContainer.scrollTop = 0
+ show(searchOverlay)
+ show(resultsContainer)
+ }
+
+ function hideSearchBox() {
+ hide(searchOverlay)
+ hide(resultsContainer)
+ }
})();
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html
index f7e116d..685903d 100644
--- a/layouts/_default/baseof.html
+++ b/layouts/_default/baseof.html
@@ -4,7 +4,7 @@
{{ partial "docs/html-head" . }}
{{ partial "docs/inject/head" . }}
-
+
@@ -24,6 +24,44 @@
+
+ {{ if .IsHome }}
+
+
+
+
+
Decentraland Documentation
+
The first-ever virtual world owned by its users
+
+ {{ partial "docs/search" . }}
+
+ This site is maintained by the Decentraland Foundation.
+ The content in this site is divided into sections for different user profiles. Choose a section that matches your needs.
+