// window.addEventListener("popstate", () => {
// //const link = location.pathname.replace(/^.*[\\/]/, ""); // get filename only
// loadContent(location.href)
// })
async function loadContent(url) {
console.log("gonna load", url)
// check exact same url as current
if (url.replace(/\/$/, "") === location.href) {
console.log("do nothing")
return
}
function getBetween(s, l, r) {
return s.substring(s.indexOf(l) + l.length, s.indexOf(r))
}
const resp = await fetch(url)
if (!resp.ok) return
let s = await resp.text()
//console.log(newContent);
//console.log(newContent.split('', 2))
const content = getBetween(s, "", "")
const title = getBetween(s, "", "")
console.log("got", title)
//let newContent = newContent.split('', 2)[1].split('',1)[0]
//better is to just replace the body content (do a split with a specific separator tag!)
document.body.innerHTML = content
document.title = title
window.scrollTo(0, 0)
}
async function nav(e, aEl) {
// e = e || window.event
// if (!e) return
// let href = aEl.href
// if (href.indexOf(location.origin) === -1) return
// e.preventDefault()
// history.pushState(null, null, href)
// loadContent(href)
}
function toggleThemePreference() {
let theme = localStorage.theme || "light"
const toggled = theme === "dark" ? "light" : "dark"
localStorage.theme = toggled
if (toggled === "dark") {
document.documentElement.classList.add("dark")
} else {
document.documentElement.classList.remove("dark")
}
}
function copyCode(target) {
target.querySelector("textarea").value = target.querySelector(
"code"
).innerText
target.querySelector("textarea").select()
document.execCommand("copy")
console.log("copied", target)
window.getSelection()?.removeAllRanges()
target.querySelector("span").innerText = "Copied ✓"
setTimeout(() => {
target.querySelector("span").innerText = "Copy Code"
}, 1000)
}
/*
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
*/
function previewImage(src, caption) {
console.log("src", src)
// create a new modal if not already exists
let modal = document.getElementById("modal")
if (!modal) {
modal = document.createElement("div")
modal.id = "modal"
modal.className = "modal"
modal.innerHTML = `
×
`
document.body.appendChild(modal)
}
modal.style.display = "block"
// assign the image to src
modal.querySelector("#img01").src = src
// Get the caption text and set it
if (caption) {
let captionEl = modal.querySelector("#caption")
captionEl.innerHTML = caption
}
// display modal
modal.style.display = "block"
}