1function initHotKeys() {
3 var bodyEl = document.body;
4 var searchInput = document.getElementById('search');
6 document.addEventListener('keydown', function (event) {
7 if (event.defaultPrevented) {
11 var key = event.key || event.keyCode;
13 // ESC to close search window
14 if (key === 'Escape' || key === 'Esc' || key === 27) {
15 document.body.classList.remove('has-active-search-window');
18 // / to close toggle window
19 if (key === '/' || key === 'Period' || key === 58) {
20 event.preventDefault();
21 document.body.classList.toggle('has-active-search-window');
22 if (bodyEl.classList.contains('has-active-search-window')) {
27 //when search is active TAB puts search input into focus
28 if (bodyEl.classList.contains('has-active-search-window')) {
29 if (key === 'Tab' || key === 'Tab' || key === 9) {
37// search functionality
38function debounce(func, wait) {
44 clearTimeout(timeout);
46 timeout = setTimeout(function () {
48 func.apply(context, args);
53// The strategy is as follows:
54// First, assign a value to each word in the document:
55// Words that correspond to search terms (stemmer aware): 40
57// First word in a sentence: 8
58// Then use a sliding window with a constant number of words and count the
59// sum of the values of the words within the window. Then use the window that got the
60// maximum sum. If there are multiple maximas, then get the last one.
61// Enclose the terms in <b>.
62function makeTeaser(body, terms) {
64 var NORMAL_WORD_WEIGHT = 2;
65 var FIRST_WORD_WEIGHT = 8;
66 //var TEASER_MAX_WORDS = 17;
67 var TEASER_MAX_CHARS = 63;
69 var stemmedTerms = terms.map(function (w) {
70 return elasticlunr.stemmer(w.toLowerCase());
73 var termFound = false;
75 var weighted = []; // contains elements of ["word", weight, index_in_document]
77 // split in sentences, then words
78 var sentences = body.toLowerCase().split(". ");
80 for (var i in sentences) {
81 var words = sentences[i].split(" ");
82 var value = FIRST_WORD_WEIGHT;
84 for (var j in words) {
87 if (word.length > 0) {
88 for (var k in stemmedTerms) {
89 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
94 weighted.push([word, value, index]);
95 value = NORMAL_WORD_WEIGHT;
99 index += 1; // ' ' or '.' if last word in sentence
102 index += 1; // because we split at a two-char boundary '. '
105 if (weighted.length === 0) {
109 var windowWeights = [];
112 // loop over words (weighted) till max chars is reached adding a counter to windowSize every time a new word has passed
114 for (var i = 0; i < weighted.length; i++) {
115 sumChars += weighted[i][0].length;
116 if (sumChars <= TEASER_MAX_CHARS) {
121 // We add a window with all the weights first
123 for (var i = 0; i < windowSize; i++) {
124 curSum += weighted[i][1];
126 windowWeights.push(curSum);
128 for (var i = 0; i < weighted.length - windowSize; i++) {
129 curSum -= weighted[i][1];
130 curSum += weighted[i + windowSize][1];
131 windowWeights.push(curSum);
134 // If we didn't find the term, just pick the first window
139 for (var i = windowWeights.length - 1; i >= 0; i--) {
140 if (windowWeights[i] > maxFound) {
141 maxFound = windowWeights[i];
148 var startIndex = weighted[maxSumIndex][2];
149 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
150 var word = weighted[i];
151 if (startIndex < word[2]) {
152 // missing text from index to start of `word`
153 teaser.push(body.substring(startIndex, word[2]));
154 startIndex = word[2];
157 startIndex = word[2] + word[0].length;
158 teaser.push(body.substring(word[2], startIndex));
162 return teaser.join("");
165function formatSearchResultTitle(item) {
166 var pathArray = item.ref.split( '/' );
168 var firstPartTitle = pathArray[3];
169 let firstTitleCapitals = firstPartTitle.charAt(0).toUpperCase() + firstPartTitle.slice(1);
171 var lastPartTitle = item.doc.title;
173 var fullTitle = firstTitleCapitals + '<span class="gray1 dib pl2">' + ' / ' + lastPartTitle + '</span>';
178function formatSearchResultItem(item, terms) {
179 var li = document.createElement("li");
180 var createA = document.createElement("a");
181 li.appendChild(createA);
182 var teaserTitle = formatSearchResultTitle(item);
183 li.classList.add("search-results__item");
184 var hrefA = item.ref;
185 createA.setAttribute('href', hrefA);
186 createA.setAttribute('class','no-underline db pl1 pv2');
187 createA.innerHTML = `<span class="capitalize ph3">${teaserTitle}</span>`;
188 createA.innerHTML += `<span class='dn arrow fr'>↩</span>`;
189 createA.innerHTML += `<div class="truncate pr2 ph3">${makeTeaser(item.doc.body, terms)}</div>`;
193function initSearch() {
194 var searchInput = document.getElementById("search");
195 var searchForm = document.getElementById("search-form");
196 var inputReset = searchForm.nextElementSibling;
201 var glossaryResults = document.querySelector(".glossary-results");
202 var glossaryResultsHeader = document.querySelector(".glossary-results__header");
203 var glossaryResultsItem = document.querySelector(".glossary-results__item");
204 var searchResults = document.querySelector(".search-results");
205 var searchResultsHeader = document.querySelector(".search-results__header");
206 var searchResultsItems = document.querySelector(".search-results__items");
210 expand: true, // turn on partial word search
216 var currentTerm = "";
217 var index = elasticlunr.Index.load(window.searchIndex);
218 // toggle search window
219 var toggleSearchWindow = document.getElementById('js-search-window-toggle');
220 var bodyEl = document.body;
221 var searchOverlay = document.getElementById('search-overlay');
222 var searchWindow = document.getElementById('search-window');
223 var searchActive = bodyEl.classList.contains('has-active-search-window');
225 if (toggleSearchWindow !== null) {
226 toggleSearchWindow.onclick = function() {
227 inputReset.style.display="none";
230 bodyEl.classList.toggle('has-active-search-window');
231 //put input in focus when activating search
234 searchInput.value = "";
235 searchResultsHeader.value = "";
236 searchResults.style.display = searchInput.value.trim() === "" ? "none" : "block";
238 searchOverlay.addEventListener('click', function(e){
239 if (!searchWindow.contains(e.target)){
240 bodyEl.classList.toggle('has-active-search-window');
245 // Don't refresh the page on enter
246 searchInput.addEventListener('keydown', function(event){
247 var key = event.key || event.keyCode;
249 if (key === 13 || key === "Enter") {
250 event.preventDefault();
256 searchInput.addEventListener("keyup", debounce(function() {
257 inputReset.style.display="block";
258 glossaryResults.style.display="none";
259 searchResultsHeader.value = "";
260 var term = searchInput.value.trim();
262 //removed to fix: a state where there’s a valid search term, but no results
264 if (term === currentTerm || !index) {
268 searchResults.style.display = term === "" ? "none" : "block";
269 searchResults.style.overflowY = term === "" ? "hidden" : "scroll";
270 searchResultsItems.innerHTML = "";
272 inputReset.style.display="none";
276 for (let rune of runes) {
277 if (rune.symbol === term) {
278 glossaryResults.style.display="block";
279 glossaryResultsItem.innerHTML = `
280 <h3 class="black"><code class="red3 mr1">${rune.symbol}</code> ${rune.name}</h3>
281 <p class="black">${rune.desc}</p>
282 <a href="${rune.link}" class="db tr black fw5" style="font-family: 'Inter UI', sans-serif;">Read more in Documentation -></a>
287 var results = index.search(term, options).filter(function (r) {
288 return r.doc.body !== "";
291 if (results.length === 0) {
292 searchResultsHeader.innerText = `No search results for '${term}'.`;
297 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
298 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
299 if (!results[i].doc.body) {
302 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
306 // reset input button
307 inputReset.onclick=function(){
308 document.getElementById('search-form').reset();
310 searchForm.addEventListener("reset", function(event){
311 var e = document.createEvent('KeyboardEvent');
312 e.initEvent("keyup", false, true);
313 searchInput.dispatchEvent(e);
318if (document.readyState === "complete" ||
319 (document.readyState !== "loading" && !document.documentElement.doScroll)
321 initToggleSearchWindow();
323 document.addEventListener("DOMContentLoaded", function () {
329if (window.location.href.includes("docs")) {
332 const title = reference.getAttribute('title')
333 reference.removeAttribute('title')
340 let docsSelect = document.getElementById('docsSelect');
341 let goTo = function() {
342 let url = docsSelect.options[docsSelect.selectedIndex].value;
343 if (url.startsWith("http")) {
344 document.location.assign(url);
348 docsSelect.addEventListener('change', goTo);
351// same-page navigation on-scroll behaviour
353if (document.body.classList.contains("page-indiced")) {
354let h2 = document.getElementsByTagName("h2");
355let h3 = document.getElementsByTagName("h3");
356let all = document.querySelectorAll("nav li a");
357let headers = document.querySelectorAll("details summary");
359// smooth scrolling on click
361 link.addEventListener("click", event => {
362 event.preventDefault();
363 let target = document.querySelector(event.target.hash);
364 target.scrollIntoView({
374window.addEventListener("scroll", event => {
375 let fromTop = window.scrollY;
377 all.forEach(link => {
378 let section = document.querySelector(link.hash);
379 let sectionChildren = document.querySelector(link.hash).nextElementSibling;
382 ((section.offsetTop <= fromTop) ||
383 (sectionChildren.offsetTop <= fromTop)) &&
384 (section.offsetTop + section.offsetHeight + sectionChildren.offsetHeight > fromTop)
386 link.classList.add("current");
388 link.classList.remove("current");
393let oceanvid = document.getElementById("ocean");
395if ((oceanvid !== null) && (window.innerWidth > window.innerHeight)) {
396 oceanvid.innerHTML = `<source
397 src="https://storage.googleapis.com/media.urbit.org/site/sea30-1440.mp4"
399 Your browser does not support the video tag.`
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list