Skip to content

Commit f5810a2

Browse files
Feat: Random Page and Scroll to Top
- added 'Scroll to top' and 'Random page' buttons to site footer - Updated credits Credit: [Eilleen's (online!) Everything Notebook](https://quartz.eilleeenz.com/)
1 parent d438fac commit f5810a2

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Diff for: content/credits-and-readmes/index.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ aliases:
1717
## [ITS Theme for Obsidian](https://publish.obsidian.md/slrvb-docs/ITS+Theme/ITS+Theme)
1818

1919
- Thanks to [SIRvb](https://github.com/SlRvb) for providing the ITS Theme's [Image Adjustments](https://github.com/SlRvb/Obsidian--ITS-Theme/blob/main/Snippets/S%20-%20Images%20Adjustments.css) and [Callouts](https://github.com/SlRvb/Obsidian--ITS-Theme/blob/main/Snippets/S%20-%20Callouts.css) CSS snippets used throughout the site.
20-
- [License](https://github.com/SlRvb/Obsidian--ITS-Theme/blob/main/LICENSE)
20+
- [License](https://github.com/SlRvb/Obsidian--ITS-Theme/blob/main/LICENSE)
21+
22+
## [Eilleen's (online!) Everything Notebook](https://quartz.eilleeenz.com/)
23+
24+
- Thanks to [fanteastick](https://github.com/fanteastick) for 'Eilleen's (online!) Everything Notebook', from whom the code for the site footer's 'Scroll to top' and 'Random page' buttons was borrowed.
25+
- **Github:** [fanteastick/quartz-test](https://github.com/fanteastick/quartz-test)
26+
- [License](https://github.com/fanteastick/quartz-test/blob/v4/LICENSE.txt)

Diff for: quartz/components/Footer.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } fro
22
import style from "./styles/footer.scss"
33
import { version } from "../../package.json"
44
import { i18n } from "../i18n"
5+
// MMW randomPage script
6+
// @ts-ignore
7+
import script from "./scripts/randomPage.inline"
58

69
interface Options {
710
links: Record<string, string>
@@ -25,10 +28,24 @@ export default ((opts?: Options) => {
2528
</li>
2629
))}
2730
</ul>
31+
<p></p>
32+
<ul>
33+
<li>
34+
<a href="#">
35+
Scroll to top ↑
36+
</a>
37+
</li>
38+
<li>
39+
<a id="random-page-button">
40+
Random Page 🎲
41+
</a>
42+
</li>
43+
</ul>
2844
</footer>
2945
)
3046
}
3147

3248
Footer.css = style
49+
Footer.afterDOMLoaded = script
3350
return Footer
3451
}) satisfies QuartzComponentConstructor

Diff for: quartz/components/scripts/randomPage.inline.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { FullSlug, getFullSlug, pathToRoot, simplifySlug } from "../../util/path"
2+
3+
function getRandomInt(max: number) {
4+
return Math.floor(Math.random() * max);
5+
}
6+
7+
async function navigateToRandomPage() {
8+
const fullSlug = getFullSlug(window)
9+
const data = await fetchData
10+
const allPosts = Object.keys(data).map((slug) => simplifySlug(slug as FullSlug))
11+
// window.location.href = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`
12+
let newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`;
13+
14+
if (newSlug === fullSlug) {
15+
// Generate a new random slug until it's different from the starting fullSlug
16+
do {
17+
newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`;
18+
} while (newSlug === fullSlug);
19+
}
20+
window.location.href = newSlug;
21+
}
22+
23+
document.addEventListener("nav", async (e: unknown) => {
24+
// const slug = (e as CustomEventMap["nav"]).detail.url
25+
const button = document.getElementById("random-page-button")
26+
button?.removeEventListener("click", navigateToRandomPage)
27+
button?.addEventListener("click", navigateToRandomPage)
28+
})

Diff for: quartz/components/styles/footer.scss

+4
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ footer {
1313
margin-top: -1rem;
1414
}
1515
}
16+
17+
#random-page-button:hover{
18+
cursor: pointer;
19+
}

0 commit comments

Comments
 (0)