From 5253cc24187bba0dd134882ccd47a9cee1f408b4 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 12 Apr 2022 16:09:33 +0800 Subject: [PATCH] feat: add toc --- docs/.vuepress/config.js | 1 + .../.vuepress/markdown/createMarkdownArray.js | 8 +- docs/.vuepress/theme/components/Navbar.vue | 4 + docs/.vuepress/theme/components/Sticker.vue | 64 +++++++ docs/.vuepress/theme/components/Toc.vue | 166 ++++++++++++++++++ docs/.vuepress/theme/layouts/Layout.vue | 6 +- docs/.vuepress/theme/styles/index.styl | 5 + docs/.vuepress/theme/styles/palette.styl | 3 +- docs/.vuepress/theme/util/index.js | 21 +++ 9 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 docs/.vuepress/theme/components/Sticker.vue create mode 100644 docs/.vuepress/theme/components/Toc.vue diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index b40a3a54d..1132d850b 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -36,6 +36,7 @@ const config = { docsDir: 'docs', editLinks: true, editLinkText: '帮助我们改善此页面!', + lastUpdated: '上次更新', // smoothScroll: true, algolia: { apiKey: '2fdcc4e76c8e260671ad70065e60b2e7', diff --git a/docs/.vuepress/markdown/createMarkdownArray.js b/docs/.vuepress/markdown/createMarkdownArray.js index f92dcd3fc..efc565cfd 100644 --- a/docs/.vuepress/markdown/createMarkdownArray.js +++ b/docs/.vuepress/markdown/createMarkdownArray.js @@ -45,15 +45,13 @@ function createMarkdownArray(contents = [], childrenName = 'children') { } } - function removeParent(childs = []) { + // 移除最后一项 parent 节点,防止循环引用报错 + (function removeParent(childs = []) { childs.forEach(child => { if (child.parent) delete child.parent if (child[childrenName]) removeParent(child[childrenName]) }) - } - - // 移除最后一项 parent 节点,防止循环引用报错 - removeParent(markdownArray[markdownArray.length - 1][childrenName]) + })(markdownArray[markdownArray.length - 1][childrenName]) return markdownArray } diff --git a/docs/.vuepress/theme/components/Navbar.vue b/docs/.vuepress/theme/components/Navbar.vue index d7e8ae840..2f512f510 100644 --- a/docs/.vuepress/theme/components/Navbar.vue +++ b/docs/.vuepress/theme/components/Navbar.vue @@ -121,6 +121,7 @@ export default { this.mainNavBar = document.querySelector('.main-navbar') this.subNavBar = document.querySelector('.sub-navbar') this.pageContainer = document.querySelector('.page') + this.vuepressToc = document.querySelector('.vuepress-toc') this.navbarHeight = this.navbar.clientHeight this.subNavBarHeight = this.subNavBar.clientHeight this.mainNavBarHeight = this.mainNavBar.clientHeight @@ -134,6 +135,7 @@ export default { window.removeEventListener('scroll', this.onWindowScroll) this.fixedNavbar = false this.sideBar && this.sideBar.removeAttribute('style') + this.vuepressToc && this.vuepressToc.removeAttribute('style') this.navbar && this.navbar.removeAttribute('style') this.pageContainer && this.pageContainer.removeAttribute('style') }, @@ -144,6 +146,7 @@ export default { let sideTop = this.navbarHeight - scrollTop sideTop <= this.subNavBarHeight && (sideTop = this.subNavBarHeight) this.sideBar && (this.sideBar.style.top = `${sideTop + 1}px`) + this.vuepressToc && (this.vuepressToc.style.top = `${sideTop + 1}px`) } if (scrollTop >= this.mainNavBarHeight) { @@ -182,6 +185,7 @@ export default { this.navbarHeight = this.navbar.clientHeight this.subNavBarHeight = this.subNavBar.clientHeight this.sideBar.style.top = `${this.navbarHeight + 1}px` + this.vuepressToc.style.top = `${this.navbarHeight + 1}px` }) } } diff --git a/docs/.vuepress/theme/components/Sticker.vue b/docs/.vuepress/theme/components/Sticker.vue new file mode 100644 index 000000000..e1ac95104 --- /dev/null +++ b/docs/.vuepress/theme/components/Sticker.vue @@ -0,0 +1,64 @@ + + + + + diff --git a/docs/.vuepress/theme/components/Toc.vue b/docs/.vuepress/theme/components/Toc.vue new file mode 100644 index 000000000..b8e6f28ff --- /dev/null +++ b/docs/.vuepress/theme/components/Toc.vue @@ -0,0 +1,166 @@ + + + + + diff --git a/docs/.vuepress/theme/layouts/Layout.vue b/docs/.vuepress/theme/layouts/Layout.vue index b2e594975..687a07186 100644 --- a/docs/.vuepress/theme/layouts/Layout.vue +++ b/docs/.vuepress/theme/layouts/Layout.vue @@ -43,6 +43,8 @@