Skip to content

Commit

Permalink
Merge pull request #27 from AayushSharma-1/main
Browse files Browse the repository at this point in the history
Hamburger Menu now Working
  • Loading branch information
monk1337 authored Oct 8, 2024
2 parents 21ef75a + 39f0bef commit c884722
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 73 deletions.
100 changes: 46 additions & 54 deletions _site/assets/js/just-the-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function initNav() {
const siteNav = document.getElementById('site-nav');
const mainHeader = document.getElementById('main-header');
const menuButton = document.getElementById('menu-button');

disableHeadStyleSheets();

jtd.addEvent(menuButton, 'click', function(e){
Expand All @@ -52,67 +52,53 @@ function initNav() {
menuButton.ariaPressed = false;
}
});

{%- if site.search_enabled != false and site.search.button %}
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');

jtd.addEvent(searchButton, 'click', function(e){
e.preventDefault();

mainHeader.classList.add('nav-open');
searchInput.focus();
});
{%- endif %}
}

// The <head> element is assumed to include the following stylesheets:
// 0. a <link> to /assets/css/just-the-docs-default.css
// 1. a <link> to /assets/css/just-the-docs-head-nav.css
// 2. a <style> containing the result of _includes/css/activation.scss.liquid.
// It also includes any styles provided by users in _includes/head_custom.html.
// Stylesheet 2 may be missing (compression can remove empty <style> elements)
// so disableHeadStyleSheet() needs to access it by its id.
// - a <link> to /assets/css/just-the-docs-head-nav.css,
// with id 'jtd-head-nav-stylesheet'
// - a <style> containing the result of _includes/css/activation.scss.liquid.
// To avoid relying on the order of stylesheets (which can change with HTML
// compression, user-added JavaScript, and other side effects), stylesheets
// are only interacted with via ID

function disableHeadStyleSheets() {
document.styleSheets[1].disabled = true;
const headNav = document.getElementById('jtd-head-nav-stylesheet');
if (headNav) {
headNav.disabled = true;
}

const activation = document.getElementById('jtd-nav-activation');
if (activation) {
activation.disabled = true;
}
}

{%- if site.search_enabled != false %}
// Site search

function initSearch() {
var request = new XMLHttpRequest();
request.open('GET', '{{ "assets/js/search-data.json" | relative_url }}', true);
request.open('GET', '/assets/js/search-data.json', true);

request.onload = function(){
if (request.status >= 200 && request.status < 400) {
var docs = JSON.parse(request.responseText);

lunr.tokenizer.separator = {{ site.search.tokenizer_separator | default: site.search_tokenizer_separator | default: "/[\s\-/]+/" }}
lunr.tokenizer.separator = /[\s/]+/

var index = lunr(function(){
this.ref('id');
this.field('title', { boost: 200 });
this.field('content', { boost: 2 });
{%- if site.search.rel_url != false %}
this.field('relUrl');
{%- endif %}
this.metadataWhitelist = ['position']

for (var i in docs) {
{% include lunr/custom-index.js %}

this.add({
id: i,
title: docs[i].title,
content: docs[i].content,
{%- if site.search.rel_url != false %}
relUrl: docs[i].relUrl
{%- endif %}
});
}
});
Expand Down Expand Up @@ -276,7 +262,7 @@ function searchLoaded(index, docs) {
var previewEnd = position[0] + position[1];
var ellipsesBefore = true;
var ellipsesAfter = true;
for (var k = 0; k < {{ site.search.preview_words_before | default: 5 }}; k++) {
for (var k = 0; k < 3; k++) {
var nextSpace = doc.content.lastIndexOf(' ', previewStart - 2);
var nextDot = doc.content.lastIndexOf('. ', previewStart - 2);
if ((nextDot >= 0) && (nextDot > nextSpace)) {
Expand All @@ -291,7 +277,7 @@ function searchLoaded(index, docs) {
}
previewStart = nextSpace + 1;
}
for (var k = 0; k < {{ site.search.preview_words_after | default: 10 }}; k++) {
for (var k = 0; k < 3; k++) {
var nextSpace = doc.content.indexOf(' ', previewEnd + 1);
var nextDot = doc.content.indexOf('. ', previewEnd + 1);
if ((nextDot >= 0) && (nextDot < nextSpace)) {
Expand Down Expand Up @@ -351,7 +337,7 @@ function searchLoaded(index, docs) {
resultLink.appendChild(resultPreviews);

var content = doc.content;
for (var j = 0; j < Math.min(previewPositions.length, {{ site.search.previews | default: 3 }}); j++) {
for (var j = 0; j < Math.min(previewPositions.length, 2); j++) {
var position = previewPositions[j];

var resultPreview = document.createElement('div');
Expand All @@ -367,13 +353,10 @@ function searchLoaded(index, docs) {
}
}
}

{%- if site.search.rel_url != false %}
var resultRelUrl = document.createElement('span');
resultRelUrl.classList.add('search-result-rel-url');
resultRelUrl.innerText = doc.relUrl;
resultTitle.appendChild(resultRelUrl);
{%- endif %}
}

function addHighlightedText(parent, text, start, end, positions) {
Expand Down Expand Up @@ -463,7 +446,6 @@ function searchLoaded(index, docs) {
}
});
}
{%- endif %}

// Switch theme

Expand All @@ -474,27 +456,43 @@ jtd.getTheme = function() {

jtd.setTheme = function(theme) {
var cssFile = document.querySelector('[rel="stylesheet"]');
cssFile.setAttribute('href', '{{ "assets/css/just-the-docs-" | relative_url }}' + theme + '.css');
cssFile.setAttribute('href', '/assets/css/just-the-docs-' + theme + '.css');
}

// Note: pathname can have a trailing slash on a local jekyll server
// and not have the slash on GitHub Pages

function navLink() {
var href = document.location.pathname;
if (href.endsWith('/') && href != '/') {
href = href.slice(0, -1);
var pathname = document.location.pathname;

var navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"]');
if (navLink) {
return navLink;
}
return document.getElementById('site-nav').querySelector('a[href="' + href + '"], a[href="' + href + '/"]');

// The `permalink` setting may produce navigation links whose `href` ends with `/` or `.html`.
// To find these links when `/` is omitted from or added to pathname, or `.html` is omitted:

if (pathname.endsWith('/') && pathname != '/') {
pathname = pathname.slice(0, -1);
}

if (pathname != '/') {
navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"], a[href="' + pathname + '/"], a[href="' + pathname + '.html"]');
if (navLink) {
return navLink;
}
}

return null; // avoids `undefined`
}

// Scroll site-nav to ensure the link to the current page is visible

function scrollNav() {
const targetLink = navLink();
if (targetLink) {
const rect = targetLink.getBoundingClientRect();
document.getElementById('site-nav').scrollBy(0, rect.top - 3*rect.height);
targetLink.scrollIntoView({ block: "center" });
targetLink.removeAttribute('href');
}
}
Expand All @@ -521,19 +519,16 @@ function activateNav() {
// Document ready

jtd.onReady(function(){
initNav();
{%- if site.search_enabled != false %}
if (document.getElementById('site-nav')) {
initNav();
activateNav();
scrollNav();
}
initSearch();
{%- endif %}
activateNav();
scrollNav();
});

// Copy button on code


{%- if site.enable_copy_code_button != false %}

jtd.onReady(function(){

if (!window.isSecureContext) {
Expand Down Expand Up @@ -574,8 +569,5 @@ jtd.onReady(function(){

});

{%- endif %}

})(window.jtd = window.jtd || {});

{% include js/custom.js %}
73 changes: 55 additions & 18 deletions assets/js/just-the-docs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
layout: null
---
(function (jtd, undefined) {

// Event handling
Expand Down Expand Up @@ -36,7 +39,7 @@ function initNav() {
const siteNav = document.getElementById('site-nav');
const mainHeader = document.getElementById('main-header');
const menuButton = document.getElementById('menu-button');

disableHeadStyleSheets();

jtd.addEvent(menuButton, 'click', function(e){
Expand Down Expand Up @@ -67,15 +70,19 @@ function initNav() {
}

// The <head> element is assumed to include the following stylesheets:
// 0. a <link> to /assets/css/just-the-docs-default.css
// 1. a <link> to /assets/css/just-the-docs-head-nav.css
// 2. a <style> containing the result of _includes/css/activation.scss.liquid.
// It also includes any styles provided by users in _includes/head_custom.html.
// Stylesheet 2 may be missing (compression can remove empty <style> elements)
// so disableHeadStyleSheet() needs to access it by its id.
// - a <link> to /assets/css/just-the-docs-head-nav.css,
// with id 'jtd-head-nav-stylesheet'
// - a <style> containing the result of _includes/css/activation.scss.liquid.
// To avoid relying on the order of stylesheets (which can change with HTML
// compression, user-added JavaScript, and other side effects), stylesheets
// are only interacted with via ID

function disableHeadStyleSheets() {
document.styleSheets[1].disabled = true;
const headNav = document.getElementById('jtd-head-nav-stylesheet');
if (headNav) {
headNav.disabled = true;
}

const activation = document.getElementById('jtd-nav-activation');
if (activation) {
activation.disabled = true;
Expand Down Expand Up @@ -139,6 +146,18 @@ function searchLoaded(index, docs) {
var currentInput;
var currentSearchIndex = 0;

{%- if site.search.focus_shortcut_key %}
// add event listener on ctrl + <focus_shortcut_key> for showing the search input
jtd.addEvent(document, 'keydown', function (e) {
if ((e.ctrlKey || e.metaKey) && e.key === '{{ site.search.focus_shortcut_key }}') {
e.preventDefault();

mainHeader.classList.add('nav-open');
searchInput.focus();
}
});
{%- endif %}

function showSearch() {
document.documentElement.classList.add('search-active');
}
Expand Down Expand Up @@ -481,20 +500,36 @@ jtd.setTheme = function(theme) {
// and not have the slash on GitHub Pages

function navLink() {
var href = document.location.pathname;
if (href.endsWith('/') && href != '/') {
href = href.slice(0, -1);
var pathname = document.location.pathname;

var navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"]');
if (navLink) {
return navLink;
}
return document.getElementById('site-nav').querySelector('a[href="' + href + '"], a[href="' + href + '/"]');

// The `permalink` setting may produce navigation links whose `href` ends with `/` or `.html`.
// To find these links when `/` is omitted from or added to pathname, or `.html` is omitted:

if (pathname.endsWith('/') && pathname != '/') {
pathname = pathname.slice(0, -1);
}

if (pathname != '/') {
navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"], a[href="' + pathname + '/"], a[href="' + pathname + '.html"]');
if (navLink) {
return navLink;
}
}

return null; // avoids `undefined`
}

// Scroll site-nav to ensure the link to the current page is visible

function scrollNav() {
const targetLink = navLink();
if (targetLink) {
const rect = targetLink.getBoundingClientRect();
document.getElementById('site-nav').scrollBy(0, rect.top - 3*rect.height);
targetLink.scrollIntoView({ block: "center" });
targetLink.removeAttribute('href');
}
}
Expand All @@ -521,12 +556,14 @@ function activateNav() {
// Document ready

jtd.onReady(function(){
initNav();
if (document.getElementById('site-nav')) {
initNav();
activateNav();
scrollNav();
}
{%- if site.search_enabled != false %}
initSearch();
{%- endif %}
activateNav();
scrollNav();
});

// Copy button on code
Expand Down Expand Up @@ -578,4 +615,4 @@ jtd.onReady(function(){

})(window.jtd = window.jtd || {});

{% include js/custom.js %}
{% include js/custom.js %}
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
layout: default
layout: home
title: Home
nav_order: 1
description: "Open Source AI Research & Mentorship"
Expand Down

0 comments on commit c884722

Please sign in to comment.