|
| 1 | +<template> |
| 2 | + <div class="sidebar"> |
| 3 | + <ul> |
| 4 | + <li v-for="(item, i) in sidebarItems"> |
| 5 | + <router-link v-if="item.type === 'page'" :to="item.path"> |
| 6 | + {{ item.title || item.path }} |
| 7 | + </router-link> |
| 8 | + <div v-else-if="item.type === 'heading'" |
| 9 | + class="sidebar-group" |
| 10 | + :class="{ first: i === 0 }"> |
| 11 | + <p class="sidebar-heading">{{ item.title }}</p> |
| 12 | + <ul> |
| 13 | + <li v-for="child in item.children"> |
| 14 | + <router-link v-if="child.type === 'page'" :to="child.path"> |
| 15 | + {{ child.title || child.path }} |
| 16 | + </router-link> |
| 17 | + </li> |
| 18 | + </ul> |
| 19 | + </div> |
| 20 | + </li> |
| 21 | + </ul> |
| 22 | + </div> |
| 23 | +</template> |
| 24 | + |
| 25 | +<script> |
| 26 | +function resolveSidebar (route, site) { |
| 27 | + const { pages, themeConfig } = site |
| 28 | + const sidebarConfig = themeConfig.sidebar |
| 29 | + if (!sidebarConfig) { |
| 30 | + return pages |
| 31 | + } else { |
| 32 | + const matchingConfig = Array.isArray(sidebarConfig) |
| 33 | + ? sidebarConfig |
| 34 | + : resolveMatchingSidebar(route, sidebarConfig) |
| 35 | + return matchingConfig.map(item => resolveItem(item, site.pages)) |
| 36 | + } |
| 37 | +} |
| 38 | +
|
| 39 | +function resolveMatchingSidebar (route, sidebarConfig) { |
| 40 | + for (const base in sidebarConfig) { |
| 41 | + if (ensureEndingSlash(route.path).indexOf(base) === 0) { |
| 42 | + return sidebarConfig[base] |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +
|
| 47 | +function ensureEndingSlash (path) { |
| 48 | + return /(\.html|\/)$/.test(path) |
| 49 | + ? path |
| 50 | + : path + '/' |
| 51 | +} |
| 52 | +
|
| 53 | +function resolveItem (item, pages) { |
| 54 | + if (typeof item === 'string') { |
| 55 | + return resolvePage(pages, item) |
| 56 | + } else if (Array.isArray(item)) { |
| 57 | + return Object.assign(resolvePage(pages, item[0]), { |
| 58 | + title: item[1] |
| 59 | + }) |
| 60 | + } else { |
| 61 | + const children = item.children || [] |
| 62 | + return { |
| 63 | + type: 'heading', |
| 64 | + title: item.title, |
| 65 | + children: children.map(child => resolveItem(child, pages)), |
| 66 | + collapsable: children.length && item.collapsable |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +function resolvePage (pages, rawPath) { |
| 72 | + const path = normalize(rawPath) |
| 73 | + for (let i = 0; i < pages.length; i++) { |
| 74 | + if (normalize(pages[i].path) === path) { |
| 75 | + return Object.assign({}, pages[i], { |
| 76 | + type: 'page', |
| 77 | + path: ensureExt(rawPath) |
| 78 | + }) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +
|
| 83 | +const hashRE = /#.*$/ |
| 84 | +const extRE = /\.(md|html)$/ |
| 85 | +const slashRE = /\/$/ |
| 86 | +
|
| 87 | +function normalize (path) { |
| 88 | + return path |
| 89 | + .replace(hashRE, '') |
| 90 | + .replace(extRE, '') |
| 91 | +} |
| 92 | +
|
| 93 | +function ensureExt (path) { |
| 94 | + if (slashRE.test(path)) { |
| 95 | + return path |
| 96 | + } |
| 97 | + const hashMatch = path.match(hashRE) |
| 98 | + const hash = hashMatch ? hashMatch[0] : '' |
| 99 | + return normalize(path) + '.html' + hash |
| 100 | +} |
| 101 | +
|
| 102 | +export default { |
| 103 | + computed: { |
| 104 | + sidebarItems () { |
| 105 | + return resolveSidebar( |
| 106 | + this.$route, |
| 107 | + this.$site |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +</script> |
| 113 | + |
| 114 | +<style lang="stylus"> |
| 115 | +@import './styles/config.stylus' |
| 116 | +
|
| 117 | +.sidebar |
| 118 | + ul |
| 119 | + padding 0 |
| 120 | + margin 0 |
| 121 | + list-style-type none |
| 122 | + a |
| 123 | + display inline-block |
| 124 | + color $textColor |
| 125 | + border-left 0.25rem solid transparent |
| 126 | + padding 0.25rem |
| 127 | + padding-left 1.25rem |
| 128 | + &:hover |
| 129 | + color $accentColor |
| 130 | + &.router-link-active |
| 131 | + font-weight 600 |
| 132 | + color $accentColor |
| 133 | + border-left-color $accentColor |
| 134 | + .sidebar-group:not(.first) |
| 135 | + margin-top 1.5rem |
| 136 | + .sidebar-heading |
| 137 | + font-size 1.1em |
| 138 | + font-weight bold |
| 139 | + text-transform uppercase |
| 140 | + padding-left 1.5rem |
| 141 | + margin-top 0 |
| 142 | + margin-bottom 0.5rem |
| 143 | +</style> |
0 commit comments