Skip to content

Commit

Permalink
Add sidebar menu to rules page (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
btea and bluwy authored Aug 25, 2023
1 parent cf64c1e commit c5e04de
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
22 changes: 16 additions & 6 deletions site/rules.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script type="module" src="./src/rules.js"></script>
</head>
<body tabindex="-1">
<main class="docs w-full max-w-3xl mx-auto p-4 pt-10">
<main class="w-full mx-auto p-4 pt-10">
<div style="display: none">
<svg>
<symbol id="autolink-icon" width="24" height="24" fill="currentColor">
Expand All @@ -19,7 +19,7 @@
</symbol>
</svg>
</div>
<header class="absolute top-0 left-0 w-full z-10 px-4">
<header class="absolute xl:fixed top-0 left-0 w-full z-10 px-4">
<nav class="bookmark-nav flex gap-4 justify-end max-w-3xl mx-auto">
<a class="bookmark decoration-none" href="/" title="Go to main page">
<img
Expand All @@ -43,10 +43,20 @@
</a>
</nav>
</header>
<!-- rules.md -->
<footer class="my-16">
<a class="anchor-link" href="/"> ➥ Back to main page </a>
</footer>
<div class="flex justify-center">
<div class="docs max-w-3xl xl:ml-32">
<!-- rules.md -->
<footer class="my-16">
<a class="anchor-link" href="/"> ➥ Back to main page </a>
</footer>
</div>
<div class="relative hidden xl:block">
<aside class="rules-aside-toc pt-18 sticky top-10">
<p class="font-semibold mt-0 mb-2">On this page</p>
<!-- rules.md (aside) -->
</aside>
</div>
</div>
</main>
</body>
</html>
24 changes: 22 additions & 2 deletions site/scripts/vitePluginMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ export function markdown() {
async transformIndexHtml(html) {
if (html.includes('<!-- rules.md -->')) {
const markdown = await fs.readFile(rulesPath, 'utf-8')
const rules = (await markdownProcessor.process(markdown)).toString()
return html.replace('<!-- rules.md -->', rules)
const rulesHtml = (await markdownProcessor.process(markdown)).toString()
const tocHtml = getTocHtml(rulesHtml)
return html
.replace('<!-- rules.md -->', rulesHtml)
.replace('<!-- rules.md (aside) -->', tocHtml)
}
},
handleHotUpdate(ctx) {
Expand All @@ -74,3 +77,20 @@ export function markdown() {
}
}
}

/**
* @param {string} rulesHtml
*/
function getTocHtml(rulesHtml) {
const idMatch = /<h2\s*id="(.*?)">/g
/** @type {string[]} */
const links = []
let m
while ((m = idMatch.exec(rulesHtml))) {
links.push(m[1])
}
return `\
<ul>
${links.map((link) => `<li><a href="#${link}">${link}</a></li>`).join('\n')}
</ul>`
}
40 changes: 40 additions & 0 deletions site/src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ body {
.docs li {
line-height: 1.5;
opacity: 0.8;
list-style: none;
}

p {
Expand Down Expand Up @@ -202,6 +203,33 @@ button {
);
}

.rules-aside-toc {
padding-left: 2rem;
}

.rules-aside-toc ul {
margin: 0;
padding: 0;
}

.rules-aside-toc li {
list-style: none;
line-height: 1.5;
margin-bottom: 0.2rem;
opacity: 0.8;
}

.rules-aside-toc a {
text-decoration: none;
transition: color cubic-bezier(0.4, 0, 0.2, 1) 150ms;
}

.rules-aside-toc a.active,
.rules-aside-toc a:hover,
.rules-aside-toc a:focus {
color: #cf7522;
}

@media (prefers-color-scheme: dark) {
html,
body {
Expand Down Expand Up @@ -251,6 +279,12 @@ button {
rgb(209, 213, 219) 100%
);
}

.rules-aside-toc a.active,
.rules-aside-toc a:hover,
.rules-aside-toc a:focus {
color: #e6ab73;
}
}

@media (min-width: 456px) {
Expand All @@ -268,3 +302,9 @@ button {
height: 5rem;
}
}

@media screen and (max-width: 1290px) {
.aside-menu-container {
display: none;
}
}
6 changes: 4 additions & 2 deletions site/src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ window.addEventListener('hashchange', (e) => {
* @param {boolean} active
*/
function setHashActive(hash, active) {
console.log('as')
hash = hash.replace('#', '')
if (!hash) return
const heading = document.getElementById(hash)
if (!heading) return
const tocLink = document.querySelector(`.rules-aside-toc [href="#${hash}"]`)
if (!heading || !tocLink) return
if (active) {
heading.classList.add('active')
tocLink.classList.add('active')
} else {
heading.classList.remove('active')
tocLink.classList.remove('active')
}
}

0 comments on commit c5e04de

Please sign in to comment.