Added and internalized Pico theme for customization

This commit is contained in:
Aron Petau 2025-04-29 20:02:54 +02:00
parent 8e7f72ce8b
commit 8c90bb2dfc
71 changed files with 4587 additions and 0 deletions

View file

@ -0,0 +1,17 @@
function enableCopy() {
document.querySelectorAll('pre:not(.mermaid)').forEach(node => {
let copyBtn = document.createElement("span");
copyBtn.classList.add('copybutton');
copyBtn.classList.add('icon');
copyBtn.classList.add('icon-copy');
node.appendChild(copyBtn);
copyBtn.addEventListener("click", async () => {
if (navigator.clipboard) {
let text = node.querySelectorAll('code')[0].innerText;
await navigator.clipboard.writeText(text);
copyBtn.classList.add('clicked');
}
setTimeout(() => copyBtn.classList.remove('clicked'), 600);
})
})
}

View file

@ -0,0 +1,105 @@
const cIcon = document.getElementById('cIcon');
mmdElements = document.getElementsByClassName('mermaid');
const mmdHTML = [];
for (let i = 0; i < mmdElements.length; i++) {
mmdHTML[i] = mmdElements[i].innerHTML;
}
function mermaidRender(theme) {
if ( theme == 'dark' ) {
initOptions = {
startOnLoad: false,
theme: 'dark'
}
}
else {
initOptions = {
startOnLoad: false,
theme: 'neutral'
}
}
for (let i = 0; i < mmdElements.length; i++) {
delete mmdElements[i].dataset.processed;
mmdElements[i].innerHTML = mmdHTML[i];
}
mermaid.initialize(initOptions);
mermaid.run();
}
function giscusRender(theme) {
if (document.head.dataset['commentsEnabled'] == 'true'){
baseUrl = document.head.dataset['baseUrl'];
themes = {
"dark": `${baseUrl}css/gs_dark.css`,
"light":`${baseUrl}css/gs_light.css`
}
giscus = document.getElementById('giscusScript');
if (giscus){
giscus.remove();
}
let js = document.createElement('script');
js.setAttribute("id", 'giscusScript');
js.setAttribute("src", document.head.dataset['giscusSrc']);
js.setAttribute("data-repo", document.head.dataset['giscusRepo']);
js.setAttribute("data-repo-id", document.head.dataset['giscusRepoId']);
js.setAttribute("data-category", document.head.dataset['giscusCategory']);
js.setAttribute("data-category-id", document.head.dataset['giscusCategoryId']);
js.setAttribute("data-mapping", document.head.dataset['giscusMapping']);
js.setAttribute("data-strict",document.head.dataset['giscusStrict']);
js.setAttribute("data-reactions-enabled",document.head.dataset['giscusReactionsEnabled']);
js.setAttribute("data-emit-metadata", document.head.dataset['giscusEmitMetadata']);
js.setAttribute("data-input-position", document.head.dataset['giscusInputPosition']);
js.setAttribute("data-theme", themes[theme]);
js.setAttribute("data-lang", document.head.dataset['giscusLang']);
js.setAttribute("crossorigin", document.head.dataset['giscusCrossorigin']);
js.setAttribute("nonce", document.head.dataset['giscusNonce']);
js.async = true;
commentsBase = document.getElementById('giscusWidget').appendChild(js);
}
}
function setStartTheme(){
let targetColorTheme = '';
let currentTheme = localStorage.getItem('color-theme');
if (currentTheme != null) {
targetColorTheme = currentTheme
} else {
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
if (darkThemeMq.matches) {
targetColorTheme = 'dark';
// opposite = 'light';
} else {
targetColorTheme = 'light';
// opposite = 'dark';
}
}
cIcon.classList.remove('icon-'+currentTheme);
cIcon.classList.add('icon-'+targetColorTheme);
document.head.dataset.colorTheme = targetColorTheme;
document.documentElement.setAttribute('color-theme', targetColorTheme);
localStorage.setItem('color-theme',targetColorTheme);
giscusRender(targetColorTheme);
mermaidRender(targetColorTheme);
}
function switchTheme() {
currentTheme = document.documentElement.getAttribute('color-theme')
if (currentTheme == 'dark') {
targetColorTheme = 'light';
}
if ( currentTheme == 'light') {
targetColorTheme = 'dark';
}
cIcon.classList.remove('icon-'+currentTheme);
cIcon.classList.add('icon-'+targetColorTheme);
document.documentElement.setAttribute('color-theme', targetColorTheme);
document.head.dataset.colorTheme = targetColorTheme;
localStorage.setItem('color-theme',targetColorTheme);
giscusRender(targetColorTheme)
mermaidRender(targetColorTheme)
}
document.getElementById("cIcon").addEventListener("click", switchTheme);

View file

@ -0,0 +1,8 @@
document.addEventListener("DOMContentLoaded", function(event){
setStartTheme();
enableCopy();
if (document.head.dataset['buildSearchIndex'] == "true") {
initSearch();
}
});

1759
themes/pico/static/js/mermaid.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,202 @@
function debounce(func, wait) {
var timeout;
return function () {
var context = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
func.apply(context, args);
}, wait);
};
}
function makeTeaser(body, terms) {
var TERM_WEIGHT = 40;
var NORMAL_WORD_WEIGHT = 2;
var FIRST_WORD_WEIGHT = 8;
var TEASER_MAX_WORDS = 20;
var stemmedTerms = terms.map(function (w) {
return elasticlunr.stemmer(w.toLowerCase());
});
var termFound = false;
var index = 0;
var weighted = []; // contains elements of ["word", weight, index_in_document]
// split in sentences, then words
var sentences = body.toLowerCase().split(". ");
for (var i in sentences) {
var words = sentences[i].split(" ");
var value = FIRST_WORD_WEIGHT;
for (var j in words) {
var word = words[j];
if (word.length > 0) {
for (var k in stemmedTerms) {
if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
value = TERM_WEIGHT;
termFound = true;
}
}
weighted.push([word, value, index]);
value = NORMAL_WORD_WEIGHT;
}
index += word.length;
index += 1; // ' ' or '.' if last word in sentence
}
index += 1; // because we split at a two-char boundary '. '
}
if (weighted.length === 0) {
return body;
}
var windowWeights = [];
var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
// We add a window with all the weights first
var curSum = 0;
for (var i = 0; i < windowSize; i++) {
curSum += weighted[i][1];
}
windowWeights.push(curSum);
for (var i = 0; i < weighted.length - windowSize; i++) {
curSum -= weighted[i][1];
curSum += weighted[i + windowSize][1];
windowWeights.push(curSum);
}
// If we didn't find the term, just pick the first window
var maxSumIndex = 0;
if (termFound) {
var maxFound = 0;
// backwards
for (var i = windowWeights.length - 1; i >= 0; i--) {
if (windowWeights[i] > maxFound) {
maxFound = windowWeights[i];
maxSumIndex = i;
}
}
}
var teaser = [];
var startIndex = weighted[maxSumIndex][2];
for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
var word = weighted[i];
if (startIndex < word[2]) {
// missing text from index to start of `word`
teaser.push(body.substring(startIndex, word[2]));
startIndex = word[2];
}
// add <em/> around search terms
if (word[1] === TERM_WEIGHT) {
teaser.push("<b>");
}
startIndex = word[2] + word[0].length;
teaser.push(body.substring(word[2], startIndex));
if (word[1] === TERM_WEIGHT) {
teaser.push("</b>");
}
}
teaser.push("…");
return teaser.join("");
}
function formatSearchResultItem(item, terms) {
return '<div class="search-results__item">'
+ `<h4><a href="${item.ref}">${item.doc.title}</a></h4>`
+ `<div class="teaser">${makeTeaser(item.doc.body, terms)}</div>`
+ '</div>';
}
function initSearch() {
var $searchInput = document.getElementById("searchInput");
var $searchResults = document.getElementById('sResults');
var $searchResultsItems = document.getElementById('sResultsUL')
var MAX_ITEMS = 5;
var options = {
bool: "AND",
fields: {
title: {boost: 2},
body: {boost: 1},
}
};
var currentTerm = "";
var index;
var initIndex = async function () {
if (index === undefined) {
let $baseUrl = document.head.dataset['baseUrl']
index = fetch($baseUrl + "search_index.en.json")
.then(
async function(response) {
return await elasticlunr.Index.load(await response.json());
}
);
}
let res = await index;
return res;
}
$searchInput.addEventListener("keyup", debounce(async function() {
if ($searchInput.value != '') {
$searchInput.classList.add("textIn")
} else {
$searchInput.classList.remove("textIn")
}
var term = $searchInput.value.trim();
if (term === currentTerm) {
return;
}
$searchResults.style.display = term === "" ? "none" : "block";
$searchResultsItems.innerHTML = "";
$searchResults.style.top = `${document.documentElement.scrollTop}px`;
$searchResultsItems.style.display="block";
$searchResultsItems.style.transition="opacity 3s";
currentTerm = term;
if (term === "") {
return;
}
var results = (await initIndex()).search(term, options);
if (results.length === 0) {
$searchResults.style.display = "none";
return;
}
var item = document.createElement("li");
clSearch = `<span class="closeBtn icon icon-close" onclick="closeSearchResults();"></span>`
item.innerHTML = clSearch;
$searchResultsItems.appendChild(item);
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
var item = document.createElement("li");
item.innerHTML = formatSearchResultItem(results[i], term.split(" "));
$searchResultsItems.appendChild(item);
}
}, 150));
window.addEventListener('click', function(e) {
if ($searchResults.style.display == "block" && !$searchResults.contains(e.target)) {
$searchResults.style.display = "block";
}
});
}
function closeSearchResults() {
document.getElementById("sResults").style.display="none";
document.getElementById("searchInput").value="";
document.getElementById("searchInput").classList.remove("textIn");
initSearch;
}