Skip to content

Commit

Permalink
feat: post sidebar support table of content
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Oct 15, 2022
1 parent e415357 commit bf574b7
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"@iconify/iconify": "^3.0.0",
"alpinejs": "^3.10.3"
"alpinejs": "^3.10.3",
"tocbot": "^4.18.2"
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "./styles/tailwind.css";
import "./styles/main.css";
import "@purge-icons/generated";

import Alpine from "alpinejs";
import * as tocbot from "tocbot";

window.Alpine = Alpine;

Expand All @@ -18,3 +18,18 @@ const onScroll = () => {
};

window.addEventListener("scroll", onScroll);

export function generateToc() {
tocbot.init({
tocSelector: ".toc",
contentSelector: "#content",
headingSelector: "h1, h2, h3, h4",
extraListClasses: "space-y-1",
extraLinkClasses: 'group flex items-center justify-between rounded py-1 px-1.5 transition-all hover:bg-gray-100 text-sm opacity-80',
activeLinkClass: "is-active-link bg-gray-100",
collapseDepth: 6,
headingsOffset: 100,
scrollSmooth: true,
scrollSmoothOffset: -100,
});
}
4 changes: 4 additions & 0 deletions src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ body {
.menu-sticky .menu-dropdown li a {
@apply text-gray-600 hover:text-blue-600;
}

.toc-list-item > .toc-list {
@apply my-3 ml-3 space-y-1 border-l pl-2;
}
2 changes: 1 addition & 1 deletion tailwind.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./templates/**/*.html"],
content: ["./templates/**/*.html","./src/main.ts"],
theme: {
extend: {},
container: {
Expand Down
6 changes: 3 additions & 3 deletions templates/assets/dist/main.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/assets/dist/style.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/category.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html
xmlns:th="https://www.thymeleaf.org"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, contentClass = null)"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, sidebar = null, contentClass = null)"
>
<th:block th:fragment="content">
<th:block th:replace="modules/category-filter" />
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html
xmlns:th="https://www.thymeleaf.org"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, contentClass = null)"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, sidebar = null, contentClass = null)"
>
<th:block th:fragment="content">
<th:block th:replace="modules/category-filter" />
Expand Down
9 changes: 7 additions & 2 deletions templates/modules/layout.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" th:fragment="html (header,content,head,footer,contentClass)">
<html lang="en" th:fragment="html (header,content,head,footer,sidebar,contentClass)">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
Expand All @@ -24,7 +24,12 @@
<div class="col-span-4 sm:col-span-3">
<th:block th:replace="${content}" />
</div>
<th:block th:replace="modules/sidebar" />
<th:block th:if="${sidebar != null}">
<th:block th:replace="${sidebar}" />
</th:block>
<th:block th:if="${sidebar == null}">
<th:block th:replace="modules/sidebar :: sidebar(prepend = null)" />
</th:block>
</section>
<th:block th:replace="modules/footer" />
<th:block th:if="${footer != null}">
Expand Down
5 changes: 4 additions & 1 deletion templates/modules/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<aside class="col-span-1 hidden h-full flex-col gap-6 sm:flex">
<aside th:fragment="sidebar (prepend)" class="col-span-1 hidden h-full flex-col gap-6 sm:flex">
<th:block th:with="widgets = ${#strings.listSplit(theme.config.sidebar.widgets,',')} ">
<th:block th:if="${prepend != null}">
<th:block th:replace="${prepend}" />
</th:block>
<th:block th:each="widget : ${widgets}">
<th:block th:replace="'modules/widgets/'+${widget}" />
</th:block>
Expand Down
2 changes: 1 addition & 1 deletion templates/page.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html
xmlns:th="https://www.thymeleaf.org"
th:replace="modules/layout :: html(header = ~{::header}, content = ~{::content}, head = null, footer = ~{::footer}, contentClass = ${theme.config.layout.content_header} and ${not #strings.isEmpty(singlePage.spec.cover)} ? '!-mt-20' : '')"
th:replace="modules/layout :: html(header = ~{::header}, content = ~{::content}, head = null, footer = ~{::footer}, sidebar = null, contentClass = ${theme.config.layout.content_header} and ${not #strings.isEmpty(singlePage.spec.cover)} ? '!-mt-20' : '')"
>
<th:block th:fragment="header">
<th:block
Expand Down
21 changes: 20 additions & 1 deletion templates/post.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html
xmlns:th="https://www.thymeleaf.org"
th:replace="modules/layout :: html(header = ~{::header}, content = ~{::content}, head = null, footer = ~{::footer}, contentClass = ${theme.config.layout.content_header} and ${not #strings.isEmpty(post.spec.cover)} ? '!-mt-20' : '')"
th:replace="modules/layout :: html(header = ~{::header}, content = ~{::content}, head = null, footer = ~{::footer}, sidebar = ~{::sidebar}, contentClass = ${theme.config.layout.content_header} and ${not #strings.isEmpty(post.spec.cover)} ? '!-mt-20' : '')"
>
<th:block th:fragment="header">
<th:block
Expand All @@ -18,6 +18,25 @@
<th:block th:include="modules/libs/lightgallery :: import"></th:block>
<th:block th:include="modules/libs/lightgallery :: script"></th:block>
</th:block>

<script>
console.log(main.generateToc());
main.generateToc();
</script>
</th:block>
<th:block th:fragment="sidebar_prepend">
<div
class="w-full cursor-pointer overflow-hidden rounded-xl bg-white p-3 shadow transition-all duration-500 hover:shadow-md"
>
<h2 class="inline-flex items-center gap-2 text-base">
<span data-icon="tabler:list" class="iconify text-lg"></span>
目录
</h2>
<div class="toc mt-2"></div>
</div>
</th:block>
<th:block th:fragment="sidebar">
<th:block th:replace="modules/sidebar :: sidebar(prepend = ~{::sidebar_prepend})"></th:block>
</th:block>
<th:block th:fragment="content">
<div class="rounded-xl bg-white p-4">
Expand Down
2 changes: 1 addition & 1 deletion templates/tag.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html
xmlns:th="https://www.thymeleaf.org"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, contentClass = null)"
th:replace="modules/layout :: html(header = null, content = ~{::content}, head = null, footer = null, sidebar = null, contentClass = null)"
>
<th:block th:fragment="content">
<div class="flex text-gray-900">
Expand Down

0 comments on commit bf574b7

Please sign in to comment.