Skip to content

Commit fb5730e

Browse files
committed
feat($theme-default): implement support for external links in sidebar
We introduce a new type of sidebar item called 'external' which are treated differently in Sidebar/SidebarLink. These items are rendered as <a> elements with an added icon indicating that the element points to an external location.
1 parent efece12 commit fb5730e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

packages/@vuepress/theme-default/components/Sidebar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default {
7070
function resolveOpenGroupIndex (route, items) {
7171
for (let i = 0; i < items.length; i++) {
7272
const item = items[i]
73-
if (item.type === 'group' && item.children.some(c => isActive(route, c.path))) {
73+
if (item.type === 'group' && item.children.some(c => c.type !== 'external' && isActive(route, c.path))) {
7474
return i
7575
}
7676
}

packages/@vuepress/theme-default/components/SidebarLink.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export default {
1515
const active = item.type === 'auto'
1616
? selfActive || item.children.some(c => isActive($route, item.basePath + '#' + c.slug))
1717
: selfActive
18-
const link = renderLink(h, item.path, item.title || item.path, active)
18+
const link = item.type === 'external'
19+
? renderExternal(h, item.path, item.title || item.path)
20+
: renderLink(h, item.path, item.title || item.path, active)
1921
const configDepth = $page.frontmatter.sidebarDepth != null
2022
? $page.frontmatter.sidebarDepth
2123
: $site.themeConfig.sidebarDepth
@@ -56,6 +58,19 @@ function renderChildren (h, children, path, route, maxDepth, depth = 1) {
5658
])
5759
}))
5860
}
61+
62+
function renderExternal(h, to, text) {
63+
return h('a', {
64+
attrs: {
65+
href: to,
66+
target: '_blank'
67+
},
68+
class: {
69+
'sidebar-link': true
70+
}
71+
}, [text, h('OutboundLink')])
72+
}
73+
5974
</script>
6075

6176
<style lang="stylus">

packages/@vuepress/theme-default/util/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export function isActive (route, path) {
5454
}
5555

5656
export function resolvePage (pages, rawPath, base) {
57+
if (isExternal(rawPath)) {
58+
return {
59+
type: 'external',
60+
title: rawPath,
61+
path: rawPath,
62+
}
63+
}
5764
if (base) {
5865
rawPath = resolvePath(rawPath, base)
5966
}

0 commit comments

Comments
 (0)