Skip to content

Commit

Permalink
Add custom layout
Browse files Browse the repository at this point in the history
  • Loading branch information
OrfeasZ committed Dec 18, 2019
1 parent 4a9d652 commit ca9851e
Show file tree
Hide file tree
Showing 14 changed files with 462 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
resources/_gen
283 changes: 283 additions & 0 deletions assets/js/offline-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
// Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/

(function($) {
'use strict';

window.renderSearch = function(indexURL) {
const $searchResults = $('#search-results');

const params = new URLSearchParams(window.location.search);
const searchQuery = params.get('q');

// Download the index and search it.
let idx = null; // Lunr index
const resultDetails = new Map();

$.ajax(indexURL).then(
data => {
idx = lunr(function() {
this.ref('ref');
this.field('title', { boost: 2 });
this.field('body');

data.forEach(doc => {
this.add(doc);

resultDetails.set(doc.ref, {
title: doc.title,
excerpt: doc.excerpt
});
});
});

const results = idx
.query(q => {
const tokens = lunr.tokenizer(searchQuery.toLowerCase());
tokens.forEach(token => {
const queryString = token.toString();
q.term(queryString, {
boost: 100
});
q.term(queryString, {
wildcard:
lunr.Query.wildcard.LEADING |
lunr.Query.wildcard.TRAILING,
boost: 10
});
q.term(queryString, {
editDistance: 2
});
});
})
.slice(0, 10);

//
// Make result html
//

$searchResults.html('');
const $html = $searchResults;

$html.append(
$('<div>')
.css({
display: 'flex',
justifyContent: 'space-between',
marginBottom: '1em'
})
);

const $searchResultBody = $('<div>').css({
overflowY: 'auto'
});
$html.append($searchResultBody);

if (results.length === 0) {
$searchResultBody.append(
$('<p>').text(`No results found for query "${searchQuery}"`)
);
} else {
results.forEach(r => {
const $cardHeader = $('<div>').addClass('card-header');
const doc = resultDetails.get(r.ref);

$cardHeader.append(
$('<a>')
.attr('href', r.ref)
.text(doc.title)
);

const $cardBody = $('<div>').addClass('card-body');
$cardBody.append(
$('<p>')
.addClass('card-text text-muted')
.text(doc.excerpt)
);

const $card = $('<div>').addClass('card');
$card.append($cardHeader).append($cardBody);

$searchResultBody.append($card);
});
}
}
);
};

$(document).ready(function() {
const $searchInput = $('.td-search-input');

//
// Options for popover
//

$searchInput.data('html', true);
$searchInput.data('placement', 'bottom');

//
// Register handler
//

$searchInput.on('change', event => {
render($(event.target));
});

// Redirect to full results page when someone presses enter.
$searchInput.keypress(e => {
if (e.which !== 13) {
return true;
}

document.location.href = '/search?q=' + encodeURIComponent($searchInput.val());

return false;
});

// Prevent reloading page by enter key on sidebar search.
$searchInput.closest('form').on('submit', () => {
return false;
});

//
// Lunr
//

let idx = null; // Lunr index
const resultDetails = new Map(); // Will hold the data for the search results (titles and summaries)

// Set up for an Ajax call to request the JSON data file that is created by Hugo's build process
$.ajax($searchInput.data('offline-search-index-json-url')).then(
data => {
idx = lunr(function() {
this.ref('ref');
this.field('title', { boost: 2 });
this.field('body');

data.forEach(doc => {
this.add(doc);

resultDetails.set(doc.ref, {
title: doc.title,
excerpt: doc.excerpt
});
});
});

$searchInput.trigger('change');
}
);

const render = $targetSearchInput => {
// Dispose the previous result
$targetSearchInput.popover('dispose');

//
// Search
//

if (idx === null) {
return;
}

const searchQuery = $targetSearchInput.val();
if (searchQuery === '') {
return;
}

const results = idx
.query(q => {
const tokens = lunr.tokenizer(searchQuery.toLowerCase());
tokens.forEach(token => {
const queryString = token.toString();
q.term(queryString, {
boost: 100
});
q.term(queryString, {
wildcard:
lunr.Query.wildcard.LEADING |
lunr.Query.wildcard.TRAILING,
boost: 10
});
q.term(queryString, {
editDistance: 2
});
});
})
.slice(0, 10);

//
// Make result html
//

const $html = $('<div>');

$html.append(
$('<div>')
.css({
display: 'flex',
justifyContent: 'space-between',
marginBottom: '1em'
})
.append(
$('<span>')
.text('Search results')
.css({ fontWeight: 'bold' })
)
.append(
$('<i>')
.addClass('fas fa-times search-result-close-button')
.css({
cursor: 'pointer'
})
)
);

const $searchResultBody = $('<div>').css({
maxHeight: `calc(100vh - ${$targetSearchInput.offset().top +
180}px)`,
overflowY: 'auto'
});
$html.append($searchResultBody);

if (results.length === 0) {
$searchResultBody.append(
$('<p>').text(`No results found for query "${searchQuery}"`)
);
} else {
results.forEach(r => {
const $cardHeader = $('<div>').addClass('card-header');
const doc = resultDetails.get(r.ref);

$cardHeader.append(
$('<a>')
.attr('href', r.ref)
.text(doc.title)
);

const $cardBody = $('<div>').addClass('card-body');
$cardBody.append(
$('<p>')
.addClass('card-text text-muted')
.text(doc.excerpt)
);

const $card = $('<div>').addClass('card');
$card.append($cardHeader).append($cardBody);

$searchResultBody.append($card);
});
}

$targetSearchInput.on('shown.bs.popover', () => {
$('.search-result-close-button').on('click', () => {
$targetSearchInput.val('');
$targetSearchInput.trigger('change');
});
});

$targetSearchInput
.data('content', $html[0].outerHTML)
.popover('show');
};
});
})(jQuery);
Empty file.
57 changes: 35 additions & 22 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
baseURL = "/"
title = "VU Documentation"

theme = ["docsy"]

enableGitInfo = true

contentDir = "content"
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = false

[languages]
[languages.en]
title = "VU Documentation"
description = "Venice Unleashed documentation"
languageName ="English"

[params]
copyright = "Venice Unleashed"
github_repo = "https://github.com/EmulatorNexus/VU-Docs"
algolia_docsearch = false
offlineSearch = true
baseURL = "/"
title = "VU Documentation"

theme = ["docsy"]

enableGitInfo = true

contentDir = "content"
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = false

[languages]
[languages.en]
title = "VU Documentation"
description = "Venice Unleashed documentation"
languageName ="English"

[params]
copyright = "Venice Unleashed"
github_repo = "https://github.com/EmulatorNexus/VU-Docs"
algolia_docsearch = false
offlineSearch = true

[params.ui.feedback]
enable = false

[markup]
[markup.highlight]
codeFences = true
hl_Lines = ""
lineNoStart = 1
lineNos = false
lineNumbersInTable = true
noClasses = true
style = "igor"
4 changes: 4 additions & 0 deletions content/search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Search Results
layout: search
---
30 changes: 30 additions & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="{{ .Site.Language.Lang }}" class="no-js">
<head>
{{ partial "head.html" . }}
<title>{{ if .IsHome }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{ . }} | {{ end }}{{ .Site.Title }}{{ end }}</title>
</head>
<body class="td-{{ .Kind }}">
<header>
{{ partial "navbar.html" . }}
</header>
<div class="container-fluid td-outer">
<div class="td-main">
<div class="row flex-xl-nowrap">
<div class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
{{ partial "sidebar.html" . }}
</div>
<div class="d-none d-xl-block col-xl-2 td-toc d-print-none">
{{ partial "toc.html" . }}
</div>
<main class="col-12 col-md-9 col-xl-8 pl-md-5" role="main">
{{ if not .Site.Params.ui.breadcrumb_disable }}{{ partial "breadcrumb.html" . }}{{ end }}
{{ block "main" . }}{{ end }}
</main>
</div>
</div>
{{ partial "footer.html" . }}
</div>
{{ partial "scripts.html" . }}
</body>
</html>
Loading

0 comments on commit ca9851e

Please sign in to comment.