Skip to content

Commit d624af2

Browse files
Live update side nav
1 parent 09528b7 commit d624af2

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ NODE_VERSION = "15"
33

44
[build]
55
publish = "dist/"
6-
command = "npx pnpm i --store=node_modules/.pnpm-store && npx pnpm run build"
6+
command = "npm run build"
77

88
[[redirects]]
99
from = "/*"

src/components/Title.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="flex flex-col mb-16 title items-center content-center">
3-
<h1 class="font-serif text-center title__header">
3+
<h1 class="font-serif text-center title__header" :id="slugify(title)">
44
<slot>{{ title }}</slot>
55
</h1>
66
<p class="text-center title__description text-gray-50">
@@ -10,6 +10,7 @@
1010
</template>
1111

1212
<script>
13+
import { slugify } from '../markdown/slugify'
1314
export default {
1415
props: {
1516
title: {
@@ -20,6 +21,9 @@ export default {
2021
required: false,
2122
type: String,
2223
}
24+
},
25+
methods: {
26+
slugify
2327
}
2428
}
2529

src/layouts/article.vue

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
<span
2121
v-if="current == h.hash"
2222
class="absolute rounded-full bg-pink-900"
23-
:class="{ 'h-4 w-4': h.type == 'H2', 'h-3 w-3': h.type == 'H3', 'h-2 w-2': h.type == 'H4' }"
23+
:class="{ 'h-4 w-4': h.type == 'H2' || h.type == 'H1', 'h-3 w-3': h.type == 'H3', 'h-2 w-2': h.type == 'H4' }"
2424
></span>
2525
<span
2626
class="relative block rounded-full"
2727
:class="{
28-
'w-2 h-2': h.type == 'H2',
28+
'w-2 h-2': h.type == 'H2' || h.type == 'H1',
2929
'w-1.5 h-1.5': h.type == 'H3',
3030
'w-1 h-1': h.type == 'H4',
3131
'bg-pink-600': current == h.hash,
@@ -66,22 +66,43 @@ const root = ref(null);
6666
6767
const parseHeading = (x) => {
6868
const title = x.innerText.replace('#', '').trim();
69-
const obj = { title, hash: x.children[0]?.hash ?? title, type: x.tagName };
70-
console.dir(x);
69+
const obj = { title, hash: x.children[0]?.hash ?? '#', type: x.tagName, pos: x.getBoundingClientRect().top };
7170
console.log(obj);
7271
return obj;
7372
}
7473
74+
// setTimeout(() => {
75+
// const element = document.querySelector(to.hash);
76+
// const offset = 40;
77+
// const bodyRect = document.body.getBoundingClientRect().top;
78+
// const elementRect = element.getBoundingClientRect().top;
79+
// const elementPosition = elementRect - bodyRect;
80+
// const offsetPosition = elementPosition - offset;
81+
82+
// window.scrollTo({
83+
// top: offsetPosition,
84+
// behavior: 'smooth'
85+
// });
86+
// }, 0);
87+
7588
const headings = computed(() => sections.filter(x => x.closest('.slides') == null).map(x => parseHeading(x)));
7689
7790
const jumps = computed(() => sections.map(x => x));
7891
7992
onMounted(() => {
8093
94+
document.addEventListener('scroll', function(e) {
95+
for (const heading of headings.value) {
96+
if (heading.pos < (window.scrollY + (window.innerHeight / 3))) {
97+
current = heading.hash;
98+
}
99+
}
100+
});
101+
81102
const observer = new MutationObserver((mutationsList, observer) => {
82103
for (const mutation of mutationsList) {
83104
if (mutation.type === 'childList') {
84-
sections = [...root.value.querySelectorAll('h2, h3, h4, .slides')]
105+
sections = [...root.value.querySelectorAll('h1, h2, h3, h4, .slides')]
85106
}
86107
}
87108
});
@@ -92,6 +113,7 @@ onMounted(() => {
92113
setTimeout(() => {
93114
observer.disconnect();
94115
}, 2000)
116+
95117
})
96118
97119

src/main.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ const router = createRouter({
1717
if (savedPosition) {
1818
return savedPosition;
1919
} else if (to.hash) {
20-
// setTimeout(() => {
21-
// const element = document.querySelector(to.hash);
22-
// const offset = 40;
23-
// const bodyRect = document.body.getBoundingClientRect().top;
24-
// const elementRect = element.getBoundingClientRect().top;
25-
// const elementPosition = elementRect - bodyRect;
26-
// const offsetPosition = elementPosition - offset;
27-
28-
// window.scrollTo({
29-
// top: offsetPosition,
30-
// behavior: 'smooth'
31-
// });
32-
// }, 0);
3320
return { el: to.hash, behavior: 'smooth' };
3421
} else {
3522
return { left: 0, top: 0, behavior: 'auto' };

0 commit comments

Comments
 (0)