From eab0632d5c94fc3cbac52e4b1daff349bf53258e Mon Sep 17 00:00:00 2001 From: Mat Manna <91392083+matmanna@users.noreply.github.com> Date: Sun, 23 Nov 2025 23:45:44 +0000 Subject: [PATCH] :zap: Make small js files inline --- _includes/scrollspy.html | 64 +++++++++++++++++++++++++++++++++++++ _includes/toggle-theme.html | 36 +++++++++++++++++++++ _layouts/default.html | 2 +- _layouts/page.html | 2 +- _layouts/post.html | 2 +- assets/js/scrollspy.js | 62 ----------------------------------- tailwind.config.js | 2 +- 7 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 _includes/scrollspy.html create mode 100644 _includes/toggle-theme.html diff --git a/_includes/scrollspy.html b/_includes/scrollspy.html new file mode 100644 index 0000000..6dc546e --- /dev/null +++ b/_includes/scrollspy.html @@ -0,0 +1,64 @@ + \ No newline at end of file diff --git a/_includes/toggle-theme.html b/_includes/toggle-theme.html new file mode 100644 index 0000000..8101335 --- /dev/null +++ b/_includes/toggle-theme.html @@ -0,0 +1,36 @@ + \ No newline at end of file diff --git a/_layouts/default.html b/_layouts/default.html index 7b5b493..d14ff1b 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -23,7 +23,7 @@ {% include foot.html %} - + {% include toggle-theme.html %} \ No newline at end of file diff --git a/_layouts/page.html b/_layouts/page.html index b08be64..8653a97 100644 --- a/_layouts/page.html +++ b/_layouts/page.html @@ -15,4 +15,4 @@ layout: default {{ content }} - \ No newline at end of file +{% include scrollspy.html %} \ No newline at end of file diff --git a/_layouts/post.html b/_layouts/post.html index 63ea398..6447111 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -15,4 +15,4 @@ layout: default {{ content }} - \ No newline at end of file +{% include scrollspy.html %} \ No newline at end of file diff --git a/assets/js/scrollspy.js b/assets/js/scrollspy.js index cff0d84..e69de29 100644 --- a/assets/js/scrollspy.js +++ b/assets/js/scrollspy.js @@ -1,62 +0,0 @@ -/** - * TOC ScrollSpy - * Highlights the current section in the table of contents - */ -function initTocScrollSpy() { - const headings = document.querySelector('.content').querySelectorAll('h2[id], h3[id], h4[id], h5[id], h6[id]'); - const tocLinks = document.querySelectorAll('.toc-link'); - - if (headings.length === 0 || tocLinks.length === 0) return; - - const idToTocLink = {}; - tocLinks.forEach(link => { - const href = link.getAttribute('href'); - if (href && href.startsWith('#')) { - idToTocLink[href.substring(1)] = link; - } - }); - - const headingPositions = Array.from(headings).map(heading => { - return { - id: heading.id, - top: heading.getBoundingClientRect().top + window.pageYOffset - 100 - }; - }); - - function updateToc() { - const scrollPosition = window.pageYOffset; - - let currentHeadingIndex = headingPositions.findIndex(heading => - heading.top > scrollPosition - ) - 1; - - if (currentHeadingIndex === -2) { - currentHeadingIndex = headingPositions.length - 1; - } else if (currentHeadingIndex < 0) { - currentHeadingIndex = 0; - } - - tocLinks.forEach(link => link.classList.remove('font-bold', 'text-primary-800', 'dark:text-primary-300')); - - const currentId = headingPositions[currentHeadingIndex]?.id; - if (currentId && idToTocLink[currentId]) { - idToTocLink[currentId].classList.add('font-bold', 'text-primary-800', 'dark:text-primary-300'); - } - } - - updateToc(); - - let ticking = false; - window.addEventListener('scroll', function () { - if (!ticking) { - window.requestAnimationFrame(function () { - updateToc(); - ticking = false; - }); - ticking = true; - } - }); -} - -document.addEventListener('DOMContentLoaded', initTocScrollSpy); - diff --git a/tailwind.config.js b/tailwind.config.js index be7116f..f5738a7 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -6,7 +6,7 @@ module.exports = { content: [ './_includes/**/*.html', './_layouts/**/*.html', - './_posts/*.md', + './_pages/**/*.html', './*.html', ], theme: { -- 2.51.2